1 /* USART-Init beim ATmegaXX */
6 //#include <avr/signal.h>
7 #include <avr/interrupt.h>
22 #define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) ((F_CPU)/((UART_BAUD_RATE)*16L)-1)
26 volatile static char rxbuf[UART_RXBUFSIZE];
27 volatile static char txbuf[UART_TXBUFSIZE];
28 volatile static char *volatile rxhead, *volatile rxtail;
29 volatile static char *volatile txhead, *volatile txtail;
32 SIGNAL(SIG_UART_DATA) {
37 if ( txhead == txtail ) {
38 UCSRB &= ~(1 << UDRIE); /* disable data register empty IRQ */
40 UDR = *txtail; /* schreibt das Zeichen x auf die Schnittstelle */
41 if (++txtail == (txbuf + UART_TXBUFSIZE)) txtail = txbuf;
45 SIGNAL(SIG_UART_RECV) {
53 diff = rxhead - rxtail;
54 if ( diff < 0 ) diff += UART_RXBUFSIZE;
55 if (diff < UART_RXBUFSIZE -1) {
58 if (++rxhead == (rxbuf + UART_RXBUFSIZE)) rxhead = rxbuf;
60 UDR; //reads the buffer to clear the interrupt condition
64 #endif // UART_INTERRUPT
67 void uart_init(void) {
68 PORTD |= 0x01; //Pullup an RXD an
70 UCSRB |= (1<<TXEN); //UART TX einschalten
71 UCSRC |= (1<<URSEL)|(3<<UCSZ0); //Asynchron 8N1
73 UCSRB |= ( 1 << RXEN ); //Uart RX einschalten
75 UBRRH=(uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_CPU)>>8);
76 UBRRL=(uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_CPU));
80 rxhead = rxtail = rxbuf;
81 txhead = txtail = txbuf;
84 UCSRB |= (1 << RXCIE);
85 #endif // UART_INTERRUPT
89 void uart_putc(char c) {
94 diff = txhead - txtail;
95 if ( diff < 0 ) diff += UART_TXBUFSIZE;
96 } while ( diff >= UART_TXBUFSIZE -1 );
100 if (++txhead == (txbuf + UART_TXBUFSIZE)) txhead = txbuf;
102 UCSRB |= (1 << UDRIE); /* enable data register empty IRQ */
105 #else // WITHOUT INTERRUPT
106 void uart_putc(char c) {
107 while (!(UCSRA & (1<<UDRE))); /* warten bis Senden moeglich */
108 UDR = c; /* schreibt das Zeichen x auf die Schnittstelle */
110 #endif // UART_INTERRUPT
113 void uart_putstr(char *str) {
119 void uart_putstr_P(PGM_P str) {
121 while((tmp = pgm_read_byte(str))) {
127 void uart_hexdump(void* buf, int len)
129 unsigned char table[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
132 uart_putc(table[((*((char*)buf))>>4)&0xf]);
133 uart_putc(table[(*((char*)buf))&0xf]);
140 #ifdef UART_INTERRUPT
145 while(rxhead==rxtail) ;
148 if (++rxtail == (rxbuf + UART_RXBUFSIZE)) rxtail = rxbuf;
152 #else // WITHOUT INTERRUPT
155 while (!(UCSRA & (1<<RXC))); // warten bis Zeichen verfuegbar
156 return UDR; // Zeichen aus UDR zurueckgeben
158 #endif // UART_INTERRUPT
160 // returns 1 on success
161 #ifdef UART_INTERRUPT
162 char uart_getc_nb(char *c)
164 if (rxhead==rxtail) return 0;
167 if (++rxtail == (rxbuf + UART_RXBUFSIZE)) rxtail = rxbuf;
171 #else // WITHOUT INTERRUPT
172 char uart_getc_nb(char *c)
174 if (UCSRA & (1<<RXC)) { // Zeichen verfuegbar
181 #endif // UART_INTERRUPT