avr serial tutorial electroons

6
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. Share

Upload: neha-karthikeyan

Post on 20-Dec-2015

220 views

Category:

Documents


1 download

DESCRIPTION

avr serial communication tutorial

TRANSCRIPT

Page 1: Avr Serial Tutorial Electroons

Serial Communication(Asynchronous)   

Serial Communication is a form of I/O in which the bits of a byte beingtransferred appear one after other in a timed sequence on a single wire. SerialCommunication uses two methods, asynchronous and synchronous. The Synchronousmethod transfers a block of data at a time, while the asynchronous methodtransfers a single byte at a time. In Synchronous Communication the data gettransferred 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. Thesestart and stop bits are the parity bits to identify the data present betweenthe start and stop bits.

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

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

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

Share

Page 2: Avr Serial Tutorial Electroons

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 AVRdevice; second step is to connect all these stuff to make a set up that cancommunicate serially with other rs232 compatible devices, once programmed. Forthat here is the connection scheme…

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

Page 3: Avr Serial Tutorial Electroons

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

Use AVR Calculator to calculate UBRR value

UDR (UART Data Register) ‐ UDR is the buffer resigter in AVR UART. All databytes 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 thatfrom UDR. 

UCSRA (USART Control and Status Register A ) ‐ UCSRA register contains Receivecomplete and transmit complete flags. these flags are used to interrupt theCPU. 

UCSRB (USART Control and Status Register B ) ‐ UCSRB register contains ReceiveEnable and transmit Enable bits as well as it contain the TX,RX interruptenable bits, bits in this register (UCSZ2) alongwith some bits in UCSRCregister decides the frame size. 

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

/********************************************************************Example program to demonstrate UART transmitter workingThis program transmits a string continously by Tx.Any queries ‐ [email protected]********************************************************************/

Page 4: Avr Serial Tutorial Electroons

#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 enableUCSRC=1<<URSEL | 1<<UCSZ1 | 1<<UCSZ0; // 8 data bit, a stop, none parityUBRRH=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. Itis possible that 9600 baud rate may not work properly. If the above codedoesn’t work fine in your case reduce the baud rate to 2400 by replacing theUBBRL value to 25.

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

/******************************************************************** Exampleprogram to demonstrate the working of UART receiver.

Page 5: Avr Serial Tutorial Electroons

This program is designed to take input from our PC keyboard (Ofcourse whenHyper terminal open) and to display the same onto the LCD connected to ourexperiment 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 PORTDDRD=0x00; // PORTD as input as using as receiverDDRB=0xFF; // PORTB output as LCD is connected to itPORTD=0xFF;PORTB=0x00;UCSRA=0;UCSRB=(1<<RXEN)|(1<<TXEN); // Both receiver and transmitter enableUCSRC=1<<URSEL | 1<<UCSZ1 | 1<<UCSZ0; // 8 data bit, a stop, none parityUBRRH=0;UBRRL=25; // for 2400 baud at 1MHzlcd_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);

Page 6: Avr Serial Tutorial Electroons

lcd_cmd(0x80);lcd_cmd(0x10);}

if(c==0x0d) // carrige return{lcd_cmd(0xc0);lcd_cmd(0x10);}

lcd_data(c);

}return 0;}

HOME PAGE

[email protected] viewed at Firefox 1024x768 resolution