diff --git a/README.md b/README.md index a25f34c..c682fab 100644 --- a/README.md +++ b/README.md @@ -68,3 +68,7 @@ created by the compiler before linking) to the `OBJS` list in the Makefile. - Similarly, if you *add a new folder with header files*, add it to `INCL_DIRS`. - In case you need `printf` (or `printf` with floats), enable the appropriate LD_FLAGS in the Makefile (it's well commented). Code size will - obviously - grow quite a bit. + +## Notes + +- Since the Arduino Uno has a quirk where Double Speed Asynchronous Mode for USART is enabled by default, we made sure to disable this. If this mode is enabled it means that the USART baud rate is doubled, so if you were using a baud rate of `9200` in the code, you'd have to use a baud rate of 19200 in your terminal. We disabled this to keep things consistent, and because it has no real benefit. If you wish to turn this on however, you can do so by using `usart_set_2x(true);` \ No newline at end of file diff --git a/lib/usart.c b/lib/usart.c index e9e020a..0ca3783 100644 --- a/lib/usart.c +++ b/lib/usart.c @@ -19,10 +19,22 @@ void usart_init(uint16_t ubrr) // Enable Rx and Tx UCSR0B = (1 << RXEN0) | (1 << TXEN0); + // Clear U2X0 + cbi(UCSR0A, U2X0); + // 8-bit data, 1 stop bit UCSR0C = (0b11 << UCSZ00); } +void usart_set_2x(bool set) +{ + // Turn on Double Speed Asynchronous Mode + if (set) + sbi(UCSR0A, U2X0); + // Turn off Double Speed Asynchronous Mode + else + cbi(UCSR0A, U2X0); +} /** Send byte over USART */ void usart_tx(uint8_t data) diff --git a/lib/usart.h b/lib/usart.h index 5c325f7..6857e98 100644 --- a/lib/usart.h +++ b/lib/usart.h @@ -35,6 +35,9 @@ enum { /** Init UART with a UBRR value - can use the BAUD_* constants for 16 MHz */ void usart_init(uint16_t ubrr); +/** Set Double Speed Asynchronous mode on or off */ +void usart_set_2x(bool set); + /** Check if there's a byte in the RX register */ #define usart_rx_ready() bit_is_high(UCSR0A, RXC0)