MPU9255 上的磁力计没有数据 [英] No data from magnetometer on MPU9255

查看:37
本文介绍了MPU9255 上的磁力计没有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从 MPU9255 上的磁力计读取数据时遇到问题.加速度计和陀螺仪工作正常,但我没有收到来自磁力计的任何数据.我正在研究 STM32F103 目标.

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

推荐答案

需要在MPU9255上开启I2C旁路模式才能直接与AK8963通信.在程序开始时设置 INT Pin/Bypass Enable Configuration 寄存器(寄存器 55)中的 BYPASS_EN 位:

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

参见此处的工作示例:Arduino Sensor 10DOF MPU9255 无法获取磁力计数据

这篇关于MPU9255 上的磁力计没有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