---
title: A UART transmitter and receiver
heading: 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
eyebrow: D10 · Lesson 11 · A UART
xp: 40
---

::: hook
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.
:::

::: play
- 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.
:::

```sim plot
view: graph
x: t = -1..11
y: v = -0.3..1.3
slider: B = 0..255 step 1 = 75
plot: step(-t) + step(t - 9) + step(t - 1)*step(9 - t - 1e-9)*(floor(B/2^(floor(t) - 1)) - 2*floor(B/2^floor(t))) | samples: 1200 | label: line level (t in bit times)
readout: byte in binary = B | format: bin8
readout: frame time at 115200 baud, µs = 10/115200*1e6 | digits: 4
```

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

- [ ] 115,200
- [ ] 14,400
- [x] 11,520
- [ ] 9,600

::: reveal
Each byte travels in a 10-bit frame: a start bit, 8 data bits and a stop bit. So 115,200/10 = 11,520 bytes per second; counting 8 bits per byte would suggest 14,400, but 20% of the line time goes to framing.
:::
:::

::: nameit
## 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 = f_\text{clk}/\text{baud}$, rounded to an integer, which leaves a rate error of $f_\text{clk}/(N\cdot\text{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.
:::

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

::: solution
1. **Direct divisor.**
   $$\frac{50 \times 10^6}{115\,200} = 434.03$$
   so N = 434.
2. **Baud and error, direct.**
   $$\frac{50 \times 10^6}{434} = 115\,207\ \text{baud}$$
   an error of +0.006%.
3. **Divisor with 16× oversampling.**
   $$\frac{50 \times 10^6}{16 \times 115\,200} = 27.13$$
   rounded to 27.
4. **Baud and error, oversampled.**
   $$\frac{50 \times 10^6}{16 \times 27} = 115\,741\ \text{baud}$$
   +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%).
:::
:::

::: example
## What does the frame for the byte 0x4B look like?

::: solution
1. **Write the byte in binary.**
   $$\text{0x4B} = 0100\,1011$$
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).
:::
:::

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

::: solution
1. **Find the start bit.** The receiver detects the start bit's falling edge to within one sample tick:
   $$\frac{8.68\ \mu\text{s}}{16} = 0.54\ \mu\text{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
given: b = 9600 | 115200 | 921600
answer: 1e6/b
unit: µs
tolerance: 0.5%

## How long is one bit at {b} baud?

::: right
Right. 1/baud = {answer} µs.
:::

::: misses
- [any] Not yet. One over the baud rate.
:::
:::

::: check
given: b = 9600 | 115200 | 921600
answer: b/10
tolerance: 0.5%

## At {b} baud with 8N1 framing, how many bytes per second can a UART carry?

::: right
Right. baud/10 = {answer} bytes per second.
:::

::: misses
- [b/8] Each byte also needs a start and a stop bit: 10 bits per frame.
- [any] Not yet. Ten bit times per byte.
:::
:::

::: check
given: f = 16 | 50 | 100; b = 9600 | 115200
answer: 100*(f*1e6/(round(f*1e6/b)*b) - 1)
tolerance: 2%

## A {f} MHz clock is divided by the nearest whole number to make {b} baud. What's the percentage error in the baud rate?

::: right
Right. N = round($f_\text{clk}$/baud), error = {answer}%.
:::

::: misses
- [any] Not yet. Round the divisor, then compare the actual rate.
:::
:::

::: check
given: n = 8 | 9 | 10
answer: 100*0.5/(n + 1.5)
tolerance: 0.5%

## A frame has a start bit, {n} 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?

::: right
Right. The stop bit's middle is {n} + 1.5 bit times after the start edge, so 0.5/({n} + 1.5) = {answer}%.
:::

::: misses
- [100*0.5/(n + 2)] The last sample is at the middle of the stop bit, not its end.
- [any] Not yet. Half a bit over the time to the last sample.
:::
:::

::: check
kind: parts
given: f = 12 | 50 | 100; b = 9600 | 115200

## A {f} MHz FPGA makes {b} baud with a counter.

::: part
answer: round(f*1e6/b)
tolerance: 0

### What divisor?

::: misses
- [any] Clock ÷ baud, rounded.
:::
:::

::: part
answer: (f*1e6/round(f*1e6/b) - b)/b*100
unit: %
tolerance: 2%

### What is the rate's error?

::: misses
- [any] (actual − wanted)/wanted.
:::
:::
:::

::: check
kind: parts
given: b = 9600 | 115200 | 921600; o = 8 | 16

## A receiver oversamples {b} baud by {o}.

::: part
answer: 1/b*1e6
unit: µs
tolerance: 0.5%

### How long is a bit?

::: misses
- [any] 1/baud.
:::
:::

::: part
answer: 1/b*1e6/o
unit: µs
tolerance: 0.5%

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

::: misses
- [any] Bit time ÷ oversampling.
:::
:::
:::

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

::: model
The receiver doesn't share a clock with the transmitter, so it has to work out where each bit begins from the data itself. The only reliable timing mark is the falling edge at the start of the start bit. Running a counter at sixteen times the baud rate lets the receiver detect that edge to within a sixteenth of a bit, wait half a bit to land in the middle of the start bit, confirm it's genuinely low, and then sample every bit near its middle, as far as possible from the transitions where the value is uncertain. Sampling in the middle leaves the most room for the two clocks to drift apart before a sample slides into a neighboring bit.
:::
:::

::: practice

::: check
given: b = 300 | 9600 | 115200 | 1000000 | 3000000 | 4000000
answer: 1e6/b
unit: µs
tolerance: 0.5%

## Bit time at {b} baud?

::: misses
- [any] 1/baud.
:::
:::

::: check
given: b = 1200 | 2400 | 9600 | 19200 | 57600 | 230400 | 1000000
answer: b/10
tolerance: 0.5%

## 8N1 bytes per second at {b} baud?

::: misses
- [any] baud/10.
:::
:::
:::
