In this article, I will discuss Fundamentals of UART Communication. I plan to create a series of articles on UART/USART and implement the source code for different microcontrollers, e.g., 8051, pic, stm32, avr, and Arduino.
So let’s start this article with an understanding of UART…
What is a UART?
UART stands for Universal Asynchronous Receiver/Transmitter, a hardware communication protocol commonly used for serial communication between two devices. Both devices must use the same baud rate, which defines the speed of data transmission.
UART is one of the most popular serial communication protocols that mainly relies on two wires: one for transmitting (TX) and one for receiving (RX), along with a shared ground connection.
UART defines a set of rules for this exchange, making it simple and reliable for device-to-device communication. For more advanced systems, USART (Universal Synchronous-Asynchronous Receiver/Transmitter) builds on UART’s features.
USART supports both asynchronous communication (like UART) and synchronous communication, which uses a shared clock. It also handles protocols such as LIN (Local Interconnect Network), smartcard protocols, and IrDA (Infrared Data Association). These enhancements make USART suitable for more complex embedded applications.
UART/USART supports three modes of communication: simplex (data flows in one direction), half-duplex (data flows in both directions but only one side communicates at a time), and full-duplex (both sides transmit simultaneously).
In UART/USART devices exchange data in the form of frames. Each frame contains bits sent sequentially, and the receiving device reassembles them into complete bytes.
UART frame format:
In the UART/USART communication frame data bits are embedded between the start and stop bits including the optional parity bits.
Let’s understand the different parts of the communication frame of UART/USART.
Start Bit:
Data transmission begins with the start bit. UART data transmission line is normally held at a high voltage level when idle (it’s not transmitting data).
To start the transmission of data, the transmitting UART pulls the transmission line from high to low for one lock cycle. When the receiving UART detects the high-to-low voltage transition, it begins reading the bits in the data frame at the frequency of the baud rate.
Data Bits (Data Frame):
The data frame contains the actual data being transferred. It can be configurable to 5, 6, 7, 8, or 9 bits per frame, depending on the system’s settings. It also supports the programmable data order with MSB-first or LSB-first shifting but in most cases, the data is sent with the least significant bit first.
Parity:
Parity describes the evenness or oddness of a number. It is an error-checking method that adds an extra bit, called the parity bit, to the data. The parity bit helps verify the integrity of the transmitted information.
This method helps us to find out the errors introduced by electromagnetic interference, mismatched baud rates, or long-distance communication. But this method also has some limitations which will be discussed in the separate blog post.
Now let’s understand how the parity mechanism works:
Enable parity:
First, you need to enable the parity setting even parity or odd parity.
Now parity works according to the parity setting even or odd.
Setting the Parity Bit:
Before sending a data frame, the transmitting UART calculates the parity bit based on the “1” bits in the data.
- For even parity, the UART sets the parity bit to
0
if the total number of “1” bits is even. Otherwise, it sets the parity bit to1
make the total even. - For odd parity, the UART sets the parity bit to
0
if the total number of “1” bits is odd. Otherwise, it sets the parity bit to1
make the total odd.
Receiving and Verifying:
- After reading the data frame, the receiving UART counts the “1” bits in the data and includes the parity bit in its calculation.
- It compares the total against the expected parity (even or odd) based on the parity configuration.
If the parity check fails, the corresponding flag is set, and an interrupt is generated if the interrupt is available and enabled.
Let’s see an example for a better understanding.
Example: If the transmitted data is 01010101 (4 “1” bits):
- Even Parity: The UART sets the parity bit to 0.
- Odd Parity: The UART sets the parity bit to 1.
Stop Bit:
The stop bit denotes the end of the data transmission. The sending UART drives the data transmission line from a low voltage to a high voltage for 0.5, 1, 1.5, or 2-bit (s) duration. It is configurable and depends on the requirement.
Steps of UART Transmission:
Initially, when no data is being transmitted, the UART line remains in the idle state, represented by a logic HIGH level. Both the transmitter and receiver are in a ready state, waiting for data to transmit or receive.
Now let’s understand the basic sequence of data transmission and reception in UART/USART.
Step:1- Data Reception by Transmitting UART
The transmitting UART receives a byte of data from the data bus in parallel format, typically 8 bits.
Step:2-Framing the Data Frame:
The transmitting UART creates a data frame by adding:
- Start Bit: Mark the beginning of transmission.
- Parity Bit (optional): Used for error detection.
- Stop Bit(s): Marks the end of the data frame.
Step:3- Serial Data Transmission:
The complete data frame (start bit, data bits, parity bit, and stop bits) is transmitted serially over the UART data line to the receiving UART using a shift register. The configured baud rate governs the transmission speed.
Step:4- Frame Decoding by Receiving UART:
The receiving UART removes the start bit, parity bit, and stop bit(s) from the received data frame to extract the original payload. You must ensure that both the transmitting and receiving UARTs are configured with the same baud rate otherwise you will not receive the expected data.
Step:5- Data Conversion:
The receiving UART converts received serial data back into parallel format with the help of the shift register and sends it to the data bus on the receiving end for further processing.
Example:
Suppose we want to send the capital letter “P” using UART communication in the 7-bit ASCII format. The character “P” is represented by the binary value 1010000 (or 0x50 in hexadecimal).
Below is the UART configuration:
- Word Length: 8 bits
- Parity Bit: None
- Data Order: LSB-first
- Stop Bits: One
Now the following process is done by the transmitting and receiving UART.
- The original Bit order of the ASCII binary value for “P” is 1010000. This is the standard representation, with the most significant bit (MSB) on the left.
- Since the UART is configured for LSB-first, the bit order of “P” is reversed before transmission.
Original Order: 1010000
Reversed Order (LSB First): 0 0 0 0 1 0 1 - UART frames the Data before the transmission and adds framing bits:
- A start bit (logic 0) at the beginning to signal the start of the transmission.
- The 7 data bits in LSB-first order: 0 0 0 0 1 0 1.
- An optional parity bit (not used in this example).
- A stop bit (logic 1) at the end to signal the end of the frame.
- The UART transmits the bits in the following sequence:Start bit (0) → 0 0 0 0 1 0 1 (data bits in LSB-first order) → Stop bit (1).
- After the stop bit, the communication line returns to the idle state (logic 1), waiting for the next data frame.
Advantages and Disadvantages of UARTs
Each communication protocol has some pros and cons which means no communication protocol is perfect. But UARTs offer a reliable and straightforward solution for many applications. Here are some advantages and disadvantages of UARTs.
Advantages of UARTs:-
- Simple Wiring: Requires only two wires (Tx and Rx) for data transmission and reception.
- No Clock Signal Needed: Operates asynchronously, eliminating the need for a separate clock signal.
- Error Checking: Includes an optional parity bit for basic error detection.
- Customizable Data Frames: The structure of the data packet (e.g., word length, parity, stop bits) can be configured, provided both sides are set up identically.
- Proven Technology: Widely documented and extensively used in embedded systems and serial communication.
- Hardware flow control: Support the hardware flow control to avoid data loss but hardware flow control is not universally available in all UART modules.
Disadvantages of UARTs:-
- Limited Frame Size: The data frame is restricted to a maximum of 9 bits.
- No Support for Multi-Master or Multi-Slave Systems: UARTs are designed for point-to-point communication and lack built-in support for more complex network topologies.
- Strict Baud Rate Matching: The baud rates of the transmitting and receiving UARTs must be within 10% of each other to ensure reliable communication.