This document discusses the Universal Asynchronous Receiver/Transmitter (UART) communication protocol. It explains that UART is an asynchronous serial communication protocol commonly used for communication between microcontrollers and peripheral devices. It operates by framing data bits with start and stop bits across two or more wires. The document covers UART fundamentals like frame formatting, transmission and reception of data, baud rate calculation, and differences between UART and Universal Synchronous/Asynchronous Receiver/Transmitter (USART). It also provides block diagrams of the UART module and examples of configuring baud rates on microcontrollers.
14. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
UART - Universal Asynchronous serial Receiver and
Transmitter
Full Duplex mode.(two wires)
Asynchronous == No Clock
Peer to peer
Used to:
Make a communication channel between our micro-controller and our
computer
talk to some sensors/chips, the most used ones are serial backpacks for lcd’s
and graphical lcd’s and GPS modules that use almost always an serial
interface.
The serial protocol is a fairly old protocol created many years ago it was used
by terminals
It is Most used with Embedded Linux to show the Linux Kernel log until the
login by the UART
14
15. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
THE UART INTERFACE 15
Compared to I2C, using the UART is darn-easy. UART stands for Universal Asynchronous
Receive/Transmit. The hardware can also run in synchronous mode, so it is often called a
USART. A good article about the hardware is at avrbeginners.net. And a good programming
reference is Dean Camera’s UART article at fourwalledcubicle.com.
As opposed to SPI and I2C, which are often used for binary data exchange between hardware
devices, UART is often used for transmission of (slower) ASCII data. For example, you might
use the UART for keyboard input or monitor/character LCD output. Speedy SPI transfers data
to dedicated hardware devices at MHz speeds, while UART transfers are a thousand times
slower.
16. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Framing the data
Data chunk
The real meat of every serial packet is the data it carries.
The amount of data in each packet can be set to anything from 5 to 9 bits.
Certainly, the standard data size is your basic 8-bit byte
Synchronization bits
stop bit(s). these bits mark the beginning and end of a packet. There’s always only
one start bit, but the number of stop bits is configurable to either one or two .
The start bit marks the beginning of a new word, When detected, the receiver
synchronizes with the new data stream
16
18. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
UART “Asynchronous ” frame 18
Each data frame consists of
a start bit, a variable number
of data bits, an optional
parity bit, and 1 or 2 stop
bits. The most common
configuration is 1 start bit, 8
data bits, no parity bit, and 1
stop bit (“8N1”).
In asynchronous mode, there is no clock line:
data is transmitted on the transmit line (Tx) and
received on the receive line (Rx).
The UART is initialized by configuring control registers that
determine the baud rate, parity, number of stop bits:
19. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Why use a UART?
A UART may be used when:
– High speed is not required
– A cheap communication line between two devices is required
Asynchronous serial communication is very cheap
– Requires a transmitter and/or receiver
– Single wire for each direction (plus ground wire)
– Relatively simple hardware
– Asynchronous because the
PC devices such as mice and modems used to often be asynchronous serial
devices
19
31. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
USART Universal Synchronous/Asynchronous Receiver/Transmitter
The first difference between a USART and a UART is the way in which the serial data may be
clocked.
A UART generates its data clock internally to the microcontroller and synchronizes that clock with
the data stream by using the start bit transition. There is no incoming clock signal that is
associated with the data,
so in order to properly receive the data stream the receiver needs to know ahead of time what the
baud rate should be.
31
A USART, on the other hand, can be set up to run in synchronous mode. In this mode the sending
peripheral will generate a clock that the receiving peripheral can recover from the data stream
without knowing the baud rate ahead of time. Alternatively, the link will use a completely separate
line to carry the clock signal. The use of the external clock allows the data rate of the USART to be
much higher than that of a standard UART, reaching up to rates of 4 Mbps.
39. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
USART Block Diagram
39
The Receiver is the most complex part of the USART module due to its clock and data recovery
units. The recovery units are used for asynchronous data reception. Inaddition to the recovery
units, the Receiver includes a parity checker, control logic, a Shift Register and a
two level receive buffer (UDR). The Receiver supports the same frame formats as the
Transmitter, and can detect Frame Error, Data OverRun and Parity Errors.
55. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Registers
55
In the AVR microcontroller five registers are associated with the
USART. They are
UBRR (USART Baud Rate Register)
UDR (USART Data Register)
UCSRA (USART Control and Status Register A)
UCSRB (USART Control and Status Register B)
UCSRC (USART Control and Status Register C)
UBRR (12-bit USART Baud Rate Register)
The USART Baud Rate Register (UBRR) is 12-bit (0-4095)
register. This register is used to set baud rate of USART.
UDR (8-bit USART Data Register)
UDR is used to send or receive data.
58. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
UCSRA – USART Control and Status Register A 58
Bit No. D7 D6 D5 D4 D3 D2 D1 D0 Register Value
Bit Name RXC TXC UDRE FE DOR PE U2X MPCM In DEC In HEX
Value 0 0 0 0 0 0 0 0 00 00
RXC
D7 USART Receive Complete
TXC
D6 USART Transmit Complete
0 Receive buffer is empty 0
Receive buffer is empty so transmit complete
interrupt is executed
1 There is new data in the receive buffer 1
Entire frame in the transmit shift register has
been transmitted
UDRE
D5 USART Data Register Empty
FE
D4 Frame Error
0 Wait, do not write to UDR 0 No frame error
1 Tramsmit data buffer is empty 1 Frame error has occurred
DOR
D3 Data Over Run
PE
D2 Parity Error
0 No Data Over Run 0 No Paroty Error
1 Data over run is detected 1 Parity Error is detected
U2X
D1 Double the USART Transmission Speed
MPCM
D0 Multi-processor Communication Mode
0 Single Baud Rate 0 Disable Multi-processor Communication Mode
1 Double Date Transfer rate for Asynchronous Communication 1 Enable Multi-processor Communication Mode
UCSRA (USART Control and Status Register A)
60. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
UCSRB – USART Control and Status Register B
60
Bit No. D7 D6 D5 D4 D3 D2 D1 D0 Register Value
Bit Name RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8 In DEC In HEX
Value 0 0 0 1 1 0 0 0 24 18
RXCIE
D7 Receive Complete Interrupt Enable
TXCIE
D6 Transmit Complete Interrupt Enable
0 This Interrupt is disabled 0 This Interrupt is disabled
1 This interrupt is enabled 1 This interrupt is enabled
UDRIE
D5 Data Register Empty Interrupt Enable
RXEN
D4 Receive Enable
0 This Interrupt is disabled 0 USART receiver is disabled
1 This interrupt is enabled 1 Enable the USART receiver
TXEN
D3 Transmit Enable
UCSZ2
D2 Character Size
0 USART transmitter is disabled 0
This bit combined with the UCSZ1:0 bits in UCSRC
sets the number
1 Enable the USART transmitter 1 of data bits (character size) in a frame.
RXB8
D1 Receive data bit 8
TXB8
D0 Transmit data bit 8
0
This is the ninth data bit of the received character when
using serial
0
This is the ninth data bit of the transmitted character
when using
1 frames with nine data bits. 1 serial frames with nine data bits.
UCSRB (USART Control and Status Register B)
62. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
UCSRC – USART Control and
Status Register C 62
Bit No. D7 D6 D5 D4 D3 D2 D1 D0 Register Value
Bit Name URSEL UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL In DEC In HEX
Value 0 0 0 0 0 1 1 0 06 6
URSEL D7
Register Select UBRRH or UCSRC because both
have same I/O address
UMSEL D6 USART Mode Select
0 UBRRH Register is Selected 0 Asynchronous operation
1 UCSRC register is selected 1 Synchronous operation
UPM1:0 D5 D4 Parity Mode UCSZ1:0 D2 D1 Character Size
0 0 No Parity 0 0 If UCFZ2=0, Character Size = 5
0 1 Reserved 0 1 If UCFZ2=0, Character Size = 6
1 0 Even Parity 1 0 If UCFZ2=0, Character Size = 7
1 1 Odd Parity 1 1
If UCFZ2=0, Character Size = 8
else Character Size = 9
USBS D3 Stop Bit Select UCPOL D0 Clock Polarity
0 Use one stop bit 0 This bit is used for synchronous mode only
1 Use two stop bits 1
UCSRC (USART Control and Status Register C)
63. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
UBRR (USART Baud Rate Register)
For a given crystal frequency, the value loaded into the UBRR decides the baud rate.
The relation between the value loaded into UBBR and the Fosc (frequency of oscillator
connected to the XTAL1 and XTAL2 pins) is dictated by the following formula:
𝑩𝒂𝒖𝒅 𝑹𝒂𝒕𝒆 =
𝑭 𝑶𝑺𝑪
𝟏𝟔(𝑼𝑩𝑹𝑹+𝟏)
Example: 𝑩𝒂𝒖𝒅 𝑹𝒂𝒕𝒆 =
𝟕𝟑𝟕𝟐𝟖𝟎𝟎
𝟏𝟔(𝟒𝟕+𝟏)
=
𝟕𝟑𝟕𝟐𝟖𝟎𝟎
𝟕𝟔𝟖
= 𝟗𝟔𝟎𝟎
To get the X value for different baud rates we can solve the equation as follows:
𝑼𝑩𝑹𝑹 =
𝑭 𝑶𝑺𝑪
𝟏𝟔×𝑩𝒂𝒖𝒅 𝑹𝒂𝒕𝒆
− 𝟏
Example: 𝑼𝑩𝑹𝑹 =
𝟕𝟑𝟕𝟐𝟖𝟎𝟎
𝟏𝟔×𝟗𝟔𝟎𝟎
− 𝟏 = 𝟒𝟖 − 𝟏 = 𝟒𝟕
The value of UBRR can be from 0 to 4095.
For U2X = 1
Replace 16 with 8
64. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Different Values of UBRR
Fosc = 1.000000 MHz = 1000000Hz Decimal to Hexadecimal Conversion
Baud
Rate
(bps)
U2X = 0 U2X = 1 In DEC = 254 FE In HEX
UBRR % Error UBRR % Error
110 567.18 0.03 1135.36 0.03
300 207.33 0.16 415.67 0.16 Hexadecimal to Decimal Conversion
1,200 51.08 0.16 103.17 0.16 In HEX = FE 254 In DEC
2,400 25.04 0.17 51.08 0.16
4,800 12.02 0.17 25.04 0.17
9,600 5.51 10.21 12.02 0.17 Value of UBRR = 12
19,200 2.26 12.76 5.51 10.21 For U2X = 0 Baud Rate= 4,807.69
38,400 Out of Range 2.26 12.76 For U2X = 1 Baud Rate= 9,615.38
57,600 Out of Range 1.17 17.01
115,200 Out of Range Out of Range
𝑼𝑩𝑹𝑹 =
𝑭 𝑶𝑺𝑪
𝟏𝟔 × 𝑩𝒂𝒖𝒅 𝑹𝒂𝒕𝒆
− 𝟏
𝑩𝒂𝒖𝒅 𝑹𝒂𝒕𝒆 =
𝑭 𝑶𝑺𝑪
𝟏𝟔(𝑼𝑩𝑹𝑹 + 𝟏)
66. https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_systemSteps to Program the AVR to
Transmit the Data Serially:
1. Enable the USART transmitter through UCSRB
2. Use 8 data bits, asynchronous mode, 1 stop bit and no parity through UCSRC
3. Calculate the value to be loaded in UBRR for a desired baud rate generation
4. Write the character into UDR which is to be transmitted serially
5. Monitor UDRE or TXC bits to check if character has been transmitted. You can
also enable interrupts associated with above flags to execute their respective
ISRs upon completion of transmission.
6. To transmit next character, go to step 4
66