What is UART?
UART is a protocol that is used to communicate between microcontrollers
It is typically implemented with the following 3 physical logic levels:
TTL
RS-232
RS-422
RS-485 (Outside the scope of this document, not hard to pick up)
For those who have used Arduino, UART is the protocol that is used to facilitate ‘Serial’ communication
Signals Overview
TX
Transmit signal as viewed from a microcontroller (i.e. signals will be going out of this pin)
RX
Receive signal as viewed from a microcontroller (i.e. signals will be coming into this pin)
RTS
Request to send, either high or low
CTS
Cleared to send data, either high or low
Note, the TX wires of one microcontroller are wired to the RX wires of the other microcontroller. A similar situation occurs on the RTS and CTS wires. Always verify that the TX and RX wires are swapped.
Theory of Operation
Serial protocols can be characterized into 2 types, synchronous and asynchronous. As you may have guessed by the acronym, the asynchronous in UART refers to the fact that the protocol is asynchronous. It is outside the scope of this document to discuss the advantages and disadvantages of each type, it is discussed more fully here: Asynchronous vs Synchronous Protocols
The sample rate of the protocol is referred to as the baud rate. Standard baud rates include 9600, 38400, 115200, etc.
Typically, a data packet can be anywhere from 8-12 bits wide. The elements are typical as follows
Start & stop bits:
As discussed in the Async vs Sync protocol document, the line is pulled low to indicate the start of transmission. A single bit is appended to the start and end of the packet.
Data:
The data can be anywhere from 5-9 bits wide. The exact number varies per application, but typically 8.
Parity bit(s):
A parity bit may be appended to the end of the message to ensure accuracy of the message. A parity bit operates by setting the extra appended bit to either 0 or 1 to preserve the parity of the payload.
Consider a protocol that has a single ‘even’ parity bit that has a message of
10101010
. Since there are 4 high bits (1’s) in the payload, the parity of 1’s is even as 4 is even, therefore the message with the appended parity bit is101010100
.Consider a protocol that has a single ‘even’ parity bit that has a message of
10101011
. Since there are 5 high bits (1’s) in the payload, the parity of 1’s is odd as 5 is odd, therefore the message with the appended parity bit is101010111
to make the parity of 1’s even.
Common mistakes
Ensure that TX and RX are swapped as per above
The baud rate typically needs to be within +- 5% of the original baud rate, otherwise it will be mismatched and you will be unable to read anything.
Always check the tolerances on the clock of your MCU before using it in an async protocol application. If you don’t you may end up falling for baud rate mismatch. Note that typically an internal oscillator won’t cut it.
Add Comment