AVR echo

This code tested on ATmega16

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <avr/io.h>
 
#define FOSC 16000000UL
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
 
void USART_Init( unsigned int ubrr)
{
    UBRRH = (unsigned char)(ubrr>>8);
    UBRRL = (unsigned char)ubrr;
    UCSRB = (1<<RXEN)|(1<<TXEN);
    UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
 
int main()
{
    char c;
    USART_Init( MYUBRR );
    while(1)
    {
        while ( !(UCSRA & (1<<RXC))){};
        c = UDR;
        while (!(UCSRA & (1<<UDRE))){};
        UDR = c;
    }
    return 0;
}