I2C programing question

Hi all,

I am currently programming a chip which is an audio codec, which is the slave, via thw arduino uno, using I2C. Now my question is that This audio codec chip has also several internal registers, so when I am going to program the chip, first I need to send the chip, the slave address and wait for the acknowledge. Now usually, in all the examples I have seen, after the slave's chip address, a data byte is sent.

In my case, I also need to select which internal register I am going to send data to.

How am I going to use the Wire.h function codes so that after I use the Wire.beginTransmission(slave_address);  to send the address of the slave chip, I send also the address of the register I need to send data too?

Do I need to include another, Wire.beginTransmission(register_address);? or otherwise use,Wire.write(register_address);?

please help me!

The data byte you see in the examples can have any meaning, and actually you are not limited to sending a single byte.

So, for example, you may send in the first byte the register address you want to set, and in the following byte (of the same message) the value you want to set. The I2C protocol just takes care that the message is delivered but does not set the register -- this must be done by the receiving node.

You must use the second variant. In full this looks something like this:

Wire.beginTransmission(deviceAddress);
Wire.write(registerAddress);
Wire.write(registerValue);
Wire.endTransmission(); // with this call the data is actually sent to the device

Keep in mind that the buffer size is 32 bytes, so keep your data packets smaller than that.