#include <avr/io.h> #define BAUD 9600 #define MYUBRR F_CPU/16/BAUD-1 //This function is used to initialize the USART //at a given UBRR value void USARTInit(unsigned int ubrr) { //Set Baud rate UBRR0H = (ubrr>>8); UBRR0L = ubrr; //Enable The receiver and transmitter UCSR0B = (1<<RXEN0)|(1<<TXEN0); // Set fram format: 8data 2stopBit UCSR0C = (1<<USBS0)|(3<<UCSZ00); } //This function is used to read the available data //from USART. This function will wait untill data is //available. unsigned char USARTReadChar( void ) { //Wait untill a data is available while(!(UCSR0A & (1<<RXC0))) { //Do nothing } //Now USART has got data from host //and is available is buffer return UDR0; } //This fuction writes the given "data" to //the USART which then transmit it via TX line void USARTWriteChar(unsigned char data) { //Wait untill the transmitter is ready while(!(UCSR0A & (1<<UDRE0))) { //Do nothing PORTD ^= 1 << PINB2; } //Now write the data to USART buffer UDR0 = data; } int main(void) { DDRB |= 1 << PINB2; //Varriable Declaration char data; USARTInit(MYUBRR); //Loop forever USARTWriteChar('>'); while(1) { //Read data data = USARTReadChar(); /* Now send the same data but but surround it in square bracket. For example if user sent 'a' our system will echo back '[a]'. */ //USARTWriteChar('['); USARTWriteChar(data); //USARTWriteChar(']'); } }
So, das ist der Code