Aim:
USART is one of the primitive inter-device communication protocols. It
is not used in modern computers. But still, a few mother boards come
with the module necessary for an USART communication. Here, in the case
of PCs, the port is known as COM port and it follows RS232 protocol. It
is no different from USART except for the voltage levels and some
additional signals. Commonly MAX232 IC is used to translate the voltage
level. MAX232 is a simple voltage translator buffer that converts
+12/-12V of RS232 to 5/0V for USART. Other specifications are similar
for USART and RS232. To know more about USART read this article from Wikipedia.
Description:
Let us understand the serial communication in AVR microcontrollers.
Here ATMEGA sends data to the other ATMEGA in serial. It has other mode
of communication but for easy communication we are choosing RS232. The
RS232 pin of first ATMEGA8 is connected to RXD pin of second ATMEGA8.
The data communication established is programmed to have:
- Eight data bits
- Two stop bits
- No parity check bit
- Baud rate of 2400 BPS(Bits Per Second)
- Asynchronous communication (No clock share between two ATMEGA8)
So we have two set registers for two ATMEGA8 differently, where one acts as TRANSMITTER and other acts as RECEIVER.
Now for the RS232 interfacing between two ATmega microcontrollers, the following features must be satisfied for the TRANSMITTER and RECEIVER:
- The TXD pin (data receiving feature) of first controller must be enabled for TRANSMITTER and RXD pin of second controller must be enabled for RECEIVER .
- Since the communication is serial we need to know whenever the data byte is received, so that we can stop the program until complete byte is received. This is done by enabling a data receive complete interrupt.
- DATA is transmitted and received to controller in 8bit mode. So two characters will be sent to the controller at a time.
- There are no parity bits, one stop bit in the data sent by the module.
The above features are set in the controller registers; we are going to discuss them briefly,
DARK GREY (UDRE): (TRASMITTER SIDE) This bit not set during startup but
it is used during working to check whether transmitter is ready to
transmit or not. See the program on TRASMITTER SIDE for more details.
LIGHT GREY (RXC): (RECEIVING SIDE) This bit not set during startup but
it is used during working to check whether receiver is ready to receive
data or not. See the program on RECEIVING SIDE for more details.
VOILET (TXEN): (TRASMITTER SIDE) This bit is set for enabling transmitter pin on TRASMITTER SIDE.
RED (RXEN): (RECEIVING SIDE) This bit represents receive data feature,
this bit must be set for the data from the module to be received by the
controller, it also enables RXD pin of controller.
BROWN (RXCIE): This bit must be set for getting an interrupt after
successful data reception. By enabling this bit we get to know, right
after 8 bit data receive. We are not going to use this bit here so it is
left alone.
PINK (URSEL): This bit must be set before enable other bits in UCSRC,
after setting other needed bits in UCSRC; URSEL must be disabled or put
to zero. We are not going to use this bit here so it is left alone.
YELLOW(UCSZ0,UCSZ1,UCSZ2): (RECEIVING SIDE & TRASMITTER SIDE)These
three bits are used for selecting the number of data bits we are
receiving or sending in a single go.
The communication between two ATMEGA is established as eight bit
communication. By matching the communication with table we have, UCSZ0,
UCSZ1 to one and UCSZ2 to zero.
We have to set these on both receiving and transmitting side.
ORANGE (UMSEL): (RECEIVING SIDE & TRASMITTER SIDE)This bit is set
based on whether the system is communicating asynchronously (both use
different clock) or synchronously (both use same clock).
Both the controllers do not share any clock. Since both of them use
internal clock of their own. So we need to set UMSEL to 0 in both
controllers.
GREEN (UPM1, UPM0): (RECEIVING SIDE & TRASMITTER SIDE) These two
bits are adjusted based on bit parity we are using in communication.
The ATMEGA is programmed to send data with no parity, as the data
transmission length is small, we can clearly expect no data loss or
error. So we are not setting any parity here. So we set both UPM1, UPM0
to zero or they are left, because all bits are 0 by default..
BLUE (USBS): (RECEIVING SIDE & TRASMITTER SIDE) This bit is used for
choosing the number of stop bits we are using during communication.
The communication established here is of asynchronous type, so for
getting more accurate data transmission and reception, we need to use
two stop bits, Hence we set USBS to ‘1’ in both controllers.
The baud rate is set in controller by choosing the appropriate UBRRH.
The UBRRH value is chosen by cross referring baud rate and CPU crystal frequency.
So by cross reference UBRR value is seen as ‘25’, and so the baud rate is set.
As shown in circuit a button is connected on transmitter side. When this
button in pressed a eight bit data is sent by TRANSMITTER and this data
is received by RECEIVER. On receiving this data successfully it toggles
the LED connected to it ON and OFF, which shows successful data
transfer between two controller.
Block Diagram
Schematic
Code
// *****************************************************
// Project: Serial UART communication using atmega8
// Author: Hack Projects India
// Module description: Operate max232
// *****************************************************
#define F_CPU 8000000UL
#include <avr/io.h>
#include<util/delay.h>
void USART_Init()
{
UBRRL=0x33;
UCSRB=(1<<TXEN)|(1<<RXEN);
UCSRC=(3<<UCSZ0)|(1<<UMSEL);
}
unsigned char USART_Receave()
{
while(!(UCSRA&(1<<RXC)));
return UDR;
}
void USART_Transmit(unsigned char data)
{
UDR=data;
while(!(UCSRA&(1<<TXC)));
_delay_ms(1);
}
int main(void)
{
unsigned char x;
USART_Init();
_delay_ms(200);
while(1)
{
x=USART_Receave();
USART_Transmit(x);
}
}
Downloads:
The code was compiled in Atmel Studio 6 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.
Further Reading suggestions:
You may also like,
- Interfacing keypad with AVR
- nterfacing DAC with AVR
- Interfacing SPI communication with AVR
- AVR Displaying Custom Characters on LCD
- AVR Graphical LCD
- RTC interfacing using I2C in AVR
- Interfacing ultrasonic sensor with AVR
- Interfacing GPS Modu with AVR
- Interfacing GSM Module with AVR
- Interfacing PWM in AVR
- Interfacing ADC with AVR
- Scrolling string on LCD using AVR
- Interfacing keypad with AVR
- nterfacing DAC with AVR
- Interfacing SPI communication with AVR
- AVR Displaying Custom Characters on LCD
- AVR Graphical LCD
- RTC interfacing using I2C in AVR
- Interfacing ultrasonic sensor with AVR
- Interfacing GPS Modu with AVR
- Interfacing GSM Module with AVR
- Interfacing PWM in AVR
- Interfacing ADC with AVR
- Scrolling string on LCD using AVR
No comments:
Post a Comment