Created
June 25, 2022 00:31
-
-
Save ximeg/469cb0aed8be84c5764f26683c71a8bd to your computer and use it in GitHub Desktop.
Control of Arduino registers over UART
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define uchar unsigned char | |
void setup_timer_counter_1(){ | |
/* TCCR1: Timer Counter Control Register */ | |
// bit 7 6 5 4 3 2 1 0 | |
uchar _TCCR1A = 0; // COM1A1 COM1A0 COM1B1 COM1B0 - - WGM10 WGM11 | |
uchar _TCCR1B = 0; // ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10 | |
uchar _TCCR1C = 0; // FOC1A FOC1B - - - - - - | |
// Set COM (Compare Output Mode) for pins OC1A (#9) and OC1B (#10) | |
_TCCR1A |= bit(COM1A0) | bit(COM1B0); // toggle pins on compare match | |
// Set WGM (Waveform Generation Mode) | |
_TCCR1B |= bit(WGM12); // Mode 4, CTC with top=OCR1A | |
// Select the clock source | |
_TCCR1B |= bit(CS12) | bit(CS10); // 1024x prescaler | |
// Write the registers | |
TCCR1A = _TCCR1A; | |
TCCR1B = _TCCR1B; | |
TCCR1C = _TCCR1C; | |
/* OCR1: Output Compare Register */ | |
// note: OCR1A is top; OCR1A must be larger than OCR1B | |
// 0x3D09 = 15625 x 64us = 1s (with 1024 prescaler) for pin #9 | |
OCR1AH = 0x3D; | |
OCR1AL = 0x09; | |
// 0x0F42 = 3906 x 64us = 250ms (with 1024 prescaler) for pin #10 | |
OCR1BH = 0x0F; | |
OCR1BL = 0x42; | |
} | |
// the setup function runs once when you press reset or power the board | |
void setup() { | |
// initialize digital pin LED_BUILTIN as an output. | |
// All pins on port B are outputs | |
DDRB = 0xFF; | |
setup_timer_counter_1(); | |
Serial.begin(2000000); | |
} | |
void parse_input(const char * input){ | |
char r_addr_hex[3]; | |
unsigned char r_addr; | |
char r_val_hex[3]; | |
unsigned char r_val; | |
memcpy(r_addr_hex, &input[1], 2); | |
r_addr_hex[2] = 0; | |
r_addr = strtoul(r_addr_hex, NULL, 16); | |
if (input[0] == 'W'){ | |
memcpy(r_val_hex, &input[3], 2); | |
r_val_hex[2] = 0; | |
r_val = strtoul(r_val_hex, NULL, 16); | |
Serial.print("Writing value 0x"); | |
Serial.print(r_val_hex); | |
Serial.print(" ("); | |
Serial.print((long) r_val); | |
Serial.print(") to register 0x"); | |
Serial.print(r_addr_hex); | |
Serial.print(" ("); | |
Serial.print((long) r_addr); | |
Serial.println(")"); | |
*( (unsigned char *) r_addr) = r_val; | |
}else{ | |
r_val = *((unsigned char *) r_addr); | |
Serial.print("r"); | |
Serial.print(r_addr_hex); | |
Serial.println(r_val, HEX); | |
} | |
} | |
char input[6]; | |
int charsRead; | |
unsigned long val; | |
// the loop function runs over and over again forever | |
void loop() { | |
if (Serial.available() > 0){ | |
charsRead = Serial.readBytesUntil('\n', input, 5); | |
input[charsRead] = '\0'; | |
parse_input(input); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment