Enabling AVR UART Rx

November 10, 2009 by admin · 2 Comments
Filed under: Microcontrollers 

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;

}

Enabling AVR UART tx

November 9, 2009 by admin · Leave a Comment
Filed under: Microcontrollers 

hi…
this post will cover the topic How to enable and use the inbuilt UART (Universal Asynchronous Receiver Transmitter)??? So lets start with the basic set up we need to do this experiment…

We need…
>>ATmega8 with internal clock source enabled
>>MAX232 it is the rs232->TTL and TTL->rs232 logic level converter.
>>5 Capacitors of value 1uF/63V
>>Serial cable

Now if you have the above hardware stuff with a programmer to program the AVR device; second step is to connect all these stuff to make a set up that can communicate serially with other rs232 compatible devices, once programmed. For that here is the connection scheme…

rs232

Why we need a logic converter (TTL <–> RS232)???

As our AVR microcontroller deals only with TTL signal level for communicating with external world but RS232 standard has logic level voltages somewhat different. For RS232 Logic 0 = -25V while Logic 1= +25V.

Here is the code that continously sends a string to the PC asynchronously.

/************************************************************/

#include<avr/io.h>
void UART_transmit(unsigned char data);
int main(void)
{
unsigned char i,message[]="devesh samaiya\r\n";
DDRD=0x00;
PORTD=0xFF;
UCSRA=0;
UCSRB=1<<TXEN;  // transmitter enable
UCSRC=1<<URSEL | 1<<UCSZ1 | 1<<UCSZ0;  // 8 data bit, a stop, none parity
UBRRH=0;
UBRRL=5; // for 9600 baud at 1MHz
while(1)
{

for(i=0;message[i];i++)
{
UART_transmit(message[i]);
}

}
}

void UART_transmit(unsigned char data)
{
while(!(UCSRA & (1<<UDRE)));
UDR=data;
}
/*****************************************************************/

Note as we are using the internal clock source of 1MHz to run the system. It is possible that 9600 baud rate may not work properly. If the above code doesn’t work fine in your case reduce the baud rate to 2400 by replacing the UBBRL value to 25.

And here is the output

rs232_tx

Output Screen on gtkTerm Ubuntu

rs232_tx

Next UART receiving function routines…..

Next Page »