简体   繁体   中英

No data from magnetometer on MPU9255

I have a problem with reading data from the magnetometer on an MPU9255. The accelerometer and gyroscope work properly, but I do not receive any data from the magnetometer. I am working on the STM32F103 target.

Has anyone any idea what is wrong with code below?

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);
}

You need to enable the I2C bypass mode on the MPU9255 to communicate with the AK8963 directly. Set the BYPASS_EN bit in the INT Pin / Bypass Enable Configuration register (register 55) at the beginning of your program with:

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

See here for a working example: Arduino Sensor 10DOF MPU9255 cannot fetch magnetometer data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM