IAE Academy

FOR CORE AND IT JOBS · CHAPTER 6 OF 7

Serial communication: UART, SPI and I2C

This chapter explains how a microcontroller talks to other chips one bit at a time: the UART frame and baud divisor, SPI with its four clock modes, and I2C with its addressing and acknowledge. You need it to wire up sensors, displays and other devices and to choose the right link for each.

Updated 30 Oct 2026

What you will be able to do

You will read a UART frame bit by bit and compute the divisor and error for a given clock. You will pick the SPI clock mode a device needs and time a transfer. You will write out an I2C register read step by step and count its clocks. Then you will choose between the three links for a given job.

Before you start

  1. Chapter 4, Interrupts and program structure

  2. Binary numbers and hexadecimal

  3. Exclusive OR (XOR) of binary digits

  4. Shift registers: a chain of flip-flops that moves one bit along per clock

  5. Pull-up resistors and the RC time constant of a resistor charging a capacitance

  6. Division and rounding to the nearest integer

  7. Percentage error

A parallel link sends n bits at once on n wires plus a clock. Wires drift out of step with each other (skew), so fast, long parallel links are hard, and every wire costs a pin. A serial link sends the same n bits one after another on one wire, so it takes n bit times but needs very few pins. The UART (universal asynchronous receiver-transmitter) is a simple serial link with no clock wire: both ends agree beforehand on the baud rate, the number of bit times per second, and on the frame format.

Part of frame

Start

Data D0 to D7 (0x41, bit 0 first)

Stop

Line level

0

1 0 0 0 0 0 1 0

1

Bit times

1

8

1

The line idles high. The start bit pulls it low, and that falling edge tells the receiver to begin. The data bits follow, least significant bit (bit 0) first, then an optional parity bit, then a high stop bit that returns the line to idle. A typical receiver runs a sampling clock at 16 times the baud rate. It counts 8 ticks from the falling edge to reach the middle of the start bit, then samples every 16 ticks, in the middle of each bit. A UART can raise an interrupt on each received byte; as in Chapter 4, the routine should store the byte and set a volatile flag.

BIT TIME AND BYTE RATE

Holds for a line running at a constant baud rate with frames back to back. is the baud rate in bits per second, the bit time in seconds, , and the best possible bytes per second. Gaps between frames lower it.

EVEN PARITY BIT

For 8 data bits and even parity, makes the total number of ones, data plus , even; odd parity uses the complement of . is exclusive OR and is data bit . 0x41 has two ones, so . An odd number of flipped bits is detected; an even number is not.

DIVISOR FOR A BAUD RATE

Holds for a baud generator that divides the peripheral clock by an integer to make the 16-times sampling clock (a typical design; some parts store ). is in Hz, and in baud.

BAUD ERROR AND WHAT IT MAY BE

For an 8N1 frame (8 data bits, no parity, 1 stop bit). is a rate error as a fraction; is the difference between the two ends' errors. The stop bit is sampled 9.5 bit times after the falling edge, so the sample point drifts bit times. Under half a bit needs ; keep each end under about 2% to leave room for the 1/16-bit uncertainty in finding the edge.

WORKED EXAMPLE

A link runs 8N1 at 9600 baud. How long do 100 bytes take at best?

Solution
  1. bits, because the start and stop bits cost time too.

  2. bytes per second.

  3. s.

Answer: About 0.104 s, so the useful data rate is 960 bytes per second, not 1200.

WORKED EXAMPLE

Choose the divisor for 115200 baud with an 8 MHz clock. Then try an 11.0592 MHz clock.

Solution
  1. With 8 MHz: , so , the nearest integer.

  2. The actual rate is baud, so .

  3. That is above the 5.3% limit even before the other end's error, so the link would fail.

  4. With 11.0592 MHz: exactly, so and .

Answer: 8 MHz gives and 8.5% error, which is unusable. 11.0592 MHz gives and zero error.

WATCH OUT FOR

If a UART link receives garbage, check that each end's baud error is under 2% and both use the same frame format. Also check that each TX goes to the other device's RX and that the grounds are joined.

SPI: a shared clock

SPI (serial peripheral interface) adds a clock wire, so the ends need no baud agreement beyond keeping the clock under the slave's maximum, and the clock can be fast. One master drives SCLK (clock), MOSI (master out, slave in) and chip select CS, usually active low. The slave drives MISO (master in, slave out). Each end holds a shift register. On each clock MOSI moves one bit from the master's register into the slave's and MISO moves one bit back, so the two registers form a loop. After 8 clocks the two bytes have swapped. That is full duplex: every transfer sends and receives at the same time.

In the usual wiring each slave has its own CS pin and shares the other three wires. A slave whose CS is high normally ignores the clock and lets go of MISO (high impedance: it drives neither high nor low), so slaves do not fight over it. The clock idles low or high (CPOL = 0 or 1). The first edge of each clock cycle is the one that leaves the idle level: rising for CPOL = 0, falling for CPOL = 1. Data is sampled on the first edge (CPHA = 0) or the second (CPHA = 1) and changes on the other. With CPHA = 0 the first bit must be on the line before the first edge, so the slave drives it as CS falls. That gives four modes; master and slave must use the same one.

Mode

CPOL

CPHA

Clock idles

Sampled on

Data changes on

0

0

0

low

rising (first)

falling (second)

1

0

1

low

falling (second)

rising (first)

2

1

0

high

falling (first)

rising (second)

3

1

1

high

rising (second)

falling (first)

SPI TRANSFER TIME

