I'm a beginner at I2C communication and I am trying to hook up communication with an I2C device a (mpu6050) using librobotcontrol http://strawsondesign.com/docs/librobotcontrol/group___i2_c.html i2c library in C. The code Excluding stuff not I2C related I have:
#define I2C_ADDRESS 0x68
#define I2C_BUS 1
void setupMPU();
float recordAccelRegisters();
int main(){
float ax = 0;
while(1){
ax = recordAccelRegisters()
}
float recordAccelRegisters(){
float Xg = 0;
float Xa = 0, Ya, Za;
unsigned char data;
printf("Recording Accel Data\n");
rc_i2c_init (I2C_BUS,I2C_ADDRESS);
rc_i2c_read_bytes(I2C_BUS, 0x3B, 6, data);
rc_i2c_close(I2C_BUS);
//Xa = 8 << data;
//Xa = (float)Xa;
//Xg = Xa/16384.0;
return Xg;
}
void setupMPU(){
printf("Initalizing Mpu\n");
// init I2C Bus
rc_i2c_init (I2C_BUS,I2C_ADDRESS);
//Wake up setup
rc_i2c_write_byte(I2C_BUS, 0x6B, 0x00);
rc_i2c_close(I2C_BUS);
printf("Configuring Accelerometer\n");
printf("Initalizing Mpu\n");
//Accel Config
rc_i2c_init (I2C_BUS,I2C_ADDRESS);
rc_i2c_write_byte(I2C_BUS, 0x1C, 0x00);
printf("Done with Accel Config\n");
rc_i2c_close(I2C_BUS);
}
}
I am trying to figure out if I need to close and reopen the port every time like in an Arduino using wire library or if I can just initialize it once. I have tried many different types of code configurations. For instance only initializing the communication only once, using send words instead of send bytes, sending only 1 byte at a time ect... but I am still unable to get data from the device. I am also struggling with weather or not to use read words or read bytes. It keeps returning a -1 bytes as seen by this error message when using
read bytes:
ERROR: in rc_i2c_read_bytes, received -1 bytes from device, expected 1
and this error when using read words:
ERROR: Segmentation Fault
Fault address: (nil)
Address not mapped.
Segmentation fault
Any help on this will be much appreciated.