TactivergeLite

Lite / Textbooks / Electrical and Embedded Systems / Chapter 8

8. Talking between chips

Version 1 · Technically checked (automated checks only) · 3 source lessons · Full interactive version

Introduction

Sensors, displays and radios talk to the microcontroller over a handful of wires. UART sends bytes on one wire with a start bit and a stop bit at an agreed speed. I²C shares two wires among many devices, each with an address, and every byte is acknowledged. SPI uses a clock and separate data lines to swap bytes at high speed. Knowing the framing lets you calculate throughput and decode a captured signal by eye.

Figure 8.1. Start, eight data bits, stop (described in words)

A UART frame for the byte 0x41: a start bit 0, eight data bits sent least significant first (10000010), and a stop bit 1. Ten bits at 9600 baud take 1.042 ms, giving 960 bytes per second.

Explanation

Built from 3 School lessons. Each part is the lesson's own explanation.

8.1 UART: one wire, ten bits, one byte

There is no clock wire between your laptop and a GPS module, yet not a single bit gets lost. How does the receiver know when each bit starts?

Asynchronous means no shared clock

A UART line rests high (idle). To send a byte, the transmitter pulls it low for one bit time: the start bit. The receiver sees that falling edge, waits half a bit, then samples once per bit time. Both sides must agree on the baud rate in advance; each keeps its own clock, so they only have to stay in step for one short frame before the next start bit lines them up again.

The frame, bit by bit

After the start bit come 8 data bits, least significant bit first, then an optional parity bit, then a stop bit (high) that returns the line to idle. The common format 8N1 means 8 data bits, no parity, 1 stop bit: 10 bits on the wire per byte. 8E1 adds an even-parity bit (11 bits). The letter A is 0x41 = 01000001, so the data bits go out as 1 0 0 0 0 0 1 0.

Parity: a one-bit alarm

With even parity the transmitter picks the parity bit so the total number of 1s in data + parity is even. 0x41 has two 1s, so its even-parity bit is 0. If noise flips any single bit, the count becomes odd and the receiver flags the frame. Two flipped bits cancel out and slip through, so parity detects errors but cannot correct them; real protocols add checksums on top.

When the text turns to garbage

A baud-rate mismatch makes the receiver sample in the wrong places, so bytes decode as nonsense or framing errors (no high stop bit where one was expected). Other usual suspects: transmit wired to transmit instead of crossing TX→RX, no shared ground, or a 5 V device talking to a 3.3 V input. UART voltage levels are a separate choice from the frame format.

Derivation: From baud rate to bytes per second
  1. One bit lasts T = 1/baud seconds. At 9600 baud, T = 1/9600 s ≈ 104.2 µs.

  2. One 8N1 frame is 1 start + 8 data + 1 stop = 10 bits, so it lasts 10·T ≈ 1.042 ms.

  3. Back to back with no idle gaps, frames per second = 1/(10·T) = baud/10 = 960.

  4. Each frame carries one data byte, so the maximum data rate is 960 bytes/s. In general: bytes/s = baud ÷ (bits per frame).

Common mix-ups

Open the Lite lesson: UART: one wire, ten bits, one byte

8.2 I²C: decoding a bus conversation

Twenty sensors share the same two wires, and none of them talk over each other. Who decides who speaks, and how does a sensor say “got it”?

Two wires, pulled up

I²C has SCL (clock, driven by the controller) and SDA (data). Devices never drive a line high: they can only pull it low or let go (open drain). Pull-up resistors bring released lines back high. So two devices can never fight, one pushing high and one low; at worst both pull low. It also means a 0 is a definite act, which is why the acknowledge is a 0.

Start, address, acknowledge

A transaction begins with START: SDA falls while SCL is high (data normally changes only while SCL is low). Then the controller sends the address byte: 7 address bits, MSB first, plus R/W (0 = write, 1 = read). On the 9th clock it releases SDA; the device with that address pulls it low: ACK. If nobody does, SDA stays high: NACK, meaning no such device (or it is busy).

Reading a register

Most sensors keep values in numbered registers. A register read is: START, address+W, register index (target ACKs both), repeated START, address+R, then the target sends data bytes. Now the controller acknowledges: ACK means “send another”, and a NACK on the last byte means “that's enough”, followed by STOP (SDA rises while SCL is high).

Counting the time

Every byte costs 9 clocks: 8 data bits and the acknowledge. Reading 2 data bytes from a register is 5 bytes on the bus, so 45 clocks: 450 µs at standard mode (100 kHz) or 112.5 µs at fast mode (400 kHz), before counting the START, repeated START and STOP. The addressing overhead is why I²C suits small, occasional reads rather than streaming video.

Derivation: How long a register read takes
  1. List the bytes: address+W, register index, address+R (after the repeated START), then n data bytes: 3 + n bytes.

  2. Each byte is 8 data clocks plus 1 acknowledge clock: 9 clocks per byte, so 9(3 + n) clocks.

  3. One clock lasts 1/f. At 100 kHz that is 10 µs; at 400 kHz, 2.5 µs.

  4. Time ≈ 9(3 + n)/f. For n = 2: 45 clocks → 450 µs at 100 kHz, 112.5 µs at 400 kHz (START/STOP conditions add a little more).

Common mix-ups

Open the Lite lesson: I²C: decoding a bus conversation

8.3 SPI: swapping bytes at full speed

A tiny colour screen needs over a megabyte every second. Which bus can keep up, and why does reading from it mean sending something too?

Four wires, one conversation at a time

SCLK is the clock from the controller. MOSI (controller out, peripheral in) and MISO (controller in, peripheral out) carry data. CS (chip select, active low) picks which peripheral is listening: each peripheral normally gets its own CS line, so four devices need four CS pins. There are no addresses: pulling a CS low is the address.

Two shift registers in a ring

Inside, the controller and the peripheral each hold an 8-bit shift register, joined in a ring by MOSI and MISO. On each clock, each side shifts its most significant bit out and takes the other side's bit in. After 8 clocks the bytes have swapped. That is why SPI is full duplex, and why reading always means writing something.

Modes: when to sample

Devices differ on the clock's idle level (CPOL) and on which edge data is sampled (CPHA), giving modes 0–3. Mode 0 (clock idles low, sample on the rising edge) is the most common. Controller and peripheral must use the same mode, or every bit is read half a clock off. The datasheet always states it; check it before blaming the wiring.

Choosing a bus

UART: 2 wires, point to point, no clock; 115200 baud 8N1 is about 11.5 kB/s. I²C: 2 wires shared by many addressed devices, acknowledged; 400 kHz gives at most about 44 kB/s before addressing overhead. SPI: 3 wires plus a CS per device, no addressing or acknowledge; 10 MHz moves 1.25 MB/s. Choose by data rate, number of devices and spare pins.

Derivation: SPI burst time and data rate
  1. SPI moves one bit per clock edge pair and has no start, stop, parity or acknowledge bits.

  2. So N bytes need exactly 8N clocks (with CS held low and no gaps).

  3. One clock lasts 1/f, so the burst takes t = 8N/f. With f in MHz, t comes out in microseconds.

  4. The maximum data rate is N/t = f/8 bytes per second: 8 MHz → 1 000 000 bytes/s.

Common mix-ups

Open the Lite lesson: SPI: swapping bytes at full speed

Worked examples

UART: one wire, ten bits, one byte: example 1

A logic analyser shows, after the start bit, the data bits 0 0 1 0 1 1 0 0 (first to last). What byte is it?

Solution

The first bit is bit 0. Reverse to read MSB first: 00110100 = 32 + 16 + 4 = 52 = 0x34, the ASCII character '4'.

UART: one wire, ten bits, one byte: example 2

How long does one 8E1 frame take at 115200 baud?

Solution

11 bits × (1/115200 s) = 11 × 8.68 µs ≈ 95.5 µs.

I²C: decoding a bus conversation: example 1

A capture shows the first byte after START as SDA bits 1 1 0 1 0 0 0 1 (MSB first), then SDA low on the 9th clock. Decode it.

Solution

11010001 = 0xD1. The top 7 bits 1101000 = 0x68 are the address; the last bit 1 means read. SDA low on clock 9 is an ACK: a device at 0x68 is present and ready to send.

I²C: decoding a bus conversation: example 2

After sending address 0x3C+W, SDA stays high on the 9th clock. What does it mean and what would you check?

Solution

NACK: no device acknowledged 0x3C. Check the address in the datasheet (some quote the 8-bit form 0x78), the wiring and the pull-up resistors, and that the device is powered.

SPI: swapping bytes at full speed: example 1

How long does it take to send 64 bytes to an SD card at 4 MHz?

Solution

64 × 8 = 512 clocks. 512 / 4 MHz = 128 µs.

SPI: swapping bytes at full speed: example 2

A board has 3 SPI peripherals and 1 I²C sensor. How many microcontroller pins do the buses use?

Solution

SPI: SCLK, MOSI, MISO shared (3) plus one CS each (3) = 6. I²C: SDA and SCL = 2. Total 8 pins.

Interactive figures, in words

Figure 8.2 from UART: one wire, ten bits, one byte

Pick the letter A (65) and look at the waveform: find the start bit and read the data bits from left to right. Then flip one data bit with parity off, then with even parity on. Which setting notices the damage?

