
UART BASICS
Ver.01.090425
INTRODUCTION
The Universal Asynchronous Receiver Transmitter or simple UART ("you
art" as some will pronounce) is one of the common peripheral found on
microcontrollers (MCU) widely used for communication with the external devices
and systems. Modules, ASIC's , and PC's are among the devices that the
microcontroller can communicate to through the UART.
For microcontrollers that offer this peripheral, the UART circuitry is built-in
the chip and can be accessed from 2 pins, a transmitter and a receiver usually
named RX and TX or RXD and TXD respectively.
With such a configuration, a full duplex set up is possible. Although in some
applications, such as in display modules, only the transmit pin may be utilized
as the MCU only needs to send graphic information to the display. A GPS
module on the other hand, may only require the UART's receive pin since the MCU
only awaits the GPS data but need not sent information to the module. In any
case, we can say that the UART can receive and transmit information to and from
another compatible device.
OBJECTIVE
The objective of this article is to demonstrate the use of the UART that is
built-in a microcontroller unit(MCU) in basic applications intended for the
novice electronics designer. It shall cover the physical layer design and
fundamental software control. In-depth discussion of the data protocol will not
be covered. This short article is intended as a practical guide on UART
interfacing
UART FUNDAMENTALS
As mentioned earlier, the UART is used for digital communications. The elements
of a usual setup, which we shall adopt in this discussion, is a UART of an
external device or system connected to the built-in UART of the host MCU. So
long as both UART's are operating at the same voltage level, the connection
between the two systems shall be a direct connection either by wire or PCB
track.
It is noteworthy to mention at this point that the UART is capable of
transmitting/receiving 7 or 8 bits of data plus 2 or 3 framing bits. One start
bit, eight data bits and one stop bit is the common configuration. We can
therefore say that in common practice, the UART can receive or transmit 1 byte
of data.
The transmit and receive pins of the UART are almost independent of each other
in terms of its main function. In asynchronous mode, the RX pin can practically
receive data regardless of the TX pin’s activity and vice versa. We could
therefore break the system into two independent half duplex systems as in
Figure 1:

figure 1:
UART communication system.
UART DATA PACKET
Figure 2 shows the arrangement of the data sent in one UART "packet".
The packet begins with start bit, which is a logic 0, being
transmitted/received first. In the software side, this bit is important as we
can poll the RX pin for this bit to signal that a packet of data is comming.
The data bits or the payload may or may not have a parity bit. Below shows how
a typical data packet would be:

Figure 2: A
typical UART data packet
A detailed tutorial can be viewed in another section of this website. Please
check the index page.
UART REGISTERS
To use and control the UART, special internal registers are assigned to them.
Usually there will be at least four registers: control, status, receive and
transmit registers. All these vary in size depending on the MCU. For example,
high-end processors will have three-word control register for setting various
features/settings that may not be present in simpler processors, hence, one
word register may be enough.
1) Control Register - Contains settings for the UART. This register/s must be
set prior to use. Some common settings/features include: Number of databits,
number of stop bits, parity control, UART TX/RX enable/disable, baud rate
setting, RX/TX interrupt enable, etc.
2) Status Register - From its name, this contains information about the UART's
condition or state. During run-time, this register may be helpful in guiding
the processor on the next instruction to execute like when to retrieve data.
Information that can be retrieved include: data received flag, data
send/receive ready, UART active flag,etc.
3) Receive Register - This is the where received data is temporarily stored.
4) Transmit Register - A buffer register/s for temporarily storing data to be
sent.
USING THE UART
With the above basic information, we are now ready for the practical part. Our
goal is be able to wire the UART to an external device's UART and establish
communication.
Connecting the MCU's UART is a simple direct connection to the external device
so long as both logic levels are the same. Of course, the ground connection
should be common for both device. Below shows the pin connection between the
MCU and an external device, say, a GSM Module:

Figure 3:
Notice that the RX and TX pins are "crossed" against the other RX/TX.
Since a TX is an output pin, it should be connected to an input pin and vice
versa. A common mistake is to connect both RX's together and TX's in another
joint. You will have to be careful though on the name given to the pins both of
the MCU and the external device. The names
"RX","Receive","Receiver" will often pertain to
the UART's input pin relative to itself. The
"TX","Transmit" or "transmitter" pin is the
output pin at the point of view of the MCU. To be sure that your RX/TX pin is
your Input or Output pin, go over the datasheet of your device.
After this connection, the UART is almost ready for use! The remaining
requirement is to set the necessary settings in the software.
Before any software control is done, one should set the UART's control registers
accordingly. Please refer to your
data sheet for proper settings. Another article in this
website is dedicated to examples of UART settings needed in common MCU's.
Please go over the index page.
The register settings are often done in the main function before the main loop
starts. Below is how it would look like in the code:
main()
{
//lines defining UART's register
//lines defining UART's register
//lines defining UART's register
//line enabling the UART or UART's interrupt
while(1) // main loop
{
...
}
}
SOFTWARE UART CONTROL
After the pysical connection and register set-up, you are now ready to actually
"use" the UART in the MCU's software. Beginners may often be
intimidated by the programming part, but the goodnews is, it's simpler than it
is perceived. This is so because most C compilers will have directed the
so-called "stdio" interface to the UART. In terms of C code, that
would translate to the printf,puts,putch,putchar transmit commands and the
scanf,gets,getch,getchar receive commands. The equivalent machine instruction
of the above C instructions is of course, writing or reading the TX or RX
register. This internal operation should be well visible when using assembly
programming.
Before using the stdio functions, ensure that the stdio header file has been
included like so:
...
#include <stdio.h> OR
#include "stdio.h"
...
main()
...
To transmit a group of characters, you may use printf() or putchar for single
character:
#include "stdio.h"
...
main()
{
//lines defining UART's register
//lines defining UART's register
//lines defining UART's register
//line enabling the UART or UART's interrupt
printf("Magandang Umaga");
}
The above code will output "Magandang Umaga" out of the TX pin.
To receive, we can likewise use scanf as below:
#include "stdio.h"
...
main()
{
char buffer[];
//lines defining UART's register
//lines defining UART's register
//lines defining UART's register
//line enabling the UART or UART's interrupt
scanf("%s/r",&buffer);
}
The above will wait for a string of data terminated by a CR (/r) and save this
into the buffer array.
If after you are unsure if the UART is functioning properly after initial setup
and programming, please refer to another article that has some detailed
proceedure on testing the UART. Please refer to tha index page.
From here on, it will all be coding. You should be able to find several
resources on stdio examples on the web which you may apply for the built in
UART. Discussion boards specializing on specific MCU's and Compilers/Assemblers
may be of an abundant source of information.
Contribution by user kulinti
___________________________________________________________________________________________________________________________________________________________________________
Disclaimer: The information contained in this
website is for general information purposes only. The information is provided
by layadcircuits.com or its contributing users, whilst we endeavor to keep the information
up-to-date and correct, we make no representations or warranties of any kind,
express or implied, about the completeness, accuracy, reliability, suitability
or availability with respect to the website or the information, products,
services, or related graphics contained on the website for any purpose. Any
reliance you place on such information is therefore strictly at your own risk.