Hallo,
hier mal der Code um das IMU einzuschalten und auszulesen:
erstmal die notwendigen Parameter initialisieren:
uint8_t Gyro_array[6]={ 0,0,0,0,0,0} ;
int16_t* Gyro_array16 = (int16_t*)Gyro_array;
#define Gyro_X Gyro_array16[0]
#define Gyro_Y Gyro_array16[1]
#define Gyro_Z Gyro_array16[2]
uint8_t Acc_array[4]={ 0,0,0,0} ;
int16_t* Acc_array16 = (int16_t*)Acc_array;
#define Acc_X Acc_array16[0]
#define Acc_Y Acc_array16[1]
//I2C Gyro
#define Gyro_Read_Address 0xD3
#define Gyro_Write_Address 0xD2
//I2C Accelero
#define Acc_Read_Address 0x31
#define Acc_Write_Address 0x30
i2c_start_wait(Gyro_Write_Address);
i2c_write(0x20);
i2c_write(0x0F); // Power on, X Y and Z enabled
i2c_stop();
i2c_start_wait(Gyro_Write_Address);
i2c_write(0x23);
i2c_write(0x90); // output registers not updated until MSB and LSB reading, 500 dps
i2c_stop();
i2c_start_wait(Acc_Write_Address);
i2c_write(0x20);
i2c_write(0x2F); // Normal Mode, 100Hz, X Y and Z enabled
i2c_stop();
i2c_start_wait(Acc_Write_Address);
i2c_write(0x23);
i2c_write(0xb0); // output registers not updated until MSB and LSB reading, +-8g
i2c_stop();
In der Schleife dann die Werte abholen:
uint8_t i = 0;
i2c_start_wait(Gyro_Write_Address);
i2c_write(0xA8);
i2c_rep_start(Gyro_Read_Address); // set device address and read mode
for (i=0;i<5;i++) // get the 5 Bytes from the I2C-Slave
{
Gyro_array = i2c_readAck();
}
Gyro_array = i2c_readNak(); // get the 6th Byte
i2c_stop();
i2c_start_wait(Acc_Write_Address);
i2c_write(0xA8);
i2c_rep_start(Acc_Read_Address); // set device address and read mode
for (i=0;i<3;i++) // get the 3 Bytes from the I2C-Slave
{
Acc_array = i2c_readAck();
}
Acc_array = i2c_readNak(); // get the 4th Byte
i2c_stop();
Edit: hinter den Arrayvariablen fehlt immer das i in eckigen Klammern!!