Electrical – I2C Write Operation on TCA9539

i2cmicrocontrollertexas instruments

I am using TI's TCA9539 for some read and write I2C operation to control some signals on my test board (school project).
Per TI, the write operation comprises of the following,

"To write on the I2C bus, the master sends a START condition on the bus with the address of the slave, as well as the last bit (the R/W bit) set to 0, which signifies a write. After the slave sends the acknowledge bit, the master
then sends the register address of the register to which it wishes to write. The slave acknowledges again, letting the master know it is ready. After this, the master starts sending the register data to the slave until the master has sent all the data necessary (which is sometimes only a single byte), and the master terminates the transmission with a STOP condition."

However i am always getting a Nack from slave when i send the register address information to the slave.

Here is my sample code (very basic)

int Address = 0x74;
#define control_register 0x02 // command byte for Output Port 0
#define data_register_1 0xFF

void setup()
{
    byte ack;
    Wire.begin();
    Serial.begin(9600);
    while (!Serial);
    Wire.beginTransmission(Address);
    ack = Wire.endTransmission();
    if (ack == 0)
    {
        Wire.beginTransmission(control_register);
        Serial.print("Control Register is : ");
        byte bck = Wire.endTransmission();
        if (bck == 0) // since i dont get a nack, this portion of code doesnt execute
        {
            Wire.write(control_register);
            Wire.write(data_register_1);

        }

    }
}

Best Answer

Oh. Simple enough. You begin trasmission to 0x74. Then you end it.

Then you begin transmission to 0x02. That means you really sent a transmission to device address 0x02. Instead of device 0x74 register 0x02.

For that you would do

Begin transmission 0x74.  
Write 0x02. Register
Write value.  
End transmission. 
If back = 0, you successfully
wrote value to register 0x02 on device 0x74

By ending the transmission you send a i2c stop condition, ruining the write or read from the device you began transmission to.