I2C Wire.Write() Concept Question

Hi there,

I have a general question regarding the wire.write() function. I understand that to move the address pointer to a specific register you include the register address in "()". Then, if you have data to write to each subsequent register, you can simply include that byte of data in each subsequent wire.write(_ command and the data will be loaded into the buffers sequentially. My questions are as follows:

  1. If no register address is included, and you've connected to a peripheral device on the bus, does the controller assume the first register address is just 0x00?
  2. How is a slave address understood through the wire.write() command to be a slave address and not the data to transmit? It's all just 1's and 0's. How does it know the difference? What if your 7 bits of data match the slave address? How is that handled?

Thanks for anyone's help with this.

It depends on the target device.

The most common form of communication is a register address followed by the content of that register <I2C address><register><value>. (additional bytes after that in the same transaction will either be ignored or might overwrite the first bytes).

Some devices allow for <I2C address><register><value><register><value> to be sent in one transaction, thus removing the need for repeating the I2C address byte each time.

Other devices (eg SRAM chips) would allow you to do page mode access, and the device auto-increments the register address for each data byte <I2C address><register><value1><value2><value3>..<valueN>.

The behavior is down to the device you are sending data to --> look in the data-sheet and see what's supported

(there are start/ACK/stop bits on top in the protocol)

1 Like

Thanks J-M-L!

And the solution was as you say. I found the section in the datasheet for the RTC that I"m using and it clearly outlines the steps needed to read/write to the address registers in multiple ways. For this IC, there is an internal address pointer that keeps track of the address and auto increments. however you can choose a specific register to access through a write command - you just need to be wary of the address pointer's location as it will now be +1 from the register you just transmitted data.

Finally, I feel like I understand this.

Eureka moment is always a great feeling :slight_smile:
have fun

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.