D1011 of 13
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

D10 · Lesson 11 · A UART

The asynchronous serial frame of start bit, data bits least significant first and stop bit, a baud-rate generator from a clock divider and its rounding error, a transmitter as a shift register and counter, a receiver that oversamples to find the middle of each bit, and how far the two clocks may disagree

The UART is the oldest and simplest way for two digital systems to talk, and it's still on every microcontroller (C12) because it needs no shared clock: just one wire each way and an agreed speed. That simplicity hides a nice hardware problem. The receiver has to find where each bit begins using only its own clock, so it watches for the start bit's falling edge, waits half a bit, and then samples once per bit period, hoping the two clocks agree well enough to stay near the middle of every bit until the frame ends.

  • One UART frame for a byte value, 8N1: the line idles high, a start bit (0), eight data bits least significant first, and a stop bit (1).
  • Change the byte and watch the data bits change. The receiver samples at the middle of each bit (the dashed marks at half-bit positions).
  • At 115,200 baud each bit lasts 8.68 µs, so a frame takes 86.8 µs.

Predict

A UART runs at 115,200 baud with 8N1 frames. How many bytes per second can it carry at most?

Name it

The frame

The line idles at 1. A start bit (0) marks the beginning; then the data bits, least significant first; an optional parity bit; and one or two stop bits (1). "8N1" means 8 data bits, no parity, one stop bit: 10 bit times per byte.

The baud generator

A counter divides the system clock to make one tick per bit: divisor N=fclk/baud, rounded to an integer, which leaves a rate error of fclk/(N⋅baud)−1.

The transmitter

A shift register loaded with {1'b1, data, 1'b0} (stop, data, start) shifts one bit out per tick, with a 4-bit counter to stop after 10 bits, and a small state machine for idle and busy.

The receiver

The receiver runs a counter at 16 times the baud rate. When it sees the line fall, it waits 8 ticks to reach the middle of the start bit, checks it's still 0 (rejecting glitches), and then samples every 16 ticks, the middle of each data bit, shifting them into a register.

Clock tolerance

Sampling the last (stop) bit happens 9.5 bit times after the start edge. If the two ends' clocks differ by a fraction e, the sample drifts by 9.5e bit times, which must stay under half a bit: e < 0.5/9.5 = 5.3% in total, so typically about ±2% from each side after allowing for edge uncertainty.

Worked example

A 50 MHz FPGA makes 115,200 baud. What divisor and error result, directly and with 16× oversampling?

Show the solution
  1. Direct divisor. 50×106115200=434.03 so N = 434.
  2. Baud and error, direct. 50×106434=115207baud an error of +0.006%.
  3. Divisor with 16× oversampling. 50×10616×115200=27.13 rounded to 27.
  4. Baud and error, oversampled. 50×10616×27=115741baud +0.47%, still comfortable.

Answer directly, N = 434 gives 115,207 baud (+0.006%); with 16× oversampling, a divisor of 27 gives 115,741 baud (+0.47%).

Worked example

What does the frame for the byte 0x4B look like?

Show the solution
  1. Write the byte in binary. 0x4B=01001011
  2. Order the bits. The start bit comes first, then the data least significant bit first, then the stop bit.
  3. The frame. 0 (start), then 1, 1, 0, 1, 0, 0, 1, 0, then 1 (stop).

Answer 0 (start), then 1, 1, 0, 1, 0, 0, 1, 0, then 1 (stop).

Worked example

With 16× oversampling at 115,200 baud, how precisely does the receiver find the middle of each bit?

Show the solution
  1. Find the start bit. The receiver detects the start bit's falling edge to within one sample tick: 8.68μs16=0.54μs
  2. Count to the middle. It counts 8 ticks to the middle, so each sample lands within 1/16 of a bit of center.
  3. Margin. That leaves plenty of margin for the 2% clock error a UART tolerates.

Answer each sample lands within one tick, 0.54 µs or 1/16 of a bit, of the bit's center.

Check

How long is one bit at 115200 baud?

Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.

Check

At 115200 baud with 8N1 framing, how many bytes per second can a UART carry?

Check

A 50 MHz clock is divided by the nearest whole number to make 9600 baud. What's the percentage error in the baud rate?

Check

A frame has a start bit, 9 data and parity bits, and a stop bit sampled at its middle. What total clock mismatch, in percent, keeps that last sample within half a bit?

Check

A 50 MHz FPGA makes 9600 baud with a counter.

(a)

What divisor?

(b)

What is the rate's error?

Check

A receiver oversamples 115200 baud by 8.

(a)

How long is a bit?

Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.

(b)

How precisely does it locate the middle of each bit (one sample tick)?

Explain why

Why does a UART receiver oversample instead of sampling once per bit?

Practice

Keep practicing

8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.