Enabling AVR UART Rx
hi…
In prevoius post i explained the UART transmit function with code and schematic. Now this post is about receiving datra from outside world (e.g. a PC having serial port).
The schematic will remain the same, for convenience i am putting it here also.
In previous post to enable Transmitter we enabled the transmitter by setting the TXEN bit in UCSRB. Now we must enable RXEN in UCSRB i.e. UCSRB= (1<<RXEN).
Now here to demonstrate the working of UART receiver i made a kind of Interactive system. Here we will program Mega8 to receive certain characters via UART and display them to a 16×2 LCD connected to PORTB of ATmega8.
So whwtever you type on PC keyboard with terminal utility open the same get displayed on the LCD Module.
LCD connections are as follows…
RS – PORTB_0
RW – PORTB_1
EN – PORTB_2
D4 – PORTB_4
D5 – PORTB_5
D6 – PORTB_6
D7 – PORTB_7
How to interface a LCD to AVR???
/*******************************************************************
*Program functions in such a way that when you press ESC LCD display get clear
*when you press ENTER key on Keyboard Cursor goes to second line
*Auther - devesh@electroons.com
********************************************************************/
#include<avr/io.h>
#include<util/delay.h>
#include<compat/deprecated.h>
#include<lcd.h>
unsigned char UART_rx()
{
while(!(UCSRA & (1<<RXC)));
return UDR;
}
int main(void)
{
unsigned char c;
//SET DATA DIRECTION REGISTER
//SET 1 for OUTPUT PORT
//SET 0 FOR INPUT PORT
DDRD=0x00; // PORTD as input as using as receiver
DDRB=0xFF; // PORTB output as LCD is connected to it
PORTD=0xFF;
PORTB=0x00;
UCSRA=0;
UCSRB=(1<<RXEN)|(1<<TXEN); // Both receiver and transmitter enable
UCSRC=1<<URSEL | 1<<UCSZ1 | 1<<UCSZ0; // 8 data bit, a stop, none parity
UBRRH=0;
UBRRL=25; // for 2400 baud at 1MHz
lcd_init();
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("LCD Working");
while(1)
{
c=UART_rx();
if(c==0x1b) // if ESC pressed clear the screen
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_cmd(0x10);
}
if(c==0x0d) // carrige return
{
lcd_cmd(0xc0);
lcd_cmd(0x10);
}
lcd_data(c);
}
return 0;
}