The UART diagram draws one frame as a square wave. For the letter A (65 = 0x41) with even parity the line reads, in time order: start 0, data 1 0 0 0 0 0 1 0, parity 0, stop 1. Choosing a byte updates the bits; choosing a data bit to flip inverts it and the readout reports the wrongly decoded byte and whether the parity check fails (it fails for any single flip when parity is on; with no parity the error is not noticed).

Figure 8.3 from I²C: decoding a bus conversation

Leave the address at 0x48 and step through the register read one byte at a time. Watch the 9th bit of each byte: who drives it, and why is the very last one different?

The I²C diagram lists a register read from address 0x48, register 0x00, with sample data 0x1A and 0xC3: byte 1 address+W 0x90 ACK by target; byte 2 register 0x00 ACK by target; byte 3 address+R 0x91 ACK by target; byte 4 data 0x1A ACK by controller; byte 5 data 0xC3 NACK by controller, then STOP. Each step shows the 8 SDA bits most significant first and the 9th acknowledge bit. The readout totals 45 clocks: 450 µs at 100 kHz and 112.5 µs at 400 kHz.

Figure 8.4 from SPI: swapping bytes at full speed

Set the controller to 0x9F and the peripheral to 0x3C. Clock once at a time and watch both registers. After how many clocks have the two bytes completely swapped places?

The SPI diagram shows two 8-bit registers. Starting with the controller holding 0x9F (10011111) and the peripheral holding 0x3C (00111100), each press of Clock once shifts both registers left by one: the controller's top bit goes into the peripheral's bottom bit over MOSI and the peripheral's top bit goes into the controller's bottom bit over MISO. After 8 clocks the controller holds 0x3C and the peripheral holds 0x9F: the bytes have swapped.

The interactive versions are in the full chapter.

Laboratory

These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.

Practice

Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.

  1. What level does a UART transmit line sit at when nothing is being sent?
    Answer it in “UART: one wire, ten bits, one byte”
  2. After the start bit, a UART frame carries the data bits 0 0 1 0 1 1 0 0 (first to last, least significant bit first). What byte value is that, in decimal?
    Answer it in “UART: one wire, ten bits, one byte”
  3. At 9600 baud with 8N1 framing and no gaps, what is the maximum data rate, in bytes per second?
    Answer it in “UART: one wire, ten bits, one byte”
  4. During the acknowledge clock, what does the receiving device do to say ACK?
    Answer it in “I²C: decoding a bus conversation”
  5. In a multi-byte read, the controller answers the final data byte with a NACK. Why?
    Answer it in “I²C: decoding a bus conversation”
  6. In an 8-bit SPI transfer, how many clock cycles does it take for the controller's and peripheral's bytes to swap completely?
    Answer it in “SPI: swapping bytes at full speed”
  7. Which bus suits a colour display that needs about 1 MB/s, on a board with pins to spare?
    Answer it in “SPI: swapping bytes at full speed”
  8. Four SPI peripherals share SCLK, MOSI and MISO, each with its own chip select. How many microcontroller pins does the SPI bus use?
    Answer it in “SPI: swapping bytes at full speed”

Engineering applications

Debug consoles

Almost every embedded board has a UART debug port. Engineers connect it to a laptop to read log messages while the device runs, the quickest way to see what firmware is thinking.

Choosing a bus

A slow temperature sensor and a fast display can share a board: the sensor on I²C, which saves pins, and the display on SPI, which moves pixels fast enough. The choice is a trade between wires and speed.

Watch (optional)

Streaming video uses a lot of data. Everything you need is in the text.

Summary

Glossary

Bus and link bandwidth
Bandwidth is how much data a bus or link can move per second. For a UART each byte usually costs 10 bit times: one start bit, eight data bits and one stop bit. Field Guide
Bits and bytes
A bit is a single binary digit, 0 or 1. A byte is 8 bits. n bits can represent 2^n different values. Field Guide
Hexadecimal
Hexadecimal is base 16, using digits 0–9 and A–F. Each hex digit stands for exactly four bits, so two hex digits describe one byte. Field Guide
Endianness
Endianness is the order in which the bytes of a multi-byte value are stored in memory or sent over a link: little-endian puts the least significant byte first, big-endian the most significant. Field Guide

Project: A bus plan for a sensor board

A board has a microcontroller, a temperature sensor, an accelerometer sampled at 400 Hz, a small display and a debug port. Assign each to a bus, calculate the data rate each needs, and check that every bus has headroom at the speed you choose.

Deliverables

Write it up on paper or in your own notes. Lite never stores what you write.

References

← 7. PWM, state machines and actuators9. Registers, interrupts and integration →