1
\$\begingroup\$

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);

        }

    }
}
\$\endgroup\$
6
  • \$\begingroup\$ Are you using the right address? 7 bit or 8 bit? AFAIK the arduino library needs the 7 bit address, not including the read/write bit. \$\endgroup\$
    – Passerby
    Jul 23, 2017 at 4:09
  • \$\begingroup\$ A 7 bit 0x74 requires A0 and A1 to be tied to ground. Did you do that? \$\endgroup\$
    – Passerby
    Jul 23, 2017 at 4:13
  • \$\begingroup\$ What value does wire end transmission return? A 1 means transmission error. 2 is address error. 3 is data/register error. 4 is other. \$\endgroup\$
    – Passerby
    Jul 23, 2017 at 4:21
  • 1
    \$\begingroup\$ You are confusing the slave address and the register address. At least one of your beginTransmission calls is wrong. \$\endgroup\$
    – CL.
    Jul 23, 2017 at 7:11
  • \$\begingroup\$ Hello, does this mean i only need to start one transmission and then end it after i have send the complete write cycle based on the data sheet? \$\endgroup\$
    – JYasir
    Jul 24, 2017 at 1:08

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Hello Passerby, yep the address is 7-bit right justified and i can run a successful scan of i2c device and see 0x74. I was getting 2 which means the slave address was nacking when i was trying to write to command register. \$\endgroup\$
    – JYasir
    Jul 23, 2017 at 20:33
  • \$\begingroup\$ @jyasir yes. I realized that after CL posted his comment. You need to use wire.write to write the control register. \$\endgroup\$
    – Passerby
    Jul 23, 2017 at 20:38
  • \$\begingroup\$ @jyasir don't forget to accept the answer if it helped \$\endgroup\$
    – Passerby
    Jul 24, 2017 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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