- Mon Nov 08, 2010 8:41 pm
#113033
Hello folks, I'm having issues connecting a pic microcontroller to the itg-3200 gyro using I2C.
I want to read the gyro's address (reg0) to ensure the I2C communication works, but am getting inconsistent results.
The address occasionally reads correctly, but most of the time the I2C read function does not work because of a bus collision: when this happens, PIR2bits.BCLIF is 1. What could cause this error?
Here is the code I have to read a register from I2C:
I want to read the gyro's address (reg0) to ensure the I2C communication works, but am getting inconsistent results.
The address occasionally reads correctly, but most of the time the I2C read function does not work because of a bus collision: when this happens, PIR2bits.BCLIF is 1. What could cause this error?
Here is the code I have to read a register from I2C:
Code: Select all
char readRegister(unsigned char regNumber)
{
unsigned char regValue, err;
StartI2C();
while ( SSPCON2bits.SEN ); // wait for start condition to end
if( PIR2bits.BCLIF ) // test for bus collision
return( -1 );
// ITG address + Write bit
if((err = WriteI2C(0b11010000)) != 0)
return err;
// Write register to read from
if((err=WriteI2C(regNumber)) != 0)
return err;
RestartI2C();
while( SSPCON2bits.RSEN );
// ITG address + Read bit
if((err=WriteI2C(READ_FROM_ITG)) != 0)
return err;
// read reg[regNumber]
regValue = ReadI2C();
IdleI2C();
NotAckI2C();
while( SSPCON2bits.ACKEN );
StopI2C();
while( SSPCON2bits.PEN );
return regValue;
}