I2C interface problem: not getting ACK back

Discussion in 'The Projects Forum' started by Gundam, Dec 11, 2008.

  1. Gundam

    Thread Starter New Member

    Dec 11, 2008
    4
    0
    Hi All,

    I'm trying to control an Avago color sensor development kit (HDJD-JD14). I'm using the driver they provided (ftd2xx.dll) but I cannot get an ACK back from the sensor after sending the slave address for writing (B0H). I've also attached my C code below. Could anyone tell me what I've done wrong?

    Thank you very much,
    Gordon

    #include<stdio.h>
    #include<windows.h>
    #include "ftd2xx.h"
    #pragma comment(lib, "FTD2XX.lib")
    FT_STATUS ftStatus;
    FT_HANDLE ftHandle;
    // start condition
    void i2c_start()
    {
    DWORD dwBytesInQueue = 0;
    UCHAR cBufWrite; //buffer to contain data to write to device

    cBufWrite = 0x03;
    ftStatus=FT_Write(ftHandle,&cBufWrite,sizeof(cBufWrite),&dwBytesInQueue);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);//1=asynchronous bit-bang mode
    if(ftStatus!=FT_OK) printf("Device setBit error");
    dwBytesInQueue = 0; //reset counter for bytes written

    cBufWrite = 0x02;
    ftStatus=FT_Write(ftHandle,&cBufWrite,sizeof(cBufWrite),&dwBytesInQueue);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    dwBytesInQueue = 0; //reset counter for bytes written

    cBufWrite = 0x00;
    ftStatus=FT_Write(ftHandle,&cBufWrite,sizeof(cBufWrite),&dwBytesInQueue);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    dwBytesInQueue = 0; //reset counter for bytes written
    }
    // byte send, Ack returned
    unsigned char i2c_tx(unsigned char databyte)
    {
    unsigned char i=0,b,c,ack=0;
    DWORD dwBytesInQueue = 0;

    for(i=0;i<8;i++)
    {
    c=(databyte>>(7-i))&0x01;

    b=0x00|c;
    ftStatus=FT_Write(ftHandle,&b,sizeof(b),&dwBytesInQueue);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    dwBytesInQueue = 0; //reset counter for bytes written

    b=0x02|c;
    ftStatus=FT_Write(ftHandle,&b,sizeof(b),&dwBytesInQueue);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    dwBytesInQueue = 0; //reset counter for bytes written

    b=0x00|c;
    ftStatus=FT_Write(ftHandle,&b,sizeof(b),&dwBytesInQueue);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    dwBytesInQueue = 0; //reset counter for bytes written
    }

    c=0x01;
    b=0x01|c;
    ftStatus=FT_Write(ftHandle,&b,sizeof(b),&dwBytesInQueue);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    ftStatus=FT_SetBitMode(ftHandle,0xFE,0x01);//set LSB(SDA) as output
    if(ftStatus!=FT_OK) printf("Device setBit error");
    dwBytesInQueue = 0; //reset counter for bytes written

    b=0x03|c;
    ftStatus=FT_Write(ftHandle,&b,sizeof(b),&dwBytesInQueue);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    ftStatus=FT_SetBitMode(ftHandle,0xFE,0x01);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    dwBytesInQueue = 0; //reset counter for bytes written

    ftStatus=FT_GetBitMode(ftHandle,&ack);
    if(ftStatus!=FT_OK) printf("Device GetBit error");
    ack=ack&0x01;

    b=0x01|c;
    ftStatus=FT_Write(ftHandle,&b,sizeof(b),&dwBytesInQueue);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    ftStatus=FT_SetBitMode(ftHandle,0xFE,0x01);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    dwBytesInQueue = 0; //reset counter for bytes written

    return ack;
    }
    // Stop condition
    void i2c_stop()
    {
    DWORD dwBytesInQueue = 0;
    UCHAR cBufWrite; //buffer to contain data to write to device

    cBufWrite = 0x00;
    ftStatus=FT_Write(ftHandle,&cBufWrite,sizeof(cBufWrite),&dwBytesInQueue);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    dwBytesInQueue = 0; //reset counter for bytes written

    cBufWrite = 0x02;
    ftStatus=FT_Write(ftHandle,&cBufWrite,sizeof(cBufWrite),&dwBytesInQueue);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    dwBytesInQueue = 0; //reset counter for bytes written

    cBufWrite = 0x03;
    ftStatus=FT_Write(ftHandle,&cBufWrite,sizeof(cBufWrite),&dwBytesInQueue);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    if(ftStatus!=FT_OK) printf("Device setBit error");
    dwBytesInQueue = 0; //reset counter for bytes written
    }
    int main()
    {
    DWORD numDevs;
    UCHAR x;
    char buffer[64];

    ftStatus=FT_ListDevices(&numDevs,NULL,FT_LIST_NUMBER_ONLY);
    if(ftStatus==FT_OK)
    {
    printf("%i devices found\n",(int)numDevs);
    }
    if(numDevs>=1)
    {
    ftStatus=FT_ListDevices((PVOID)0,buffer,FT_LIST_BY_INDEX|FT_OPEN_BY_DESCRIPTION);
    if(ftStatus==FT_OK)
    printf("Description of Device 0: %s\n",buffer);
    ftStatus=FT_Open(0,&ftHandle);
    if(ftStatus==FT_OK)
    {
    printf("Device opened\n");

    i2c_start();
    x=i2c_tx(0xb0);
    printf("%X\n", (unsigned) x);
    i2c_stop();
    }
    }
    return 0;
    }
     
  2. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    I just indented your source code so it's easier to read.

    Code ( (Unknown Language)):
    1. #include<stdio.h>
    2. #include<windows.h>
    3. #include "ftd2xx.h"
    4. #pragma comment(lib, "FTD2XX.lib")
    5. FT_STATUS ftStatus;
    6. FT_HANDLE ftHandle;
    7. // start condition
    8. void i2c_start()
    9. {
    10.     DWORD dwBytesInQueue = 0;
    11.     UCHAR cBufWrite; //buffer to contain data to write to device
    12.  
    13.     cBufWrite = 0x03;
    14.     ftStatus=FT_Write(ftHandle,&cBufWrite,sizeof(cBufW rite),&dwBytesInQueue);
    15.     if(ftStatus!=FT_OK) printf("Device setBit error");
    16.     ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);//1=asynchronous bit-bang mode
    17.     if(ftStatus!=FT_OK) printf("Device setBit error");
    18.     dwBytesInQueue = 0; //reset counter for bytes written
    19.  
    20.     cBufWrite = 0x02;
    21.     ftStatus=FT_Write(ftHandle,&cBufWrite,sizeof(cBufW rite),&dwBytesInQueue);
    22.     if(ftStatus!=FT_OK) printf("Device setBit error");
    23.     ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    24.     if(ftStatus!=FT_OK) printf("Device setBit error");
    25.     dwBytesInQueue = 0; //reset counter for bytes written
    26.  
    27.     cBufWrite = 0x00;
    28.     ftStatus=FT_Write(ftHandle,&cBufWrite,sizeof(cBufW rite),&dwBytesInQueue);
    29.     if(ftStatus!=FT_OK) printf("Device setBit error");
    30.     ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    31.     if(ftStatus!=FT_OK) printf("Device setBit error");
    32.     dwBytesInQueue = 0; //reset counter for bytes written
    33. }
    34. // byte send, Ack returned
    35. unsigned char i2c_tx(unsigned char databyte)
    36. {
    37.     unsigned char i=0,b,c,ack=0;
    38.     DWORD dwBytesInQueue = 0;
    39.  
    40.     for(i=0;i<8;i++)
    41.     {
    42.         c=(databyte>>(7-i))&0x01;
    43.  
    44.         b=0x00|c;
    45.         ftStatus=FT_Write(ftHandle,&b,sizeof(b),&dwBytesIn Queue);
    46.         if(ftStatus!=FT_OK) printf("Device setBit error");
    47.         ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    48.         if(ftStatus!=FT_OK) printf("Device setBit error");
    49.         dwBytesInQueue = 0; //reset counter for bytes written
    50.  
    51.         b=0x02|c;
    52.         ftStatus=FT_Write(ftHandle,&b,sizeof(b),&dwBytesIn Queue);
    53.         if(ftStatus!=FT_OK) printf("Device setBit error");
    54.         ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    55.         if(ftStatus!=FT_OK) printf("Device setBit error");
    56.         dwBytesInQueue = 0; //reset counter for bytes written
    57.  
    58.         b=0x00|c;
    59.         ftStatus=FT_Write(ftHandle,&b,sizeof(b),&dwBytesIn Queue);
    60.         if(ftStatus!=FT_OK) printf("Device setBit error");
    61.         ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    62.         if(ftStatus!=FT_OK) printf("Device setBit error");
    63.         dwBytesInQueue = 0; //reset counter for bytes written
    64.     }
    65.  
    66.     c=0x01;
    67.     b=0x01|c;
    68.     ftStatus=FT_Write(ftHandle,&b,sizeof(b),&dwBytesIn Queue);
    69.     if(ftStatus!=FT_OK) printf("Device setBit error");
    70.     ftStatus=FT_SetBitMode(ftHandle,0xFE,0x01);//set LSB(SDA) as output
    71.     if(ftStatus!=FT_OK) printf("Device setBit error");
    72.     dwBytesInQueue = 0; //reset counter for bytes written
    73.  
    74.     b=0x03|c;
    75.     ftStatus=FT_Write(ftHandle,&b,sizeof(b),&dwBytesIn Queue);
    76.     if(ftStatus!=FT_OK) printf("Device setBit error");
    77.     ftStatus=FT_SetBitMode(ftHandle,0xFE,0x01);
    78.     if(ftStatus!=FT_OK) printf("Device setBit error");
    79.     dwBytesInQueue = 0; //reset counter for bytes written
    80.  
    81.     ftStatus=FT_GetBitMode(ftHandle,&ack);
    82.     if(ftStatus!=FT_OK) printf("Device GetBit error");
    83.     ack=ack&0x01;
    84.  
    85.     b=0x01|c;
    86.     ftStatus=FT_Write(ftHandle,&b,sizeof(b),&dwBytesIn Queue);
    87.     if(ftStatus!=FT_OK) printf("Device setBit error");
    88.     ftStatus=FT_SetBitMode(ftHandle,0xFE,0x01);
    89.     if(ftStatus!=FT_OK) printf("Device setBit error");
    90.     dwBytesInQueue = 0; //reset counter for bytes written
    91.  
    92.     return ack;
    93. }
    94. // Stop condition
    95. void i2c_stop()
    96. {
    97.     DWORD dwBytesInQueue = 0;
    98.     UCHAR cBufWrite; //buffer to contain data to write to device
    99.  
    100.     cBufWrite = 0x00;
    101.     ftStatus=FT_Write(ftHandle,&cBufWrite,sizeof(cBufW rite),&dwBytesInQueue);
    102.     if(ftStatus!=FT_OK) printf("Device setBit error");
    103.     ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    104.     if(ftStatus!=FT_OK) printf("Device setBit error");
    105.     dwBytesInQueue = 0; //reset counter for bytes written
    106.  
    107.     cBufWrite = 0x02;
    108.     ftStatus=FT_Write(ftHandle,&cBufWrite,sizeof(cBufW rite),&dwBytesInQueue);
    109.     if(ftStatus!=FT_OK) printf("Device setBit error");
    110.     ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    111.     if(ftStatus!=FT_OK) printf("Device setBit error");
    112.     dwBytesInQueue = 0; //reset counter for bytes written
    113.  
    114.     cBufWrite = 0x03;
    115.     ftStatus=FT_Write(ftHandle,&cBufWrite,sizeof(cBufW rite),&dwBytesInQueue);
    116.     if(ftStatus!=FT_OK) printf("Device setBit error");
    117.     ftStatus=FT_SetBitMode(ftHandle,0xFF,0x01);
    118.     if(ftStatus!=FT_OK) printf("Device setBit error");
    119.     dwBytesInQueue = 0; //reset counter for bytes written
    120. }
    121. int main()
    122. {
    123.     DWORD numDevs;
    124.     UCHAR x;
    125.     char buffer[64];
    126.  
    127.     ftStatus=FT_ListDevices(&numDevs,NULL,FT_LIST_NUMB ER_ONLY);
    128.     if(ftStatus==FT_OK)
    129.     {
    130.         printf("%i devices found\n",(int)numDevs);
    131.     }
    132.     if(numDevs>=1)
    133.     {
    134.         ftStatus=FT_ListDevices((PVOID)0,buffer,FT_LIST_BY _INDEX|FT_OPEN_BY_DESCRIPTION);
    135.         if(ftStatus==FT_OK)
    136.         printf("Description of Device 0: %s\n",buffer);
    137.         ftStatus=FT_Open(0,&ftHandle);
    138.         if(ftStatus==FT_OK)
    139.         {
    140.             printf("Device opened\n");
    141.  
    142.             i2c_start();
    143.             x=i2c_tx(0xb0);
    144.             printf("%X\n", (unsigned) x);
    145.             i2c_stop();
    146.         }
    147.     }
    148.     return 0;
    149. }
     
  3. Alberto

    Active Member

    Nov 7, 2008
    169
    36
    Did you pulled up with 2x5K resistors the I2C lines ?

    Al.
     
  4. Gundam

    Thread Starter New Member

    Dec 11, 2008
    4
    0
    Hi Al,

    Yes, I have two 2K7 pull up resistors.

    Gordon
     
  5. Gundam

    Thread Starter New Member

    Dec 11, 2008
    4
    0
    In fact, I'm not able to detect SDA = 0 as an ACK.

    Gordon
     
  6. leftyretro

    Active Member

    Nov 25, 2008
    394
    2
    I'm not a code warrior so I can't really comment on your code, but I can share from other forums that the vast majority of problems with I2C seem to boil down to the required pull up resistors and the device addressing schem.

    Seems to be a lot of confusion about 7 bit addressing Vs 8 bit with the LSB being a read/write bit. Also insuring the I2C devices are at the address you think they are at, as there is usually a device base address and a hardwired additional address bits so as to allow several devices of the same type to share the I2C buss and those hardwired address bits have to be wired high or low to generate the full address desired for the device. If this is all old news to you then never mind ;)

    I've experminated a little with a few I2C devices and I'm a real fan of this buss.

    Lefty
     
  7. Gundam

    Thread Starter New Member

    Dec 11, 2008
    4
    0
    Hi Lefty,

    Suppose the slave address is 58H, thus for reading should be B1H. After I've sent the LSB (write/read bit) (which is 1 in this case), should the SDA line go to LOW state immediately as an ACK? Or will the SDA line go to LOW state after I've made SCL go HIGH?

    Thank you very much,
    Gordon
     
  8. Misbah

    New Member

    Mar 9, 2009
    2
    0
    First check the slave address whether it is correct or not ????

    SDA should go low as ACK that can verified using scope ...

    If your slave address is correct then probably your slave device is not responding due to improper design ....get confirmed with the pull up resistor value to be correct .... last but not least check the timings ....

    In which mode (speed r u operating ) Is the device external or internal to the board ???

    What is the capacitive inductance it is offering ????
     
Loading...