Skip to main content

Subsection 10.3.4 Part 4: Reading the Command Table

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).
Figure 10.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.

Activity 10.3.2. Build the Three Startup Bytes.

Work these out as binary and then write them as hex. All three are in Figure 10.3.10.

(b)

Display setup is 0b1000 X B1 B0 D. What byte turns the display on and not blinking? What byte would turn it on and blinking at 1 Hz?

(d)

All three are one-byte writes to the device. Which I2C library function sends one, and what are its two arguments?
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.