SME
Embedded Systems · Problem type
Baud-rate register and error
Use this for “A 16 MHz microcontroller sets its UART to 115 200 baud. What goes in UBRR, and how far off is the baud rate?”
This is an example. Type your own values to start a new problem.
u
=
fclk (MHz)
16
×
baud (b/s)
−
1
UBRR
=
round(
u
)
baudactual (b/s)
=
fclk (MHz)
16
×
(
+
1)
e
Baud error
worked out (not typed) · %
Formulas
u
=
fclk
16 × baud
−
1
7.6806
=
16,000,000
16 × 115,200
−
1
UBRR
=
round(
u
)
8 = round(7.6806)
baudactual
=
fclk
16 × (UBRR + 1)
111,111
=
16,000,000
16 × (8 + 1)
e
=
100
×
baudactual − baud
baud
−3.5494
=
100
×
111,111 − 115,200
115,200
Formulas are worked in Hz. The step-by-step shows the conversions.
Assumptions
•
In normal speed the UART samples each bit 16 times, so it divides the clock by 16(UBRR + 1).
•
UBRR is a whole number in a 12-bit register, 0 to 4095, so the baud rate lands near the one asked for.
•
Both ends still agree on the last bit when the error stays within about 2% either way.
Step-by-step
WE KNOW
fclk = 16 MHz, baud = 115,200 b/s
FIND
exact UBRR (u), register value (UBRR), actual baud (baudactual), baud error (e)
Step 1 · Convert to the formula’s units
The formulas don’t work directly in the units you chose, so convert first (to Hz).
fclk = 16 MHz = 16,000,000 Hz (1 MHz = 10⁶ Hz)
Step 2 · Find exact UBRR (u)
Use u = fclk ÷ (16 × baud) − 1
Solve
baud
=
fclk
16(UBRR + 1)
for
UBRR;
it
seldom
comes
out
whole.
u
=
16,000,000
16 × 115,200
−
1
u
=
16,000,000
1,843,200
−
1
u
=
8.681
−
1
u
=
7.6806
Step 3 · Find register value (UBRR)
Use UBRR = round(u)
The register holds a whole number, so take the nearest one.
round(7.6806) = 8
UBRR = 8
Step 4 · Find actual baud (baudactual)
Use baudactual = fclk ÷ (16 × (UBRR + 1))
The clock divided by 16 for each bit and by UBRR + 1 in the counter.
baudactual
=
16,000,000
16 × (8 + 1)
baudactual
=
16,000,000
16 × 9
baudactual
=
16,000,000
144
baudactual = 111,111 b/s
Step 5 · Find baud error (e)
Use e = 100 × (baudactual − baud) ÷ baud
How far the actual rate is from the wanted one, as a percent of the wanted one.
e
=
100
×
111,111 − 115,200
115,200
e
=
100
×
−4,089
115,200
e
=
−
408,900
115,200
e
=
−3.5494%
Check
Put the numbers back in. Both sides should match.
7.6806
=
16,000,000
16 × 115,200
−
1
✓
8 = round(7.6806) ✓
111,111
=
16,000,000
16 × (8 + 1)
✓
−3.5494
=
100
×
111,111 − 115,200
115,200
✓
Topic check
See what you know: 5 questions on this topic
New questions each time. Pass to mark it complete.
Related lessons
Serial protocols
Main lesson
SPI throughput
Use this for “An SPI bus clocks at 8 MHz. How many bytes a second can it move, and how long does a 16-bit transfer take?”
I²C transaction time
Use this for “How long does a 400 kHz I²C write of 2 data bytes take, address byte included?”
UART, SPI or I²C?
Use this for “A bus has two shared wires, pull-up resistors and a 7-bit address for each device. Is it UART, SPI or I²C?”