#define FOSC 11059200
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <inttypes.h>
void USART_Init(unsigned int);
void USART_Transmit(unsigned char data);
unsigned char USART_Receive(void);
unsigned char a;
int main(void)
{
USART_Init (MYUBRR);
while(1)
{
a=USART_Receive();
USART_Transmit(a);
}
}
void USART_Init(unsigned int ubrr)
{
/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 1stop bit */
UCSRC |= (1 << URSEL) | (1 << UCSZ1) | (1<<UCSZ0);
}
void USART_Transmit(unsigned char data)
{
/* Wait for empty transmit buffer */
while (!( UCSRA & (1<<UDRE)));
/* Put data into buffer, sends the data */
UDR = data;
}
unsigned char USART_Receive(void)
{
/* Wait for data to be received */
while (!(UCSRA & (1<<RXC)));
/* Get and return received data from buffer */
return UDR;
}