Skip to main content

Subsection 10.1.3 Part 3a: How Did Two Wires Do That?

Four characters are on your display, and two wires put them there. Next we’ll work out what those two wires carried, and Subsection 10.4.1 has the written version to read afterwards.
Recall the UART, which is another communication protocol that uses 2 wires only. The UART sends a byte down a wire, and the receiver recovers the bits by starting a timer at the start bit and sampling on a schedule both ends were configured for in advance, the baud rate. Nothing on the wire says where a bit ends, so get that rate wrong at either end and the frame comes out as garbage.
I2C uses up one of the two wires to make that agreement go away (Figure 10.1.8). The controller drives a clock line, SCL, alongside the data line, SDA, and every device on the bus reads SDA while SCL is HIGH. No device has to know in advance how fast the bits will arrive, because the clock wire tells it. That is what synchronous means here: the clock is shared, not reconstructed independently at each receiver.
Figure 10.1.8. UART and I2C compared. On the left, UART: a separate transmit and receive line between exactly two devices, no clock, and a baud rate both ends must be configured for in advance, but each direction has its own wire, so both can be in use at once. On the right, I2C: one clock line (SCL) driven by the controller and one bidirectional data line (SDA), both held HIGH by pull-up resistors, with every device on the bus tapped onto the same two wires. Adding a device to the UART arrangement costs another pair of pins; adding one to the I2C bus costs none.
The other half of the difference is that the two wires are shared. Every I2C device is tapped onto the same SDA and the same SCL, which is what a bus is, and what it buys is that the wire count does not grow: a display, a temperature sensor and an accelerometer all connect to the same two wires. Both lines idle HIGH, held there by pull-up resistors, the ones already on your backpack, and a device can only ever pull a line LOW. To send a 1 it releases the line and lets the resistor bring it up.
Only one device may talk at a time, so I2C is half duplex where UART is full duplex. The whole bus runs at one speed, which has to be a speed every device on it can follow. And because every device hears every bit, each one needs an address, which is what the controller sends first, every single time. One device is in charge of all of this: the controller, our STM32C031C6, which generates the clock and starts every transaction. Everything else is a target, and a target only speaks when spoken to.
Our target is the display’s backpack chip, and its address is HT16K33_ADDR (0x70), and it is the first thing you will find on the wire.
We use 100 kHz, which is Standard mode: essentially every I2C device supports it, which is why it is the safe default. Our chip can go a lot faster: 400 kHz (Fast mode) and 1 MHz (Fast-mode Plus). The devices that need those are the ones moving a lot of data, an EEPROM (a small non-volatile memory chip) being the usual example. A four-digit display sending ten bytes when the number changes is not one of them.