本帖最后由 power568 于 2016-3-8 15:22 编辑 现在正在学习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); } |
STM32F407 定时器触发DMA 求助大神
【MCU实战经验】基于STM32F407的音频播放器设计
盘古UE-STM32F407工控板原理图
【STM32F429心得\疑问】+STM32F4之FSMC和FMC
STM32F429读取IO口传输的数据速率
STM32F407ZGT6 手摸芯片背部重启
STM32F4 SPI 动作时,软件片选信号被拉高,IO口程序逻辑失控
STM32F401RE NUCLEO求助,串口一直不能进中断
读取STM32F407内部温度传感器值错误
STM32F429多路内部ADC独立采集的办法
用spi配置9250为iic master可以读出地磁了吗
是的,已经给出解决方案...
对的,就是把上面的延时都适当的加大一点即可...