6
\$\begingroup\$

I'm using a particular I2C device, and when it is connected, I need to write data to it by sending data to address 0x30 but then to read from it I need to read from address 0x31 (or by doing 0x30 | 0x01).

Is it normal to have I2C devices with two different addresses like this?

\$\endgroup\$
1
  • \$\begingroup\$ To restate the answers, the address of your device is 0011000 (0x18). It occupies the upper seven bits of the first byte of both read and write commands. \$\endgroup\$
    – Ben Voigt
    Mar 15, 2015 at 19:46

4 Answers 4

13
\$\begingroup\$

The first byte sent on the I²C bus consists of the 7-bit address, and one bit to indicate read/write.

All devices work this way, but strictly speaking, the address is the same.

\$\endgroup\$
6
\$\begingroup\$

What you see is correct. You have found an anomaly in the way we look at the first byte that is sent to the device. Technically, the device address is 7 bits, and those are shifted by 1 to the upper 7 bits. Bit 0 is technically the read/write bit, and is not part of the address. But because we send the whole thing as one byte, we (the programmers) tend to view the whole byte as an address. If you were designing the actual hardware, you would see it differently.

In the end, we sometimes have to guess the context in which the "address" is given. And sometimes, we just have to try it (or read the data sheet) to find out which way it is.

\$\endgroup\$
2
  • \$\begingroup\$ I find the nomenclature a little irksome; I think it would have been clearer to have specified addresses as being even numbers from 0-252, rather than numbers from 0-126. \$\endgroup\$
    – supercat
    Mar 15, 2015 at 20:13
  • \$\begingroup\$ I do (the 8 bits) as I prefer, but I'm always aware not everyone does it that way. And I've seen some heated discussions about "which is right". On the flip side, some of the data sheets don't even recognize the concept of "address". They just talk about settings bits high or low. \$\endgroup\$
    – gbarry
    Mar 15, 2015 at 22:59
1
\$\begingroup\$

In every i2c device data sheet you wil find a section detailing how to address your device in either write or read mode. the address is composed of 7 bits and most often most of these 7 bits are set in factory. How the remainig bits are set depends on how you designed your circuit.

Practical example:

let's imagine that you have a MCP3426 chip on your board, the data sheet is telling us in fig. 5.1 that bits 3,4,5 and 6 are set in the factory and bits 0,1 and 2 are set as shown on table 5.3 and therefore depend on how the two address pins are connected to the board (either earthed or connected to Vss or even left unconnected).

from this table and assuming your chip's address pins are earthed then your 7 bits address becomes: 1101000 (0x68)

now to read from the chip you have to shift those 7 bits by one bit to the right: which gives you 0xD0 and OR 0x1 (that is raise the read/write bit) which gives you 0xD1. that is the address you send when you want to write.

Should you want to write then the address becomes 0xD0 as the read bit is not raised.

\$\endgroup\$
1
\$\begingroup\$

The basic problem is that you are interpreting the whole first byte as the address. Actually only the upper 7 bits of the first byte are the address, and the low bit indicates whether the IIC sequence will be read or write.

If you look closely, you will see the address is the same in both your cases. For writing, you send a address byte value of 30h = 00110000b. The address is the left 7 bits of that, which is 0011000b = 24. To read from the device, the first byte again has this same address of 0011000b in the high bits and 1 in the low bits: 00110001b = 31h.

There is more discussion of this issue at How to display I2C address in hex.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.