Subsection10.3.5Part 5: The Display RAM, and the Byte That Is Always Zero
Commands configure the chip. Getting a pattern onto the display means writing its display RAM, and we’ll read that RAM’s layout off the HT16K33 datasheet slowly, because everything in the rest of the session is built on it (Figure 10.3.14). Before that, Figure 10.3.13 is the piece that connects the display we took apart in the last part to the bytes we are about to send: which of the chip’s outputs each digit and each segment is wired to.
One warning about the names first, because it is genuinely confusing. The datasheet calls the chip’s outputs COM0–COM7 and ROW0–ROW15, and it uses those names because the HT16K33 is built to drive a rectangular LED matrix, where the ROW lines really are the rows of a grid. On a seven-segment display they are nothing of the kind: ROW0 through ROW7 are the segments a through g and the decimal point, arranged around a figure eight and not in a row of anything. The name describes the chip, not our display. It will keep coming back (in the RAM map below, and in the dimming command, which sets what the datasheet calls the pulse width of ROW), so whenever you read ROW on this chip, read segment.
Figure10.3.13.How the display’s fourteen pins reach the HT16K33, and what that means for the buffer you fill in. Every LED sits where one COM line crosses one ROW line: the COM line is which digit, and the ROW line is which segment. Our display uses COM0 through COM4 (digit 1, digit 2, the colon, digit 3, digit 4) and ROW0 through ROW7, which are a through g and the decimal point. So bit \(k\) of the byte at display RAM address \(2n\) is the LED where COM\(n\) crosses ROW\(k\text{.}\) The colon is the one line that is not a full digit: its two dots always light together, so they share a single anode, and that anode is tied to one of the eight ROW lines the backpack drives. Which one is not in the datasheet, and finding it is the experiment in Activity 10.3.4, so the eight crossings on that line are all drawn the same here.
Figure10.3.14.The HT16K33’s display RAM, from its datasheet: sixteen bytes, two per common line. The left column names the common line. The chip drives eight of them, COM0 to COM7, of which our display uses the first five: COM0 for the first digit, COM1 for the second digit, COM2 for the colon, COM3 for the third and COM4 for the fourth digit. The two hexadecimal addresses beside each COM entry hold that line’s sixteen possible LEDs, ROW0 to ROW7 in the first byte and ROW8 to ROW15 in the second. The lower table gives the bit order within a byte: the upper row is the even address, D7 down to D0 mapping to ROW7 down to ROW0, and the lower one is the odd address, mapping to ROW15 down to ROW8. The arrows mark which COM line is which position on the display, and the box around COM0’s pair is digit 1’s two bytes. A seven-segment digit uses only the first eight rows, so for us the second byte of every pair is always 0, and so only the even addresses 0x00, 0x02, 0x04, 0x06 and 0x08 are the five that carry anything. The segment map below is the same one you used yesterday, with g highlighted to match the boxed 6 in the bit-order table: bit 6 of a digit’s byte is segment g.
Two bytes per digit, then, and the second one is always zero for us: the HT16K33 can drive sixteen LEDs per common line and our digits use eight. The first byte is the segments, in the same order you used yesterday, 0b DP g f e d c b a, most significant bit first. For example, to light a 0 you’d put 0b00111111 in the D7-D0 byte.
Five pairs, at addresses 0, 2, 4, 6 and 8: digit 1, digit 2, the colon, digit 3, digit 4. The colon sits in the middle because that is where it sits on the display.
Every segment of every digit is 0b11111111 in bytes 0, 2, 6 and 8; every other byte, including the colon’s pair at 4 and 5, is 0. To produce 0 1 2 3 on the display you put 0b00111111, 0b00000110, 0b01011011, 0b01001111 in the same four places, worth deriving once by hand, after which you will use the numbertable[] array in the header, which holds the sixteen patterns for 0 through F. A decimal point after the second digit means setting bit 7 of display_buffer[2].
That leaves two questions. The first is what lights the colon? We know it is the pair at address 4, because that is COM2, but which bit within that byte is not on the datasheet pages we have. It is worth two minutes of experiment, and tonight’s clock needs the answer.
Work on a copy of your own helloDisplay.c. Its display_buffer already has the colon’s pair in it, the two entries at index 4 and 5, both currently 0.
Before you flash anything: the colon has two dots and they always light together, so the display brings them out on one anode pin. That pin has to be tied to one of the eight the backpack drives. Which of a through DP would you bet on, and why?
Now find it. Set display_buffer[4] to 0b00000001, flash, and look; then 0b00000010, then 0b00000100, and so on up. Which bit lights the colon? Is there a more efficient way to go about this experiment (remember bisection)?
The answer to the second question lies in the structure of the display RAM of the HT16K33 and how it is addressed. To write to the display RAM you use a pointer that holds the address of the RAM to which you’d like to write. When we write the entire display, we set the pointer to the start of the display RAM, and each write operation that is part of the same I2C transaction automatically advances the pointer to the next byte. This is what the datasheet means by the display data address pointer, and it is why writing ten bytes from address 0 fills addresses 0 through 9. The zeros are not decoration; they are what moves the pointer past the odd addresses so that the next real byte lands on the next digit. Leave them out and the second digit’s pattern goes to address 1, which is the top (unused) half of digit 1.
All ten bytes go in one transaction (Figure 10.3.15), and they have to: a STOP ends the transaction, and after a STOP the next write starts over with a new address. The library call for “address the device, say where inside it, and send this many bytes” is i2c1_memWrite(), the multi-byte function you have already called without knowing what it did, and this is what it was for.
Figure10.3.15.Writing the whole display in one transaction: the HT16K33 datasheet calls this a page write, on page 22. After the START and the address comes one command byte, and because the display data address pointer command is 0 0 0 0 A3 A2 A1 A0, a command byte of 0x00 means “start at display RAM address 0”. Every data byte after it is stored where the pointer is and moves the pointer on by one, so ten data bytes fill addresses 0 through 9. Each byte is acknowledged, and one STOP ends the whole transaction. It is the same transaction as the single command in Figure 10.1.20, annotated the same way, with the data bytes added after the command byte.
Every LED sits where one COM line crosses one ROW line: the COM line is which digit, and the ROW line is which segment. So bit k of the byte at display RAM address 2n is the LED where COMn crosses ROWk. The chip says ROW because it is built to drive a rectangular matrix; on our display ROW means segment, and they are arranged around a figure eight rather than in a row of anything.
The display RAM holds sixteen bytes, two per common line: ROW0–ROW7 in the first and ROW8–ROW15 in the second. A seven-segment digit uses only the first eight bits (rows) of each common line, so the second byte of every pair is always 0 for us.
Within a digit’s byte, the LED segments are listed in the same order you used yesterday: 0b DP g f e d c b a. To light a 0 you put 0b00111111 in the D7–D0 byte.
A remaining question is: why send the zeros at all, if the second byte of every pair does nothing? To write to the display RAM you use a pointer that holds the address of the RAM to which you’d like to write. We set it to the start of the display RAM, and each write operation that is part of the same I2C transaction automatically advances the pointer to the next byte.
So sending ten bytes to the display to be written to the display memory starting from address 0 fills display memory addresses 0 through 9. The zeros between the digit bytes are what moves the pointer past the odd addresses so that the next real byte lands on the next digit. If you leave the zeros out then the second digit’s pattern goes to address 1, which is the top (unused) half of digit 1 and won’t display.
All ten bytes need to be written in one I2C transaction since a STOP ends the transaction, and after a STOP the next write starts over with a new address in display memory.
The I2C library call for “address the device, specify a memory location inside the device, and the number of bytes to be sent” is i2c1_memWrite(), the multi-byte function you have already used.
The transaction consists of START, 0x70, write, ACK, then the one command byte that says start at address 0, then ten data bytes that are each acknowledged, and finally one STOP.