关于加速度的数据读取问题,贴个参考版本给大家参考!
传感器寄存器读出的数据的单位是bit,将读出数据乘以sensitivity得到mg单位的值。
- HAL_I2C_Init();
- LIS3DH_GetWHO_AM_I(&dump);
- DEBUGOUT("LIS3DH ID:%x\n", dump);
- /* Set ODR (turn ON device) */
- response = LIS3DH_SetODR(LIS3DH_ODR_400Hz);
- if (response == 1) {
- DEBUGOUT("SET_ODR_OK\r\n");
- }
- /* Set PowerMode */
- response = LIS3DH_SetMode(LIS3DH_NORMAL);
- if (response == 1) {
- DEBUGOUT("SET_MODE_OK\r\n");
- }
- /* Set Fullscale */
- response = LIS3DH_SetFullScale(LIS3DH_FULLSCALE_2);
- if (response == 1) {
- DEBUGOUT("SET_FULLSCALE_OK\r\n");
- }
- /* Set axis Enable */
- response = LIS3DH_SetAxis(LIS3DH_X_ENABLE | LIS3DH_Y_ENABLE | LIS3DH_Z_ENABLE);
- if (response == 1) {
- DEBUGOUT("SET_AXIS_OK\r\n");
/* Data bytes from hardware xL, xH, yL, yH, zL, zH */
u8 acc_data[6];
s16 hw_d[3] = { 0 };
err = acc_i2c_read_data(stat, acc_data, 6);
if (err < 0)
return err;
hw_d[0] = ( (s16) ( ( (acc_data[1] << 8) | acc_data[0] ) ) ) >>4; LIS3DH是一个12bit,左对齐的输出,如果16bit精度的话,数据不需要移位
hw_d[1] = ( (s16) ( ( (acc_data[3] << 8) | acc_data[2] ) ) ) >>4;
hw_d[2] = ( (s16) ( ( (acc_data[5] << 8) | acc_data[4] ) ) ) >>4;
hw_d[0] = hw_d[0] * sensitivity;
hw_d[1] = hw_d[1] * sensitivity;
hw_d[2] = hw_d[2] * sensitivity; 然后芯片需要或者是技术问题都可以找我。动能世纪-赵工分享
|