Thursday, March 15, 2012

Transmit Character Through UART

In this tutorial I am going to show you how to transmit a character through UART. First of all we have to initialize the UART circuitry of our micro-controller by providing proper values to the UART registers in AVR. There are basically 5 registers for UART....
(these codes have been written for ATmega128 micro-controller which has two UART circuitry, UART0 & UART1, the same code can be used with other AVR micro-controllers with some minor changes)

*********************************************************************************
Transmit a Character Through UART
*********************************************************************************

//Include AVR Header file
 #include < avr/io.h >
 
//Initialize UART circuitry
void UART1_Init()
{
     //set the baud rate as 9600 bits per second
     UBRR1H = 0x00;
     UBRR1L = 0x67;     //or 103 in decimal
     
     //set character size as 8-bit, no parity bit, one stop bit 
     UCSR1C = 0x06     // (1 << UCSZ1)|(1 << UCSZ0);
     
     //enable reception and transmission
     UCSR1B = 0x18     // (1 << RXEN)|(1 << TXEN);



//Start main function
void main()
{
   
     UART1_Init();
     
     //put the desired character in UDR register
     UDR1 = '@';

     //wait for the transmission to be completed
     while(! (UCSR1A & (1 << UDRE)));
}

1 comments:

Unknown said...

Dear,but i need to also compare it and have to get according output so how can i store the recieved string and than compare it to get output
Thank you.

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews