Now we’ll read the HT16K33’s own command table and build the three command bytes we have been sending to the chip at the start of our program helloDisplay.c. Most I2C devices have registers inside them that control their behavior, and you program them through commands the datasheet defines and which you send via I2C. The HT16K33’s commands are one byte each, sent as the data of an ordinary write, exactly what the three given lines at the top of helloDisplay.c were doing, except that this time the byte will mean something.
The command table is on pages 24–25 of the HT16K33 datasheet. Its layout takes a moment to read: the eight columns D15 down to D8 are the bits of the command byte, most significant first. A column with a fixed 0 or 1 is part of the command’s identity; a column with a name in it (S, D, B1, P3) is an option you fill in; and a column marked X is a don’t-care, which we write as 0. For now we mostly care about the rows named System setup, Display setup and Dimming set (Figure 10.3.10).
Figure10.3.10.The rows of the HT16K33 command table we need, from pages 24–25 of its datasheet. The eight middle columns are the bits of the command byte, D15 down to D8, and the magenta line divides them: to its left the fixed bits that are the command itself, to its right the options you fill in. A name there (S, D, B1, P3) is an option you supply, and an X is a don’t-care that we write as 0. System setup is 0 0 1 0 X X X S, where S is the internal oscillator: 0 leaves it off, 1 turns it on. Display setup is 1 0 0 0 X B1 B0 D, where D turns the display on and B1 B0 choose the blink rate: 00 for steady, then 2 Hz, 1 Hz and 0.5 Hz. Dimming set is 1 1 1 0 P3 P2 P1 P0, the duty cycle (the fraction of each digit’s turn in the sweep for which its segments are actually driven, which the datasheet calls the pulse width of ROW) in sixteenths, where 0000 is 1/16 and 1111 is 16/16. The Def. column on the right is each command with its options at their default. Below, separated because we do not need it until Subsection 10.3.5, is the display data address pointer, which says where a block of display data starts. The chip has more commands than these, but they belong to its other job, scanning a keypad, and we will not be sending any of them.
Take the first one all the way through, because the other two go the same way. The System setup row is 0 0 1 0 X X X S. The four bits to the left of the magenta line are the command itself and never change; the four to the right are the options, and the only one that means anything here is S, the oscillator. Here is a trick for code readability: write each half as its own byte and OR them together. You do not have to invent names for the two halves, because each such byte already has a #define (and therefore a name) in SevenSegPartial.h, the header that comes with the driver.
the HT16K33 command table, datasheet pp. 24-25:
system setup 0 0 1 0 | X X X S the row, and its magenta line
command | options
SevenSegPartial.h, the header you are given:
#define HT16K33_SYSTEM_CMD 0x20 // System setup
#define HT16K33_OSC_ON 0x01 // S = 1, oscillator on
the command 0b0010 0000 = 0x20 HT16K33_SYSTEM_CMD
the option, S = 1 0b0000 0001 = 0x01 HT16K33_OSC_ON
OR -------------------
0b0010 0001 = 0x21
i2c1_byteWrite(HT16K33_ADDR, HT16K33_SYSTEM_CMD | HT16K33_OSC_ON);
That last line is the point of the whole part. The | in it corresponds to the magenta line on the table: the command name supplies the top bits, the option name supplies the bottom bits, and OR merges them into the single byte that goes on the wire. You never write 0x21 anywhere.
The other two are built the same way. Display setup is HT16K33_DISPLAY_CMD (0x80) ORed with HT16K33_DISPLAY_ON (0x01) and HT16K33_BLINK_OFF (0x00), which is 0b10000001, 0x81. Dimming set is HT16K33_BRIGHT_CMD (0xE0) ORed with the four duty bits, and full brightness is all four set: 0b11101111, 0xEF.
Change an option and only the bottom bits move. Blinking at 1 Hz is 0b10000101, 0x85, because 1 Hz is B1 B0 = 1 0, that is HT16K33_BLINK_1HZ in place of HT16K33_BLINK_OFF, and nothing else in the byte changes. Half brightness needs the encoding read carefully, because it counts from one rather than zero (0000 is 1/16 and 1111 is 16/16), so eight sixteenths is P3–P0 = 0111 and the byte is 0xE7. Note that 0xE7 is the byte we have been sending in helloDisplay.c and so the display you have been watching has been running at half brightness the whole time. Each command is sent to the backpack chip via i2c1_byteWrite(HT16K33_ADDR, ...), one function call per command, one transaction on the wire per call (Figure 10.1.20).
Every name used above is a #define in SevenSegPartial.h, and every one of them is one field of the command table, which is why a line of driver code reads the way the datasheet row does: the command, ORed with its options. That is the structure to copy the next time you write one of these: the datasheet’s own layout becomes the header file’s layout.
Sending the three command bytes is the first function of your seven-segment driver. It has to run before the display will show anything at all, and it is three calls long. You write it in Subsection 10.3.10.
system setup 0b0010 X X X S S = oscillator on/off
display setup 0b1000 X B1 B0 D D = display on; B1 B0 = blink rate:
00 steady, 01 2 Hz, 10 1 Hz, 11 0.5 Hz
dimming set 0b1110 P3 P2 P1 P0 duty cycle in sixteenths:
0000 is 1/16, 1111 is 16/16
Below is the system setup command from the command table. The four bits left of the magenta line are the fixed command; the four to the right are the options, and only S, the oscillator, means anything here. Trick for code readability: write each half as its own byte and OR them together. Each such byte already has a #define (and therefore a name) in SevenSegPartial.h.
SevenSegPartial.h, the header you are given:
#define HT16K33_SYSTEM_CMD 0x20 // System setup
#define HT16K33_OSC_ON 0x01 // S = 1, oscillator on
the command 0b0010 0000 = 0x20 HT16K33_SYSTEM_CMD
the option, S = 1 0b0000 0001 = 0x01 HT16K33_OSC_ON
OR -------------------
0b0010 0001 = 0x21
i2c1_byteWrite(HT16K33_ADDR, HT16K33_SYSTEM_CMD | HT16K33_OSC_ON);
The | in that call corresponds to the magenta line: the command name supplies the top bits, the option name supplies the bottom bits, and OR merges them into the one byte that goes on the wire. You never write 0x21 anywhere.
Other commands are built the same way, out of the same header’s #defines.
Names are in SevenSegPartial.h, one name per field:
display setup 0b1000 0000 = 0x80 HT16K33_DISPLAY_CMD
display on 0b0000 0001 = 0x01 HT16K33_DISPLAY_ON
not blinking 0b0000 0000 = 0x00 HT16K33_BLINK_OFF
OR -------------------
0b1000 0001 = 0x81
dimming set 0b1110 0000 = 0xE0 HT16K33_BRIGHT_CMD
full, 16/16 0b0000 1111 = 0x0f P3 P2 P1 P0 all set
OR -------------------
0b1110 1111 = 0xEF
blinking at 1 Hz 0b1000 0101 = 0x85 HT16K33_BLINK_1HZ, B1 B0 = 1 0
half brightness 0b1110 0111 = 0xE7 0111 -> 7 -> (7+1)/16 = 8/16
Watch the dimming duty cycle: it counts from one, so half brightness is 0111, not 1000. 0xE7 is what helloDisplay.c has been sending: the display has been at half brightness all along.
Here is the protocol to send such a command from the STM32C0 to the display; it is the format of the transaction you captured yesterday. The command byte you derived goes into the command-code field. So altogether, the transaction consists of START, the seven address bits that are 0x70, the write bit, the display’s ACK, the one command byte, a second ACK, STOP.