Skip to main content

Chapter 10 I2C: A Two-Wire Bus, and a Display Driver

The goal of this chapter is to communicate with and control a four-digit seven-segment display via our microcontroller. A four-digit seven-segment display has 34 LEDs in it. Wiring one pin to each is not an option on a board whose Arduino headers carry 22 pins in total. The answer is to use a communication protocol that uses fewer wires and is still able to address all of the LEDs. A common such protocol is I2C (Inter-Integrated Circuit, also written I2C and also called TWI, the Two-Wire Interface), and it is how a microcontroller talks to most of the small parts around it. This protocol uses only two wires, shared by every device on them, with each device answering to an address.
This chapter covers two class days. We’ll use the display first and build its driver afterwards. We’ll start by wiring the display and flashing a short program to the microcontroller that’ll show some characters on the display. We’ll then use the AD2 oscilloscope to analyze the signals those two wires carried in order to understand the I2C protocol. Section Section 10.4 contains details on the I2C protocol.
The second day is dedicated to understanding how the seven-segment display, and in particular the controller chip, the HT16K33, that comes with it, takes commands. We’ll look at the controller’s datasheet, which shows both a command table and a RAM map; we’ll also look at the STM32C031C6’s I2C1 peripheral and the two pins through which it reaches the wires. You’ll write the device driver that turns those datasheet pages into four functions the rest of your program can call to control the display. It is the same kind of layer we have been handing you all term, except that uart.c and adc.c are interface drivers (as is i2c.c) and this one sits on top of i2c.c. The chapter’s one reading comes between the two days, and covers the display.