No hay datos del magnetómetro en MPU9255

Tengo un problema con la lectura de datos del magnetómetro en un MPU9255. El acelerómetro y el giroscopio funcionan correctamente, pero no recibo ningún dato del magnetómetro. Estoy trabajando en el objective STM32F103.

¿Alguien tiene alguna idea de lo que está mal con el código de abajo?

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) { //AX 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); //AY 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); //AZ 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); //GX 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); //GY 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); //GZ 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); } 

Debe habilitar el modo de derivación I2C en el MPU9255 para comunicarse con el AK8963 directamente. Establezca el bit BYPASS_EN en el registro de configuración de habilitación de bypass / omisión INT (registro 55) al comienzo de su progtwig con:

 uint8_t BypassEnable = 0x02; HAL_I2C_Mem_Write(&hi2c1, 0xD0, 55, I2C_MEMADD_SIZE_8BIT, &BypassEnable,1,1000); 

Vea aquí un ejemplo práctico : Arduino Sensor 10DOF MPU9255 no puede obtener datos del magnetómetro