Subsection10.1.6Part 4: The Numbers Transmitted on the Data Line
Go back to the eight bits you wrote down for the address bits off your own trace. Convert that 8-bit number to hexadecimal. Your program says 0x70 for the I2C address of the chip. Is that what you got?
Any I2C transaction starts with the address of the device with which the controller wants to communicate. Our display’s address is 0x70. Convert this hexadecimal address to a binary eight-bit number and compare to the 8 bits you read off your trace. They are not the same - but can you tell in which way they differ from each other?
The program passed 0x70 and the data line carries something else, so something between the two moved the bits. Where could that have happened, given that the only thing between the #define of the address and the I2C hardware is i2c1_byteWrite()?
Figure10.1.19.The same capture with the address phase read off it. The Start condition is where SDA falls while SCL is still HIGH. The eight bits that follow are 1 1 1 0 0 0 0 and then 0: seven address bits, which are 0x70, and then the bit circled in red, the R/W bit, 0 for a write. The device’s Ack comes on the pulse after that. If you read all eight as a single byte you get 0xE0, which is the number on the wire rather than the number in the program.
The byte on the wire is 0xE0. 0x70 is the seven-bit address of the backpack chip, 0b1110000, and what goes onto SDA is those seven bits followed by one more: the R/W bit, 0 to write to the device and 1 to read from it. If you read all eight as a byte you get 0xE0. Nothing is wrong: the address is in the top seven bits and the bottom bit is the read/write direction bit, as defined by the I2C protocol. i2c1_byteWrite() did the shift on your behalf, which is a line of it we will look at on Thursday.
Figure 10.1.20 is the HT16K33 datasheet’s own drawing of a command transaction, and the address field in it is written 1 1 1 0 A2 A1 A0: four fixed bits and three that are set by solder pads on the board, so that up to eight of these displays can share one bus. Ours has all three at 0, which makes the 7-bit address 0b1110000, 0x70. The next box along is the write bit, and after it the ACK.
Figure10.1.20.The HT16K33 datasheet’s diagram of a command write. The first field, the device address, is 1 1 1 0 A2 A1 A0: the top four bits are fixed by the chip and A2, A1 and A0 are selectable on the board, so eight of these can share a bus. With all three at 0 the 7-bit address is 0b1110000, which is 0x70. The next bit is the direction, 0 for write; then the device’s ACK; then the command byte, D15 down to D8; then a second ACK and the STOP. Note that the address occupies seven bit positions and the direction bit is separate. The datasheet is being explicit about something many are not. The red box is the seven address bits that make up 0x70, and the blue labels mark the START and the STOP.
One word of caution: Datasheets are not consistent about which of the two numbers they print: some give the 7-bit address and some give it already shifted left by one. A 7-bit address is at most 0x7F, so any address quoted above that (0xE0, say) cannot be the 7-bit form and must be the shifted one. Shift it back before you #define it. If you mix up the 7-bit and the 8-bit address, the device never answers, which looks exactly like a wiring fault.
i2c1_byteWrite() and its relatives take the 7-bit address and do the shift themselves, so whatever a datasheet gives you, what you #define is the 7-bit form.
A few notes about a failed transaction: If you use an incorrect address, the NACK you receive is not an error message from the device: it is the absence of an answer, and it looks exactly the same whether the address is wrong, the device has no power, or the SDA wire is not connected. Also note that the controller will keep clocking anyway until it has sent all nine pulses since it doesn’t know until the ninth pulse whether anyone responded. On Thursday we’ll look at this on the oscilloscope to deepen our understanding.
The oscilloscope trace, with the address phase read off it: 1 1 1 0 0 0 0 is the 7-bit address 0x70, and the bit circled in red is the R/W bit, 0 for a write. Then the ACK.
0x70 is a SEVEN-bit address: 1 1 1 0 0 0 0
on the data line: 1 1 1 0 0 0 0 0
\___address___/ R/W: 0 = write, 1 = read
If you read those eight bits as a byte you get 0xE0, not 0x70. Nothing is wrong: the address is in the top seven bits and the bottom bit is the read/write direction bit, as defined by the I2C protocol.
Careful: datasheets are not consistent about which of these two numbers they print. A 7-bit address is at most 0x7F, so any address quoted above that (0xE0, say) cannot be the 7-bit form and must be the shifted one. Shift it back before you #define it.