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

C12 · Lesson 28 · Serial communication and the UART

Bytes, one bit at a time, on a single wire

The oldest and most common way for a microcontroller to talk is the UART: one wire out, one wire in, no clock, and both ends agreeing in advance on how fast bits go by. It carries debug messages to a terminal, commands from a computer, and readings from GPS modules, modems and sensors. Each byte travels as a frame: a start bit, eight data bits, and a stop bit. Because there's no shared clock, the receiver times each bit from the start bit's edge, so both ends' baud rates must match to within a few percent.

  • The program sends a greeting to the terminal, then echoes back anything typed. The terminal types "hi" at 2 ms.
  • Zoom in on the first byte of TX: it idles high, drops for the start bit, then sends 'H' (0x48) least significant bit first, then a high stop bit. Each bit lasts 8.7 µs at 115,200 baud.
  • Change BAUD to 9600 and run again: the same text takes twelve times as long, and its frames stretch across the timeline.

Predict

At 115,200 baud with 8 data bits and one stop bit, how many bytes per second can a UART send at most?

Name it

The frame

The line idles at 1. A frame is:

  1. a start bit, 0: its falling edge tells the receiver a byte is coming;
  2. eight data bits, least significant first;
  3. a stop bit, 1, returning the line to idle.

Each bit lasts 1/baud; a frame lasts 10/baud. Common rates: 9600, 115,200 and up to a few megabaud. Some links add a parity bit or a second stop bit.

Matching rates

The receiver samples each bit near its middle, timing from the start bit's edge. By the last bit it's nine and a half bit-times in, so a rate mismatch of about 5% (between the two ends combined) shifts the sample out of its bit. Keep each end within about 2%. This board divides its clock:

BAUDDIV=fclkbaud,error=fclk/BAUDDIV−baudbaud

Using it

Cwait while UART_STATUS & UART_TX_FULL, then write UART_DATA; when UART_STATUS & UART_RX_READY, read UART_DATA
MicroPythonu = UART(0, 115200); u.write('text'), u.any(), u.read(n), u.readline()

The transmitter holds one byte waiting while another shifts out. The receiver holds just one received byte: if another arrives before the program reads it, the first is lost and UART_OVERRUN is set (lesson 29 fixes that with interrupts and a buffer).

Levels

This board's pins use 3.3 V logic ("TTL serial"). A computer connects through a USB-to-serial adapter; the old RS-232 standard uses ±3 to ±15 V and an inverting driver chip.

Worked example

Draw the frame for 'A' (0x41).

Show the solution
  1. Write it in binary. 0x41=01000001
  2. Send it LSB first. The data bits are 1, 0, 0, 0, 0, 0, 1, 0.
  3. Frame it. On the wire: idle 1, start 0, then 1 0 0 0 0 0 1 0, then stop 1.
  4. Time it. That's ten bit-times of 8.68 µs each at 115,200 baud.

Answer idle 1, start 0, data 1 0 0 0 0 0 1 0, stop 1: ten bit-times of 8.68 µs.

Worked example

What BAUDDIV gives 115,200 baud, and how far off is it?

Show the solution
  1. Divide the clock by the baud rate. 107115200=86.8 so BAUDDIV = 87.
  2. The rate it gives. 10787=114943baud
  3. How far off. That's 0.22% slow, comfortably inside the 2% budget.

Answer BAUDDIV = 87, which gives 114,943 baud, 0.22% slow.

Worked example

How many bytes per second does a 115,200-baud link carry with 8N1 framing, and with 8N2 (two stop bits)?

Show the solution
  1. 8N1. Each byte is 10 bit-times: 11520010=11520bytes/s
  2. 8N2. Each byte is 11: 11520011=10473bytes/s 9% less.
  3. When it's worth it. The extra stop bit only helps receivers that need more time between bytes.

Answer 11,520 bytes/s with 8N1 and 10,473 bytes/s with 8N2.

Check

How long is one bit at 57600 baud?

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

Check

How long does it take to send 1 bytes at 9600 baud, back to back?

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

Check

What BAUDDIV comes closest to 38400 baud on the 10 MHz board?

Check

With BAUDDIV rounded, how far off 230400 baud is the board, in percent (negative if slow)?

Check

The byte 4B is sent. Write its eight data bits in the order they appear on the wire, first bit on the left, as a binary number.

Check

What's the most bytes per second a UART can send at 4800 baud, with 8 data bits and one stop bit?

Check

A terminal shows garbled characters from a board. What's the most likely cause?

Check

Send the text N= followed by the number N (307) in decimal and a newline, at 115,200 baud. (Convert the number to digits yourself.)

C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.

Check

Wait for a line from the terminal and send it back in capitals.

MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.

Check

The UART runs at 107/BAUDDIV baud; 57600 baud is wanted.

(a)

What BAUDDIV?

(b)

How far off is the actual rate?

Check

A UART at 115200 baud uses 8 data bits, no parity and 1 stop bit(s).

(a)

How long does one byte take?

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

(b)

How many bytes per second?

Explain why

Why does a UART frame need a start bit and a stop bit when the data is only eight bits?

Practice

Keep practicing

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