Knowledge Base‎ > ‎General Gyaan‎ > ‎Tutorials‎ > ‎

Data Communication : UART

posted Sep 17, 2016, 6:51 AM by Rohit Bhaskar
One useful feature of a micro-controller is that it can send and receive data to and from another micro-controller or computer. This is particularly useful when we are using plenty of micro-controllers and data-communication between them is needed. Another very useful application is that data can be viewed real time as it is being output by the MCU,while developing applications. 

There are two ways for communication : SPI and USART. Here's a little about USART. 

USART :

Introduction : 

USART stands for Universal Synchronous Asynchronous Receiver Transmitter. It sends and receives data and is incorporated in the MCU itself. Data can be sent either synchronously and asynchronously. The difference between UART and USART is that in synchronous mode(USART only) the device require both data and clock. The clock is recovered from the data or an external one which is synchronous with data. In asynchronous mode(UART) the device requires only data. The data clock is internally generated and synchronised with start and stop bits embedded in the data received.Lets deal with UART first without worrying about clocks. 

UART is not very difficult to understand.Here's the basic layout : 

Hardware and firmware : 

On the hardware front, it has 3 pins : a transmit pin, a receive pin and a clock pin(but let's not talk about that). There are not too many connections here. Just make sure the transmit pin of one chip is connected to the receive pin of the other for both chips. 

It has 3 control registers for applying settings according to requirement, a buffer for storing received data or to hold data to be sent. There are seperate buffers for transmitting and receiving, but they can be accessed by the same variable. 

Data is sent/received serially and is automatically moved out of the buffers by accessing the data register(during reception) or by writing to the buffer(during transmission). 

A complete explanation of the control registers can be found on pages 164 to 167 of the ATMEGA16 datasheet. I am attaching a link to the ATMEGA 16 Datasheet. The pages are very comprehensive and easy to understand. An explanation shouldn't be necessary. You can however email us or read the AVR-Freaks forums for queries. 

Writing the code : I wrote a program to test UART. In this program, I send a string from the first Atmega16 and the same is received by the other Atmega16. It tests the last received character for a value to check if the string was received intact. It is a rather inaccurate assumption on my part that the last character guarantees correct reception, but then, if you are transmitting real-time data, you wouldn't be able to know or check what you are transmitting anyways. 

Code :
Click here to view the USART code
 Just burn these codes into the MCUs, connect the Rx and Tx pins as above, and you are good to go. Modify the codes if you want the output differently. Adding an LCD output function to this code would be a very good way to do that. 

Remember : 
1. You cannot transmit more than one character at a time. Please do not try. It will only lead to erroneous output(if any). 2. Please initialise the control registers correctly. A wrongly initialised/ uninitialised control register will lead to erroneous/no output. 
3. All settings ( baud rate, clock speeds,parity bits, stop bits) should be the same for both micro-controllers. Micro-controllers with different clock speeds should also work fine, but I recommend changing them anyways. For details, refer to my previous post, Fuse Bits : Changing your MCU speed and JTAG.

References 

1. ATMEGA16 Datasheet 
2. AVR-Freaks forums 
3. USART Sample Code



Contributed by :
Ankit Daftery
ankitdaf [at] gmail [dot] com

Ċ
USART.pdf
(47k)
Rohit Bhaskar,
Sep 17, 2016, 6:51 AM
Comments