C12 · Lesson 32 · SPI, shift registers and displays
A fast serial bus with a wire for every job
Where I²C saves wires, SPI spends them for speed and simplicity: a clock, a wire out, a wire in, and a separate chip-select wire for each device. There are no addresses and no acknowledgments; the controller pulls a device's chip select low, clocks bytes out while bytes come back in at the same time, and lets chip select go. SPI runs at several megahertz, and it's how microcontrollers talk to flash memory, displays, SD cards and fast converters. Its simplest partner is a shift register, a chip that turns eight serial bits into eight output pins, here driving a seven-segment digit.
- The program counts from 0 to 9 on a seven-segment digit, one number every 50 ms. Each update sends one byte over SPI (one bit per segment) into a shift register, which shows it when chip select (P13) rises.
- Zoom in on one update: CS falls, SCK pulses eight times while MOSI carries the byte, most significant bit first, then CS rises and the digit changes.
- The digit's patterns are in
SEGMENTS. Change the entry for 8 to 0x80 and watch the 8 become a lone decimal point.
Predict
Over SPI, a controller sends one byte to a device. How many bytes does it receive at the same time?
One: SPI is full duplex. On every clock the controller shifts a bit out on MOSI and shifts a bit in from MISO, so sending n bytes always receives n bytes, though when only writing (as to a shift register) the bytes received are ignored. There are no addresses: chip select decides which device listens.
Name it
The four wires
| Wire | Driven by | Job |
|---|---|---|
| SCK | controller | the clock |
| MOSI | controller | data to the device ("controller out") |
| MISO | device | data back ("controller in") |
| CS | controller | active low: selects one device |
Each device has its own CS. The controller sets CS low, clocks whole bytes (most significant bit first, usually), and sets CS high. In mode 0 (this board's), data is read on SCK's rising edge and changes on its falling edge; the four modes differ in the clock's idle level and which edge samples.
Speed
SCK runs at megahertz rates because nothing waits for an acknowledgment and the lines are actively driven both ways. This board's SPI clock is ; a byte takes 8 clock periods.
Shift registers
A 74HC595-style shift register takes a bit from its data input on each clock edge, shifting the rest along, and copies all eight into its output pins on its latch input's rising edge (here wired to CS). Its serial output can feed a second register: daisy-chained, n registers make an -bit output from three wires, sent as n bytes per update.
Seven segments
Segments a to g (clockwise from the top, then the middle) and the point are bits 0 to 7, so a digit's pattern is a byte: 0 is a, b, c, d, e, f, which is 0x3F.
Worked example
Which segments does 0x5B light, and what digit is it?
Show the solution
- Write it in binary. so bits 0, 1, 3, 4 and 6 are set.
- Map bits to segments. Those are a, b, d, e and g: the top, upper right, bottom, lower left and middle.
- Read the digit. That's a 2.
Answer segments a, b, d, e and g, which make a 2.
Worked example
Four daisy-chained shift registers are updated over SPI at 2 MHz. How long does one update take?
Show the solution
- Count the bits. Four bytes is 32 bits.
- Time on the wire. Each bit takes 0.5 µs at 2 MHz: plus the CS edges.
- Compare with I²C. The same 32 outputs over I²C at 400 kHz would take over 100 µs.
Answer about 16 µs, plus the CS edges.
Worked example
A 24-bit ADC is read over SPI at 4 MHz. How long does a reading take on the wire?
Show the solution
- Time on the wire. 24 clocks at 250 ns:
- Why SPI is fast. SPI has no addressing or acknowledge bits, so its time is just the bits, which makes it the fast choice for converters and displays.
Answer 6 µs.
Check
This board's SPI clock is . What's the SPI clock with CLKDIV = 2?
Write units the way you normally would: 1.5 MHz, 1500 kHz and 0.0015 GHz are the same answer.
Check
How long does it take to clock 4 bytes over SPI at 1 MHz?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
Segments a to g are bits 0 to 6. Light exactly the segments that show the digit 3. What byte is that, in hex?
Check
How many output pins do 2 daisy-chained 8-bit shift registers give?
Check
SPI sends 5B most significant bit first. What level is the 1th bit on MOSI?
Check
Three SPI devices share SCK, MOSI and MISO. How does the controller choose which one it's talking to?
Check
Show the digit D (4) on the seven-segment display: CS is P13 (active low, latching on its rise), and the byte goes over SPI.
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
Eight LEDs hang off the shift register (bit 0 is the bottom LED). Light the bottom 3 of them, as a level meter would.
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
An SPI transfer of 16 bits runs at 1 MHz.
(a)
How long does it take?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
(b)
How many transfers a second could it make back to back?
Check
4 daisy-chained 8-bit shift registers are updated over SPI at 1 MHz.
(a)
How long does one update take?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
(b)
How many clock pulses is that?
Explain why
Why is SPI usually much faster than I²C?
I²C's lines are open-drain, pulled up by resistors, so every rising edge is a slow RC charge, which limits the clock to hundreds of kilohertz; and every byte carries a ninth clock for the acknowledgment, plus addresses and start and stop conditions. SPI's lines are driven actively both high and low, so edges are fast and the clock can run at many megahertz; there are no acknowledgments or addresses, since a dedicated chip-select wire picks the device; and it's full duplex, moving a byte each way every eight clocks. The price is wires: four, plus one more chip select per extra device.
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.