Professional Documents
Culture Documents
Lab Manual 07
Session Fall 22
Introduction
AVR AT mega has flexible USART, which can be used for serial communication with other devices. Before
beginning with AVR USART, we will walk through the basics of serial communication. Hundreds of
communication protocols have been defined to achieve this data exchange, and, in general, each can be
separated into one of two categories: parallel or serial.
Programming steps
Initialization of USART
Enable transmission and reception by using the UCSRB register.
Set data bit size to 8-bit by using the UCSRC register.
Set baud rate using the UBRR register.
Lab Task 01:
Design a system such that in which an LED is set to turn ON but as soon as an eternal interrupt is
generated it exits the main program turns ON the second LED and then comes bac to the main program.
Code (Transmitter)
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
int main(void) { DDRD =
0x00;
PORTD=0x00;
UCSR0B = (1<<TXEN0); //Enable Transmit ALSO receives it
UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); //8 bit data
UBRR0 = 103; //Set UBRR to get specified Baud Rate
//int led;
while(1) {
unsigned char count = 0 ;
if (PIND & (1<<PD1))
{
count = 1 ;
}
else
{ count = 0 ;
}
while(!(UCSR0A&(1<<UDRE0))); //wait until UDR0 is empty
UDR0 =count ; //transmit the number
}
}
Code (Receiver)
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
int main(void)
{
DDRD = 0xFF;
PORTD=0xff;
UCSR0B = (1<<RXEN0); //Enable Receive
UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); //8 bit data
UBRR0 = 103; //Set UBRR to get specified Baud Rate
unsigned char C;
while (1)
{
while(!(UCSR0A & (1<<RXC0))); //wait for new data
C = UDR0;
if (C == 1)
{ PORTD=0x01; }
else
PORTD=0x00;
}
}
Procedure:
Open proteus on your computer and place down Arduino 2 Mega 2560, resistor, LED, Push Button
and a ground as shown in the picture.
Copy the hex file from Atmel Software and paste the hex file in the Arduino Mega configuration in
Proteus.
CONCLUSION
In this lab we studied different modes of communication between different modules. We studied all the
registered that are involved in the transfer of data from one path to the other path which also include the baud
rate register. We connected two Arduino mega using USART.