Hi all,
I'm trying to interface a LSM303AGR, to get the magnetometer and accelerometer values in I2C.
The component is responding well to whoami requests (both compass and magnetometer parts), I manage to get mag values but the accelerometer values seems to be stuck.
Here are my init parameters:
/* REBOOT ACCELEROMETER MEMORY CONTENT */
I2C_Write_reg(I2C2,I2C_LSM3_ACC_ADRESS << 1,I2C_LSM3_CTRL_REG5_A,0x80);
RTC_wait_ms(6);
/* REBOOT MAGNETOMETER MEMORY CONTENT */
I2C_Write_reg(I2C2,I2C_LSM3_MAG_ADRESS << 1,I2C_LSM3_CFG_REG_A_M,0x40);
RTC_wait_ms(6);
(void)I2C_Read(I2C2, I2C_LSM3_ACC_ADRESS << 1, 0, I2C_LSM3_WHO_AM_I_ACC, &whoamiacc ,1);
if(whoamiacc != 0x33) FAIL("LSM303AGR accelerometer side isn't responding");
else {
/* ACCELEROMETER INITIALIZATIONS */
I2C_Write_reg(I2C2,I2C_LSM3_ACC_ADRESS << 1,I2C_LSM3_CTRL_REG1_A,0x57);
I2C_Write_reg(I2C2,I2C_LSM3_ACC_ADRESS << 1,I2C_LSM3_CTRL_REG2_A,0x00);
I2C_Write_reg(I2C2,I2C_LSM3_ACC_ADRESS << 1,I2C_LSM3_CTRL_REG3_A,0x00);
I2C_Write_reg(I2C2,I2C_LSM3_ACC_ADRESS << 1,I2C_LSM3_CTRL_REG4_A,0x98);//BDU ON +/-4G Range and High res ON
I2C_Write_reg(I2C2,I2C_LSM3_ACC_ADRESS << 1,I2C_LSM3_CTRL_REG5_A,0x00);
}
(void)I2C_Read(I2C2, I2C_LSM3_MAG_ADRESS << 1, 0, I2C_LSM3_WHO_AM_I_MAG, &whoamimag ,1);
if(whoamimag != 0x40) FAIL("LSM303AGR magnetometer side isn't responding");
else{
/* MAGNETOMETER INITIALIZATIONS */
I2C_Write_reg(I2C2,I2C_LSM3_MAG_ADRESS << 1,I2C_LSM3_CFG_REG_A_M,0x8C);
// I2C_Write_reg(I2C2,I2C_LSM3_MAG_ADRESS << 1,I2C_LSM3_CFG_REG_B_M,0x02);
}
My reading fonction for the accelerometer is the following:
uint8_t drdy = 0;
uint8_t x_accel[2] = {0};
uint8_t y_accel[2] = {0};
uint8_t z_accel[2] = {0};
I2C_Read(I2C2, I2C_LSM3_ACC_ADRESS << 1, 0, I2C_LSM3_STATUS_REG_A, &drdy ,1);
if((drdy & 0x01) == 0x01) I2C_Read(I2C2, I2C_LSM3_ACC_ADRESS << 1, 0, I2C_LSM3_OUT_Y_L_A, y_accel ,2);
if((drdy & 0x02) == 0x02) I2C_Read(I2C2, I2C_LSM3_ACC_ADRESS << 1, 0, I2C_LSM3_OUT_Z_L_A, z_accel ,2);
if((drdy & 0x04) == 0x04) I2C_Read(I2C2, I2C_LSM3_ACC_ADRESS << 1, 0, I2C_LSM3_OUT_X_L_A, x_accel ,2);
I get the exact same answer at every reading:
x_accel[0] = 0xA0x_accel[1] = 0xA0y_accel[0] = 0xC0y_accel[1] = 0xC0z_accel[0] = 0x40z_accel[1] = 0x40
Am I doing something wrong in the initializations? I would like the accelerometer to be in +/-4G range, 100Hz, without FIFO and in High res mode.
The I2C_Read function seems to work well, my function reading the magnetometer works fine.
Thank you!