i2c0 reading wrong data

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

i2c0 reading wrong data

Jump to solution
938 Views
bharadwaaj
Contributor III

Hi i am trying to interface KEA128 with accelerometer(MMA8451) or magneto meter (MAG3310) am having trouble reading who am i register getting wrong values have attached the project below using code warrior.

Original Attachment has been moved to: file.zip

Labels (1)
0 Kudos
1 Solution
738 Views
bharadwaaj
Contributor III

Hi i forgot to add the dummy read thats why it was giving random values if we read from the bus now i have changed my code in order to make dummy read and then read now it works fine.... This above code is not from iic driver file. I have written this register read functionality in order to read from register....

uint8_t I2C_MasterRegisterReadWait(I2C_Type *pI2Cx,uint8_t u8SlaveAddress, uint8_t u8RegisterAddress,uint8_t *pRdBuff)

{

   

    uint8_t u8ErrorStatus=0;

    /* send start signals to bus */

    u8ErrorStatus = I2C_Start(pI2Cx);

    /* send device address to slave */

    u8ErrorStatus = I2C_WriteOneByte(pI2Cx,((uint8_t)u8SlaveAddress<<1) | I2C_WRITE);

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

     return u8ErrorStatus;

    }

   

    u8ErrorStatus = I2C_WriteOneByte(pI2Cx,((uint8_t)u8RegisterAddress) );

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

         return u8ErrorStatus;

    }

    u8ErrorStatus = I2C_RepeatStart(pI2Cx);

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

             return u8ErrorStatus;

    }

   

    u8ErrorStatus = I2C_WriteOneByte(pI2Cx,((uint8_t)u8SlaveAddress<<1) | I2C_READ);

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

       return u8ErrorStatus;

    }

   

     /* if no error occur, received the correct ack from slave

            continue to send data to slave

        */

    /* dummy read one byte to switch to Rx mode */

    u8ErrorStatus = I2C_ReadOneByte(pI2Cx,&pRdBuff[0],I2C_SEND_NACK);

   

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

     return u8ErrorStatus;

    }

   

    u8ErrorStatus = I2C_ReadOneByte(pI2Cx,&pRdBuff[0],I2C_SEND_NACK);

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

                    return u8ErrorStatus;

    }

     /* send stop signals to bus */

     u8ErrorStatus = I2C_Stop(pI2Cx);

    

     return u8ErrorStatus;

           

}

View solution in original post

0 Kudos
4 Replies
739 Views
bharadwaaj
Contributor III

Hi i forgot to add the dummy read thats why it was giving random values if we read from the bus now i have changed my code in order to make dummy read and then read now it works fine.... This above code is not from iic driver file. I have written this register read functionality in order to read from register....

uint8_t I2C_MasterRegisterReadWait(I2C_Type *pI2Cx,uint8_t u8SlaveAddress, uint8_t u8RegisterAddress,uint8_t *pRdBuff)

{

   

    uint8_t u8ErrorStatus=0;

    /* send start signals to bus */

    u8ErrorStatus = I2C_Start(pI2Cx);

    /* send device address to slave */

    u8ErrorStatus = I2C_WriteOneByte(pI2Cx,((uint8_t)u8SlaveAddress<<1) | I2C_WRITE);

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

     return u8ErrorStatus;

    }

   

    u8ErrorStatus = I2C_WriteOneByte(pI2Cx,((uint8_t)u8RegisterAddress) );

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

         return u8ErrorStatus;

    }

    u8ErrorStatus = I2C_RepeatStart(pI2Cx);

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

             return u8ErrorStatus;

    }

   

    u8ErrorStatus = I2C_WriteOneByte(pI2Cx,((uint8_t)u8SlaveAddress<<1) | I2C_READ);

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

       return u8ErrorStatus;

    }

   

     /* if no error occur, received the correct ack from slave

            continue to send data to slave

        */

    /* dummy read one byte to switch to Rx mode */

    u8ErrorStatus = I2C_ReadOneByte(pI2Cx,&pRdBuff[0],I2C_SEND_NACK);

   

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

     return u8ErrorStatus;

    }

   

    u8ErrorStatus = I2C_ReadOneByte(pI2Cx,&pRdBuff[0],I2C_SEND_NACK);

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

                    return u8ErrorStatus;

    }

     /* send stop signals to bus */

     u8ErrorStatus = I2C_Stop(pI2Cx);

    

     return u8ErrorStatus;

           

}

0 Kudos
738 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello Bharadwaaj,

Could you pleas describe the the problem In detail , that can find the problem faster .

BR

Alice

0 Kudos
738 Views
bharadwaaj
Contributor III

Hi with the below code we are trying to read the Who am i register of MAG3310(slave address is: 0x0e) I am trying to read the who am i register (reg address is: 0x07).

I am supposed to read 0xC4 but am getting 0x1D.

uint8_t I2C_MasterRegisterReadWait(I2C_Type *pI2Cx,uint8_t u8SlaveAddress, uint8_t u8RegisterAddress,uint8_t *pRdBuff)

{

   

    uint8_t u8ErrorStatus=0;

    /* send start signals to bus */

    u8ErrorStatus = I2C_Start(pI2Cx);

    /* send device address to slave */

    u8ErrorStatus = I2C_WriteOneByte(pI2Cx,((uint8_t)u8SlaveAddress<<1) | I2C_WRITE);

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

     return u8ErrorStatus;

    }

   

    u8ErrorStatus = I2C_WriteOneByte(pI2Cx,((uint8_t)u8RegisterAddress) );

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

         return u8ErrorStatus;

    }

    u8ErrorStatus = I2C_RepeatStart(pI2Cx);

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

             return u8ErrorStatus;

    }

   

    u8ErrorStatus = I2C_WriteOneByte(pI2Cx,((uint8_t)u8SlaveAddress<<1) | I2C_READ);

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

       return u8ErrorStatus;

    }

   

     /* if no error occur, received the correct ack from slave

            continue to send data to slave

        */

    /* dummy read one byte to switch to Rx mode */

    I2C_ReadOneByte(pI2Cx,&pRdBuff[0],I2C_SEND_NACK);

   

    if( u8ErrorStatus != I2C_ERROR_NULL )

    {

     return u8ErrorStatus;

    }

     /* send stop signals to bus */

     u8ErrorStatus = I2C_Stop(pI2Cx);

    

     return u8ErrorStatus;

           

}

0 Kudos
738 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello

I have check the datasheet of MAG3310 , the data of 0X1D is the read address , so are you sue you checked the data is the register of "Who am i" ?

And  i fund in you IIC file , there is a function of " I2C_MasterReadWait()" , if use this function , what about the result ?

BR

Alice

0 Kudos