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;
}
Comments
2 Comments on Enabling AVR UART Rx
-
vikalp paharia on
Sun, 15th Nov 2009 12:32 am
-
devesh on
Sun, 15th Nov 2009 1:59 am
sir,
i have the same 3g4 interface you used in nexus. As far as i know it is programmed to work on 12000 mhz and 38400 baud rate. so how ccan we use it with the atmega8l(which i think works on 8000mhz) available in market.
i’am facing following problems.
1) is changing its clock by avr studio or by using a crystal of 12000mhz a good option
2) we are unable to program the atmega(clock 12000mhz) given to us at indore workshop by tri using avr dragon board.
First of all let me tell you that most of your information is incorrect. Its 2G4 module. Its not working on 1200 but 12 MHz. You are unable to program the device it can be happen due to many reasons like mistake in MAKE file, Project configuration mistake if you are using CVavr. It can be a hardware problem too. I suggest you to go through the documents provided by TRI.
Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!



