现在正在学习mpu9250,使用STM32F4+MPU9250, 通过SPI接口方式通讯,现在已经可以正常读取加速度和陀螺仪数据,但是磁力计无法读取,根据网上资料更改的程序:http://www.amobbs.com/thread-5579616-1-1.html, 求大侠指点!!!
附上程序:
UNS8 AkWriteReg(UNS8 const regAddr, UNS8 const value)
{
OS_ERR err;
UNS8 sta = TRUE, tmp;
UNS16 retrytime = SPI_WAIT_I2C_TIME;
SpiMpuWriteReg(I2C_SLV4_ADDR, AK8963_I2C_ADDR);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
SpiMpuWriteReg(I2C_SLV4_REG, regAddr);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
SpiMpuWriteReg(I2C_SLV4_DO, value);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
SpiMpuWriteReg(I2C_SLV4_CTRL, I2C_SLV4_EN_VAL);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
do
{
SpiMpuReadReg(I2C_MSTR_STA, &tmp);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
}while( (--retryTime) && (0==(tmp&I2C_SLV4_DONE_VAL)) );
if (0 == retryTime)
{
sta = FALSE;
}
return sta;
}
UNS8 AkReadReg(UNS8 const regAddr, UNS8 *const pData)
{
OS_ERR err;
UNS8 sta = TRUE, tmp;
UNS16 retryTime = SPI_WAIT_I2C_TIME;
if (NULL == pData)
{
return FALSE;
}
SpiMpuWriteReg(I2C_SLV4_ADDR, AK8963_I2C_ADDR | SPI_MPU_READ_CMD);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
SpiMpuWriteReg(I2C_SLV4_REG, regAddr);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
SpiMpuWriteReg(I2C_SLV4_CTRL, I2C_SLV4_EN_VAL);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
do
{
SpiMpuReadReg(I2C_MSTR_STA, &tmp);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
}while( (--retryTime) && (0==(tmp&I2C_SLV4_DONE_VAL)) );
if (0 != retryTime)
{
SpiMpuReadReg(I2C_SLV4_DI, pData);
}
else
{
sta = FALSE;
}
return sta;
}
void Ak8963Init( void )
{
OS_ERR err;
UNS8 tmp;
AkReadReg(AK8963_WIA, &tmp);//// ref smartImu
if (AK8963_WIA_VAL != tmp)
{
tmp ++;
}
AkWriteReg(0x0B, 0x01);
OSTimeDlyHMSM(0, 0, 0, 100, OS_OPT_TIME_HMSM_STRICT, &err);
AkWriteReg(0x0A, 0x10);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
AkWriteReg(0x0A, 0x1F);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
//// for test
AkReadReg(0x00, &sAk8963AdjData.XMagnAdj);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
AkReadReg(0x10, &sAk8963AdjData.XMagnAdj);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
AkReadReg(0x11, &sAk8963AdjData.YMagnAdj);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
AkReadReg(0x12, &sAk8963AdjData.ZMagnAdj);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
AkWriteReg(0x0A, 0x10);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
SpiMpuWriteReg(0x24, 0x5D);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
SpiMpuWriteReg(0x25, SPI_MPU_READ_CMD | AK8963_I2C_ADDR);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
SpiMpuWriteReg(0x26, 0x02);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
SpiMpuWriteReg(0x27, 0x80 | 0x08);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
AkWriteReg(0x0A, 0x16);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
SpiMpuWriteReg(I2C_SLV4_CTRL, 0x01);
OSTimeDlyHMSM(0, 0, 0, 1, OS_OPT_TIME_HMSM_STRICT, &err);
SpiMpuWriteReg(0x67, 0x81);
OSTimeDlyHMSM(0, 0, 0, 100, OS_OPT_TIME_HMSM_STRICT, &err);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
是的,已经给出解决方案...
一周热门 更多>