1 // vim: set sw=8 ts=8 si et:
2 /********************************************
3 * UART interface without interrupt
6 **********************************************/
7 #include <avr/interrupt.h>
11 #define F_CPU 8000000UL // 8 MHz
15 unsigned int baud=51; // 9600 baud at 8MHz
17 UBRR0H=(unsigned char) (baud >>8);
18 UBRR0L=(unsigned char) (baud & 0xFF);
19 // enable tx/rx and no interrupt on tx/rx
20 UCSR0B = (1<<RXEN0) | (1<<TXEN0);
21 // format: asynchronous, 8data, no parity, 1stop bit
22 UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);
24 UBRRH=(unsigned char) (baud >>8);
25 UBRRL=(unsigned char) (baud & 0xFF);
26 // enable tx/rx and no interrupt on tx/rx
27 UCSRB = (1<<RXEN) | (1<<TXEN);
28 // format: asynchronous, 8data, no parity, 1stop bit
29 UCSRC = (1<<URSEL)|(3<<UCSZ0);
33 // send one character to the rs232
34 void uart_sendchar(char c)
37 // wait for empty transmit buffer
38 while (!(UCSR0A & (1<<UDRE0)));
41 // wait for empty transmit buffer
42 while (!(UCSRA & (1<<UDRE)));
46 // send string to the rs232
47 void uart_sendstr(char *s)
55 void uart_sendstr_p(const prog_char *progmem_s)
56 // print string from program memory on rs232
59 while ((c = pgm_read_byte(progmem_s++))) {
65 // get a byte from rs232
66 // this function does a blocking read
67 char uart_getchar(void)
70 while(!(UCSR0A & (1<<RXC0)));
73 while(!(UCSRA & (1<<RXC)));
78 // get a byte from rs232
79 // this function does a non blocking read
80 // returns 1 if a character was read
81 unsigned char uart_getchar_noblock(char *returnval)
84 if(UCSR0A & (1<<RXC0)){
97 // read and discard any data in the receive buffer
98 void uart_flushRXbuf(void)
102 while(UCSR0A & (1<<RXC0)){
106 while(UCSRA & (1<<RXC)){