从 MPU9255 上的磁力计读取数据时遇到问题。加速度计和陀螺仪工作正常,但我没有收到来自磁力计的任何数据。我正在研究 STM32F103 目标。
有谁知道下面的代码有什么问题?
uint8_t Settings = 0x00;
uint8_t SettingsM = 0x00;
uint8_t SettingsL = 0x22; //mode2 16bit
//settings accelerometer and gyroscope
HAL_I2C_Mem_Write(&hi2c1, 0xD0, 27, I2C_MEMADD_SIZE_8BIT, &Settings,1,1000);
HAL_I2C_Mem_Write(&hi2c1, 0xD0, 28, I2C_MEMADD_SIZE_8BIT, &Settings,1,1000);
//Power Down mode
HAL_I2C_Mem_Write(&hi2c1, 0x0C, 0x0A, I2C_MEMADD_SIZE_8BIT, &SettingsM,1,1000);
HAL_Delay(100);
//16 bit output mode 2
HAL_I2C_Mem_Write(&hi2c1, 0x0C, 0x0A, I2C_MEMADD_SIZE_8BIT, &SettingsL,1,1000);
while (1)
{
//A X
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 59, I2C_MEMADD_SIZE_8BIT, &DAHx,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 60, I2C_MEMADD_SIZE_8BIT, &DALx,1,100);
//A Y
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 61, I2C_MEMADD_SIZE_8BIT, &DAHy,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 62, I2C_MEMADD_SIZE_8BIT, &DALy,1,100);
//A Z
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 63, I2C_MEMADD_SIZE_8BIT, &DAHz,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 64, I2C_MEMADD_SIZE_8BIT, &DALz,1,100);
//Temp
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 65, I2C_MEMADD_SIZE_8BIT, &DHT,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 66, I2C_MEMADD_SIZE_8BIT, &DLT,1,100);
//G X
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 67, I2C_MEMADD_SIZE_8BIT, &DGHx,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 68, I2C_MEMADD_SIZE_8BIT, &DGLx,1,100);
//G Y
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 69, I2C_MEMADD_SIZE_8BIT, &DGHy,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 70, I2C_MEMADD_SIZE_8BIT, &DGLy,1,100);
//G Z
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 71, I2C_MEMADD_SIZE_8BIT, &DGHz,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0xD0, 72, I2C_MEMADD_SIZE_8BIT, &DGLz,1,100);
do
{
HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x02, I2C_MEMADD_SIZE_8BIT, &ST1,1,100);
}
while (!(ST1&0x01));
//0x0C - adress AK8963, 0x03-0x08 => HLx-HHz
HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x03, I2C_MEMADD_SIZE_8BIT, &HLx,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x04, I2C_MEMADD_SIZE_8BIT, &HHx,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x05, I2C_MEMADD_SIZE_8BIT, &HLy,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x06, I2C_MEMADD_SIZE_8BIT, &HHy,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x07, I2C_MEMADD_SIZE_8BIT, &HLz,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x08, I2C_MEMADD_SIZE_8BIT, &HHz,1,100);
HAL_I2C_Mem_Read(&hi2c1, 0x0C, 0x09, I2C_MEMADD_SIZE_8BIT, &H,1,100);
//accelerometer
Ax = (int16_t)(DAHx << 8 | DALx);
Ay = (int16_t)(DAHy << 8 | DALy);
Az = (int16_t)(DAHz << 8 | DALz);
//temperature
T = (int16_t)(DHT << 8 | DLT);
//gyroscope
Gx = (int16_t)(DGHx << 8 | DGLx);
Gy = (int16_t)(DGHy << 8 | DGLy);
Gz = (int16_t)(DGHz << 8 | DGLz);
//magnetometer
Hx= (int16_t)(HHx << 8 | HLx);
Hy= (int16_t)(HHy << 8 | HLy);
Hz= (int16_t)(HHz << 8 | HLz);
Ax_g = (float)Ax/16384.0;
Ay_g = (float)Ay/16384.0;
Az_g = (float)Az/16384.0;
Gx_g = (float)Gx/131.0;
Gy_g = (float)Gy/131.0;
Gz_g = (float)Gz/131.0;
arx = (180.0/3.141592) * atanf(Ax_g / sqrtf(Ay_g*Ay_g + Az_g*Az_g));
ary = (180.0/3.141592) * atanf(Ay_g / sqrtf((Ax_g*Ax_g + Az_g*Az_g)));
rx = (0.96 * arx) + (0.04 * Gx_g);
ry = (0.96 * ary) + (0.04 * Gy_g);
}