some cleanup and formatting

master
Ondřej Hruška 9 years ago
parent d49fa2ba51
commit b70cf17e53
  1. 6
      README.md
  2. 19
      lib/usart.c
  3. 3
      lib/usart.h
  4. 2
      main.c

@ -71,4 +71,8 @@ created by the compiler before linking) to the `OBJS` list in the Makefile.
## Notes ## 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);` - The **Arduino UNO** bootloader has a quirk where `Double Speed Asynchronous Mode` for USART
is enabled by default, so if you set your baud rate to 9600, you'd really get 19200.<br>
We correct this in the `usart_init()` function to keep things consistent and to avoid
confusion.<br>
*If you wish to turn this on* however, you can do so by using `usart_set_2x(true)`.

@ -26,16 +26,14 @@ void usart_init(uint16_t ubrr)
UCSR0C = (0b11 << UCSZ00); UCSR0C = (0b11 << UCSZ00);
} }
/** Set Double Speed Asynchronous mode */
void usart_set_2x(bool set) void usart_set_2x(bool set)
{ {
// Turn on Double Speed Asynchronous Mode set_bit(UCSR0A, U2X0, set);
if (set)
sbi(UCSR0A, U2X0);
// Turn off Double Speed Asynchronous Mode
else
cbi(UCSR0A, U2X0);
} }
/** Send byte over USART */ /** Send byte over USART */
void usart_tx(uint8_t data) void usart_tx(uint8_t data)
{ {
@ -59,8 +57,7 @@ uint8_t usart_rx(void)
/** Send string over USART */ /** Send string over USART */
void usart_puts(const char* str) void usart_puts(const char* str)
{ {
while (*str) while (*str) {
{
usart_tx(*str++); usart_tx(*str++);
} }
} }
@ -70,8 +67,7 @@ void usart_puts(const char* str)
void usart_puts_P(const char* str) void usart_puts_P(const char* str)
{ {
char c; char c;
while ((c = pgm_read_byte(str++))) while ((c = pgm_read_byte(str++))) {
{
usart_tx(c); usart_tx(c);
} }
} }
@ -81,8 +77,7 @@ void usart_puts_P(const char* str)
void usart_flush_rx(void) void usart_flush_rx(void)
{ {
uint8_t dummy; uint8_t dummy;
while (bit_is_high(UCSR0A, RXC0)) while (bit_is_high(UCSR0A, RXC0)) {
{
dummy = UDR0; dummy = UDR0;
} }
} }

@ -35,12 +35,15 @@ enum {
/** Init UART with a UBRR value - can use the BAUD_* constants for 16 MHz */ /** Init UART with a UBRR value - can use the BAUD_* constants for 16 MHz */
void usart_init(uint16_t ubrr); void usart_init(uint16_t ubrr);
/** Set Double Speed Asynchronous mode on or off */ /** Set Double Speed Asynchronous mode on or off */
void usart_set_2x(bool set); void usart_set_2x(bool set);
/** Check if there's a byte in the RX register */ /** Check if there's a byte in the RX register */
#define usart_rx_ready() bit_is_high(UCSR0A, RXC0) #define usart_rx_ready() bit_is_high(UCSR0A, RXC0)
/** Check if USART is ready to accept new byte to send */ /** Check if USART is ready to accept new byte to send */
#define usart_tx_ready() bit_is_high(UCSR0A, UDRE0) #define usart_tx_ready() bit_is_high(UCSR0A, UDRE0)

@ -26,7 +26,7 @@ ISR(USART_RX_vect)
void main() void main()
{ {
usart_init(BAUD_9600); usart_init(BAUD_115200);
usart_isr_rx_enable(true); // enable RX interrupt handler usart_isr_rx_enable(true); // enable RX interrupt handler
// configure pins // configure pins

Loading…
Cancel
Save