Saturday, March 17, 2012

Receive Character Through UART

In this tutorial we are going to receive a single character through UART and display that character over PORTC, to which we can connect the LEDs.
(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)
#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);
}

void main(){
//define PORTC as an output port to display received char
DDRC = 0xFF;

//initialize UART circuitry

 UART1_Init();

//wait for the reception to be completed
while(! (UCSR1A & (1 << RXC)));

//display the received character on PORTC
PORTC = UDR1;
}

0 comments:

Post a Comment

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