0
\$\begingroup\$

Running a test on a PIC16F18877 I encounter a problem with i2cWrite.

During sending an adress to a TCA9555 i can not get a proper ACK. After I2c start the function is called with slave adress 0x40. In fact 0x20 first 7 bits for slave adress complemented with 0 for Write on bit 0. A0;A1 and A2 are all pulled to GND. Testing with a different TCA9555 gives the same error.

I need to see ACK becomes 0 but the return value is 1.

The i2cWritefunction has been used in many projects and tested just now with a DS3231. From this device I get a proper ACK.

Stepping through the function shows the proper SDA high and low signals. Even visible on the SDA line itself.

I might be doing something wrong with the TCA9555 but have no idea what.

   bit i2cWrite(const unsigned char Byte)
       {
            unsigned char i = 0;
        for(i=0;i<8;i++)        // Repeat for every bit
        {
          Set_SCL_Low;
          Delay();
          // when it is confirm that SCL is low
          if((Byte<<i)&0x80)    // Place data bit value on SDA pin
            Set_SDA_High;   // If bit is high, make SDA high
          else          // Data is transferred MSB first
            Set_SDA_Low;    // If bit is low, make SDA low
          Delay();
          Set_SCL_High;     // So that slave can ackknowldege
          Delay();
          Delay();
         }
         // Get ACK from slave
         Set_SCL_Low;
         Set_SDA_High;
         Delay();
         Delay();
         Set_SCL_High;
         Delay();
         Delay();
         return SDA;
        }
\$\endgroup\$
9
  • \$\begingroup\$ I have to ask: Why are you bit-banging a uC that has hardware support for I2C? Those delays are making me cringe. Seems like the hard way... You had better get out a logic analyzer/scope and save some traces. \$\endgroup\$ Dec 13, 2017 at 21:42
  • 1
    \$\begingroup\$ All of this is done for you in section 31 of the datasheet. However, if you need to bit-bang this interface, please post some traces of scl & sda of the transaction. \$\endgroup\$ Dec 13, 2017 at 22:00
  • 1
    \$\begingroup\$ @ChrisKnudsen. Since I also use SPI on the same uC i need bit-bang. I will try to create some traces with the , non digital, oscilloscope. \$\endgroup\$
    – Decapod
    Dec 13, 2017 at 22:04
  • \$\begingroup\$ To debug I2C (and other serial interfaces), it is highly recommended to have a logic analyzer with built-in decoder. Saleae 24MHz 8-ch analyzer clones can be found for $6 - $12 on eBay. \$\endgroup\$ Dec 13, 2017 at 22:14
  • \$\begingroup\$ @AliChen. I ordered one just now \$\endgroup\$
    – Decapod
    Dec 13, 2017 at 22:28

0

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.

Browse other questions tagged or ask your own question.