MPU9250九轴运动传感器测试stm32程序
- #include "sys.h"
- #include "MPU9250.h"
- #include "myiic.h"
- #include "delay.h"
- u8 TX_DATA[4]; //显示据缓存区
- u8 BUF[10]; //接收数据缓存区
- short T_X,T_Y,T_Z,T_T; //X,Y,Z轴,温度
- //初始化MPU9250,根据需要请参考pdf进行修改************************
- void Init_MPU9250(void)
- {
- /*
- Single_Write(GYRO_ADDRESS,PWR_M, 0x80); //
- Single_Write(GYRO_ADDRESS,SMPL, 0x07); //
- Single_Write(GYRO_ADDRESS,DLPF, 0x1E); //±2000°
- Single_Write(GYRO_ADDRESS,INT_C, 0x00 ); //
- Single_Write(GYRO_ADDRESS,PWR_M, 0x00); //
- */
-
- IIC_Init(); //配置IIC使用端口
- IIC_Write_One_Byte(GYRO_ADDRESS,PWR_MGMT_1, 0x00); //解除休眠状态
- IIC_Write_One_Byte(GYRO_ADDRESS,SMPLRT_DIV, 0x07);
- IIC_Write_One_Byte(GYRO_ADDRESS,CONFIG, 0x06);
- IIC_Write_One_Byte(GYRO_ADDRESS,GYRO_CONFIG, 0x18);
- IIC_Write_One_Byte(GYRO_ADDRESS,ACCEL_CONFIG, 0x01);
- //----------------
- // Single_Write(GYRO_ADDRESS,0x6A,0x00);//close Master Mode
- }
-
- //******读取MPU9250数据****************************************
- void READ_MPU9250_ACCEL(void)
- {
- BUF[0]=IIC_Read_One_Byte(ACCEL_ADDRESS,ACCEL_XOUT_L);
- BUF[1]=IIC_Read_One_Byte(ACCEL_ADDRESS,ACCEL_XOUT_H);
- T_X= (BUF[1]<<8)|BUF[0];
- T_X/=164; //读取计算X轴数据
- BUF[2]=IIC_Read_One_Byte(ACCEL_ADDRESS,ACCEL_YOUT_L);
- BUF[3]=IIC_Read_One_Byte(ACCEL_ADDRESS,ACCEL_YOUT_H);
- T_Y= (BUF[3]<<8)|BUF[2];
- T_Y/=164; //读取计算Y轴数据
- BUF[4]=IIC_Read_One_Byte(ACCEL_ADDRESS,ACCEL_ZOUT_L);
- BUF[5]=IIC_Read_One_Byte(ACCEL_ADDRESS,ACCEL_ZOUT_H);
- T_Z= (BUF[5]<<8)|BUF[4];
- T_Z/=164; //读取计算Z轴数据
- }
- void READ_MPU9250_GYRO(void)
- {
- BUF[0]=IIC_Read_One_Byte(GYRO_ADDRESS,GYRO_XOUT_L);
- BUF[1]=IIC_Read_One_Byte(GYRO_ADDRESS,GYRO_XOUT_H);
- T_X= (BUF[1]<<8)|BUF[0];
- T_X/=16.4; //读取计算X轴数据
- BUF[2]=IIC_Read_One_Byte(GYRO_ADDRESS,GYRO_YOUT_L);
- BUF[3]=IIC_Read_One_Byte(GYRO_ADDRESS,GYRO_YOUT_H);
- T_Y= (BUF[3]<<8)|BUF[2];
- T_Y/=16.4; //读取计算Y轴数据
- BUF[4]=IIC_Read_One_Byte(GYRO_ADDRESS,GYRO_ZOUT_L);
- BUF[5]=IIC_Read_One_Byte(GYRO_ADDRESS,GYRO_ZOUT_H);
- T_Z= (BUF[5]<<8)|BUF[4];
- T_Z/=16.4; //读取计算Z轴数据
- // BUF[6]=Single_Read(GYRO_ADDRESS,TEMP_OUT_L);
- // BUF[7]=Single_Read(GYRO_ADDRESS,TEMP_OUT_H);
- // T_T=(BUF[7]<<8)|BUF[6];
- // T_T = 35+ ((double) (T_T + 13200)) / 280;// 读取计算出温度
- }
- void READ_MPU9250_MAG(void)
- {
- IIC_Write_One_Byte(GYRO_ADDRESS,0x37,0x02);//turn on Bypass Mode
- delay_ms(10);
- IIC_Write_One_Byte(MAG_ADDRESS,0x0A,0x01);
- delay_ms(10);
- BUF[0]=IIC_Read_One_Byte (MAG_ADDRESS,MAG_XOUT_L);
- BUF[1]=IIC_Read_One_Byte (MAG_ADDRESS,MAG_XOUT_H);
- T_X=(BUF[1]<<8)|BUF[0];
- BUF[2]=IIC_Read_One_Byte(MAG_ADDRESS,MAG_YOUT_L);
- BUF[3]=IIC_Read_One_Byte(MAG_ADDRESS,MAG_YOUT_H);
- T_Y= (BUF[3]<<8)|BUF[2];
- //读取计算Y轴数据
-
- BUF[4]=IIC_Read_One_Byte(MAG_ADDRESS,MAG_ZOUT_L);
- BUF[5]=IIC_Read_One_Byte(MAG_ADDRESS,MAG_ZOUT_H);
- T_Z= (BUF[5]<<8)|BUF[4];
- //读取计算Z轴数据
- }
复制代码
|
|
2019-1-14 22:16:11
评论
举报
|
|
|