I2C Read for PIC32

Discussion in 'Embedded Systems and Microcontrollers' started by RoboticFan87, Jan 9, 2010.

  1. RoboticFan87

    Thread Starter Member

    Sep 12, 2009
    38
    0
    Hey guys, i wrote a code for the I2C read for my pic32. Basically the pic32 reads from a control chip DS2482-800. which that chip reads from an ID chip DS2401. If you can check my code and tell me if it looks write. its still giving me problems.

    Code ( (Unknown Language)):
    1. unsigned char I2C_Read_Register(void)
    2. {
    3.  
    4.    int result;
    5.    unsigned char data;
    6.    // 1-Wire Read Bytes (Case C)
    7.    //   S AD,0 [A] 1WRB [A] Sr AD,1 [A] [Status] A [Status] A\
    8.    //                                   \--------/
    9.    //                     Repeat until 1WB bit has changed to 0
    10.    //   Sr AD,0 [A] SRP [A] E1 [A] Sr AD,1 [A] DD A\ P
    11.    //
    12.    //  [] indicates from slave
    13.    //  DD data read
    14.    I2CStart(I2C1);
    15.    I2CSetSlaveAddress(I2C1, 0x30, 0, 0);
    16.    if(I2CByteWasAcknowledged(I2C1)){
    17.  result = I2CSendByte(I2C1,0x96);
    18.    }
    19.    if(result == 0){
    20.     I2CRepeatStart(I2C1);
    21.    }
    22.    I2CSetSlaveAddress(I2C1, 0x31, 0, 0);
    23.    if(I2CByteWasAcknowledged(I2C1)){
    24.  if (I2CReceivedDataIsAvailable(I2C1)) {
    25.          I2CAcknowledgeByte(I2C1, TRUE);
    26.          data = I2CGetByte(I2C1);
    27.      }
    28. }
    29. I2CStop(I2C1);
    30.    return data;
    31. }
    32.  
    Thank you
     
Loading...