Serial Communication (Asynchronous)  
Serial Communication is a form of I/O in which the bits of a byte being transferred appear one after other in a timed sequence on a single wire. Serial Communication uses two methods, asynchronous and synchronous. The Synchronous method transfers a block of data at a time, while the asynchronous method transfers a single byte at a time. In Synchronous Communication the data get transferred based on a common clock signal. But in Asynchronous communication, in addition to the data bit, one start bit and one stop bit is added. These start and stop bits are the parity bits to identify the data present between the start and stop bits.

The ATmega16 has two pins that are used specifically for transferring and receiving data serially. These two pins are called TXD and RXD and are part of the Port-D group (PortD.0 and PortD.1). Pin 14 of the Mega16 is assigned to RXD and pin 15 is designated as TXD. These pins are TTL compatible; therefore they require a line driver to make them RS232 compatible. The line driver chip is MAX232. The MAX232 uses +5v power source, which is same as the source voltage for AVR.

AVR MC transfers and receives data serially at different baud rates. The baud rate of the AVR is programmed into the timers.

Generally Null Modem Connections are used for Data transfer between two device serially.



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…


Value of BAUD rate of communication is decided using value in UBRR (UART Baud Rate Register)register. The baud rate is dependent on UBRR value according to the following relation.



Suppose we need 2400 BAUD at 1MHz. Put value of Fosc and BAUD in the above formulae.

Use AVR Calculator to calculate UBRR value

UDR (UART Data Register) - UDR is the buffer resigter in AVR UART. All data bytes that are to be transmitted fron UART must first be placed in UDR. Similarly all incoming data first come into UDR and then we can retrive that from UDR.



UCSRA (USART Control and Status Register A ) - UCSRA register contains Receive complete and transmit complete flags. these flags are used to interrupt the CPU.



UCSRB (USART Control and Status Register B ) - UCSRB register contains Receive Enable and transmit Enable bits as well as it contain the TX,RX interrupt enable bits, bits in this register (UCSZ2) alongwith some bits in UCSRC register decides the frame size.



To know more details about these register refer to the datasheet. AVR datashets are the best way to learn about them.



/********************************************************************
Example program to demonstrate UART transmitter working
This program transmits a string continously by Tx.
Any queries - devesh@electroons.com
********************************************************************/


#include<avr/io.h>

void UART_transmit(unsigned char data);

int main(void)
{
unsigned char i,message[]="i love india\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]);
}
} // while(1) end
} // main() end


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.

Use Hyper Terminal in Windows XP or gtkTerm in Linux to see the transmitted string.








/******************************************************************** Example program to demonstrate the working of UART receiver.

This program is designed to take input from our PC keyboard (Ofcourse when Hyper terminal open) and to display the same onto the LCD connected to our experiment board.

Press ESC to clear LCD screen

Press Enter to go to second line

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


#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;
}









devesh@electroons.com
Best viewed at Firefox 1024x768 resolution