]> git.tdb.fi Git - model-railway-devices.git/blob - firmware/serial.c
2bf6901836064ece4517047dcb8d94f70838d99f
[model-railway-devices.git] / firmware / serial.c
1 #include <avr/io.h>
2 #include "serial.h"
3
4 #define BIT(n) (1<<(n))
5
6 void serial_init(uint16_t baud)
7 {
8         DDRD = (DDRD&~0x03) | 0x02;
9         PORTD &= ~0x03;
10
11         baud = (F_CPU/16+baud/2)/baud-1;
12         UBRR0H = baud>>8;
13         UBRR0L = baud;
14         UCSR0C = BIT(UCSZ00) | BIT(UCSZ01);  // 8N1
15         UCSR0B = BIT(RXEN0) | BIT(TXEN0) | BIT(RXCIE0);
16 }
17
18 void serial_write(uint8_t c)
19 {
20         while(!(UCSR0A&(1<<UDRE0))) ;
21         UDR0 = c;
22 }