Holds when one bit moves per clock cycle with no gaps. is the number of clock cycles, one per bit; a bit sent and a bit received on the same clock count once. is the clock in Hz, in seconds. A 16-bit transfer at 4 MHz takes .

WATCH OUT FOR

If received data is shifted by one bit or garbled, compare the device's idle clock level and sampling edge with the table above. If either differs, the mode is wrong.

I2C (inter-integrated circuit) uses two wires: SDA (data) and SCL (clock). Both lines are open-drain: a device can only pull a line low or let go, and a pull-up resistor to the supply pulls it high. If two devices pull at once nothing is damaged; the line is low if anyone pulls it low. This is what lets many devices share two wires, and what makes acknowledge possible. The pull-up resistance R and the bus capacitance C set the rise time through the time constant RC, so a smaller resistor allows a faster clock but wastes more current while a line is held low.

  1. START: the master pulls SDA low while SCL is high. Otherwise SDA changes only while SCL is low.

  2. The master sends the 7-bit address and the write bit (0 = write, 1 = read), most significant bit first. The slave acknowledges (ACK) by pulling SDA low on the ninth clock; leaving it high is NACK. The receiver of each byte gives this bit.

  3. The master sends the register number ; the slave ACKs.

  4. The master sends a repeated START (a START with no STOP before it), then the address with the read bit; the slave ACKs.

  5. The slave sends the data bytes; the master ACKs each one except the last, which it NACKs to say stop.

  6. STOP: the master lets SDA rise while SCL is high.

CLOCKS FOR AN I2C REGISTER READ

Holds for the sequence above, ignoring START, repeated START, STOP and pauses such as a slave holding SCL low to make the master wait. Every byte uses 9 clocks: 8 bits plus the acknowledge. The three bytes are address with write bit, register number and address with read bit, then data bytes. is in Hz, typically 100 kHz or 400 kHz; in seconds.

WORKED EXAMPLE

A sensor at 7-bit address 0x48 is read at 100 kHz: register 0x00, 2 bytes. What bytes go on the wire first and how long does it take?

Solution
  1. The 7-bit address fills bits 7 to 1 and the read/write bit is bit 0, so the first byte is . The read byte later is 0x91.

  2. Five bytes of 9 clocks each: clocks.

  3. ms.

Answer: The bytes are 0x90, 0x00, 0x91, then 2 data bytes; the read takes about 0.45 ms.

UART

SPI

I2C

Wires

2 plus ground, no clock

4, plus one CS per extra slave

2, shared by all

Speed (typical)

commonly 9600 to 115200 baud, a few Mbaud at most

several MHz to tens of MHz

100 kHz, 400 kHz or 1 MHz

Distance (typical)

metres at logic levels, far more with line drivers

centimetres, on a board

centimetres, on a board

Devices

2, point to point

one master, one CS per slave

112 usable 7-bit addresses (16 of 128 reserved), limited by bus capacitance

WATCH OUT FOR

If an I2C device never acknowledges, check the address form: a datasheet may quote the 8-bit byte (0x90) where your driver expects the 7-bit address (0x48). If it takes 7 bits, shift right by one.

Check yourself

CHECK YOURSELF 1

A UART link uses 8 data bits, even parity and 1 stop bit at 19200 baud. Frames are sent back to back with no gaps. How long, in milliseconds, does it take to send 500 bytes? Give your answer to two decimal places.

Answer and solution

Answer: (accepted range )

  1. The frame has 1 start bit, 8 data bits, 1 parity bit and 1 stop bit, so bits.

  2. 500 bytes need bit times.

  3. s, which is ms.

CHECK YOURSELF 2

A UART baud generator divides a 16 MHz peripheral clock by an integer to make the 16-times sampling clock. You want 115200 baud. Choose as the nearest integer. What is the size of the baud error in percent? Give your answer to two decimal places.

Answer and solution

Answer: (accepted range )

  1. , so .

  2. baud.

  3. , so .

CHECK YOURSELF 3

An SPI slave device needs the clock to idle high and samples MOSI on the first clock edge of each cycle, which is a falling edge. Which SPI mode does the master need?

  1. (A)

    Mode 0 (CPOL = 0, CPHA = 0)

  2. (B)

    Mode 1 (CPOL = 0, CPHA = 1)

  3. (C)

    Mode 2 (CPOL = 1, CPHA = 0)

  4. (D)

    Mode 3 (CPOL = 1, CPHA = 1)

Answer and solution

Answer: (C)

  1. Clock idles high, so CPOL = 1. That leaves modes 2 and 3.

  2. The first edge leaves the idle level, so with CPOL = 1 it is falling. Sampling on the first edge means CPHA = 0.

  3. CPOL = 1 and CPHA = 0 is mode 2.

CHECK YOURSELF 4One or more options are correct

Which of these statements about the I2C register read sequence described in this chapter are correct?

  1. (A)

    A START condition is SDA falling while SCL is high.

  2. (B)

    A slave acknowledges a byte by pulling SDA low during the ninth clock.

  3. (C)

    The master sends a repeated START before it sends the address with the read bit.

  4. (D)

    The master ACKs the last data byte it reads, to tell the slave to stop.

Answer and solution

Answer: (A), (B), (C)

  1. A is correct: START is the master pulling SDA low while SCL is high.

  2. B is correct: ACK is SDA pulled low on the ninth clock.

  3. C is correct: in this sequence, after the register number the master sends a repeated START, then the address with the read bit.

  4. D is wrong: the master NACKs the last byte to say stop. It ACKs every earlier byte.