MPU9250配置例程.docx
《MPU9250配置例程.docx》由会员分享,可在线阅读,更多相关《MPU9250配置例程.docx(22页珍藏版)》请在冰豆网上搜索。
MPU9250配置例程
MPU9250的SPI读取例程,由一个忘了什么来源的程序改成,只需要改SPI和CS的宏定义,加上延时函数,就可以使用,亲测可用,发上来给那些找不到例程的朋友,配置后的状态在另一个文档里,名为MPU9250刷新速率和单位,程序如有侵权等,请联系…
//MPU9250的陀螺仪单位转换系数:
//#defineMPU9250_Gyro_FS100032.8
//MPU9250的加速度计单位转换系数:
//#defineAcc_FS2g1671.84//tom/ss
//MPU9250磁力计的单位转换系数:
//#defineMega_FS16BIT_Ga6.83/*1uT-10mGass*/
statics16MPU9250_AK8963_ASA[3]={0,0,0};//1T=10000GASS1uT=0.01GASS
//定义MPU9250内部地址
//////////////////////////////////////////////////////////////////////////
//RegisterMapforGyroscopeandAccelerometer
#defineMPU9250_SELF_TEST_X_GYRO0x00
#defineMPU9250_SELF_TEST_Y_GYRO0x01
#defineMPU9250_SELF_TEST_Z_GYRO0x02
#defineMPU9250_SELF_TEST_X_ACCEL0x0D
#defineMPU9250_SELF_TEST_Y_ACCEL0x0E
#defineMPU9250_SELF_TEST_Z_ACCEL0x0F
#defineMPU9250_XG_OFFSET_H0x13
#defineMPU9250_XG_OFFSET_L0x14
#defineMPU9250_YG_OFFSET_H0x15
#defineMPU9250_YG_OFFSET_L0x16
#defineMPU9250_ZG_OFFSET_H0x17
#defineMPU9250_ZG_OFFSET_L0x18
#defineMPU9250_SMPLRT_DIV0x19//陀螺仪采样率,典型值:
0x07(125Hz)
#defineMPU9250_CONFIG0x1A//低通滤波频率,典型值:
0x06(5Hz)
#defineMPU9250_GYRO_CONFIG0x1B//陀螺仪自检及测量范围,典型值:
0x18(不自检,2000deg/s)
#defineMPU9250_ACCEL_CONFIG0x1C//加速计自检、测量范围及高通滤波频率,典型值:
0x01(不自检,2G,5Hz)
#defineMPU9250_ACCEL_CONFIG20x1D
#defineMPU9250_LP_ACCEL_ODR0x1E
#defineMPU9250_WOM_THR0x1F
#defineMPU9250_FIFO_EN0x23
#defineMPU9250_I2C_MST_CTRL0x24
#defineMPU9250_I2C_SLV0_ADDR0x25
#defineMPU9250_I2C_SLV0_REG0x26
#defineMPU9250_I2C_SLV0_CTRL0x27
#defineMPU9250_I2C_SLV1_ADDR0x28
#defineMPU9250_I2C_SLV1_REG0x29
#defineMPU9250_I2C_SLV1_CTRL0x2A
#defineMPU9250_I2C_SLV2_ADDR0x2B
#defineMPU9250_I2C_SLV2_REG0x2C
#defineMPU9250_I2C_SLV2_CTRL0x2D
#defineMPU9250_I2C_SLV3_ADDR0x2E
#defineMPU9250_I2C_SLV3_REG0x2F
#defineMPU9250_I2C_SLV3_CTRL0x30
#defineMPU9250_I2C_SLV4_ADDR0x31
#defineMPU9250_I2C_SLV4_REG0x32
#defineMPU9250_I2C_SLV4_DO0x33
#defineMPU9250_I2C_SLV4_CTRL0x34
#defineMPU9250_I2C_SLV4_DI0x35
#defineMPU9250_I2C_MST_STATUS0x36
#defineMPU9250_INT_PIN_CFG0x37
#defineMPU9250_INT_ENABLE0x38
#defineMPU9250_INT_STATUS0x3A
#defineMPU9250_ACCEL_XOUT_H0x3B
#defineMPU9250_ACCEL_XOUT_L0x3C
#defineMPU9250_ACCEL_YOUT_H0x3D
#defineMPU9250_ACCEL_YOUT_L0x3E
#defineMPU9250_ACCEL_ZOUT_H0x3F
#defineMPU9250_ACCEL_ZOUT_L0x40
#defineMPU9250_TEMP_OUT_H0x41
#defineMPU9250_TEMP_OUT_L0x42
#defineMPU9250_GYRO_XOUT_H0x43
#defineMPU9250_GYRO_XOUT_L0x44
#defineMPU9250_GYRO_YOUT_H0x45
#defineMPU9250_GYRO_YOUT_L0x46
#defineMPU9250_GYRO_ZOUT_H0x47
#defineMPU9250_GYRO_ZOUT_L0x48
#defineMPU9250_EXT_SENS_DATA_000x49
#defineMPU9250_EXT_SENS_DATA_010x4A
#defineMPU9250_EXT_SENS_DATA_020x4B
#defineMPU9250_EXT_SENS_DATA_030x4C
#defineMPU9250_EXT_SENS_DATA_040x4D
#defineMPU9250_EXT_SENS_DATA_050x4E
#defineMPU9250_EXT_SENS_DATA_060x4F
#defineMPU9250_EXT_SENS_DATA_070x50
#defineMPU9250_EXT_SENS_DATA_080x51
#defineMPU9250_EXT_SENS_DATA_090x52
#defineMPU9250_EXT_SENS_DATA_100x53
#defineMPU9250_EXT_SENS_DATA_110x54
#defineMPU9250_EXT_SENS_DATA_120x55
#defineMPU9250_EXT_SENS_DATA_130x56
#defineMPU9250_EXT_SENS_DATA_140x57
#defineMPU9250_EXT_SENS_DATA_150x58
#defineMPU9250_EXT_SENS_DATA_160x59
#defineMPU9250_EXT_SENS_DATA_170x5A
#defineMPU9250_EXT_SENS_DATA_180x5B
#defineMPU9250_EXT_SENS_DATA_190x5C
#defineMPU9250_EXT_SENS_DATA_200x5D
#defineMPU9250_EXT_SENS_DATA_210x5E
#defineMPU9250_EXT_SENS_DATA_220x5F
#defineMPU9250_EXT_SENS_DATA_230x60
#defineMPU9250_I2C_SLV0_DO0x63
#defineMPU9250_I2C_SLV1_DO0x64
#defineMPU9250_I2C_SLV2_DO0x65
#defineMPU9250_I2C_SLV3_DO0x66
#defineMPU9250_I2C_MST_DELAY_CTRL0x67
#defineMPU9250_SIGNAL_PATH_RESET0x68
#defineMPU9250_MOT_DETECT_CTRL0x69
#defineMPU9250_USER_CTRL0x6A
#defineMPU9250_PWR_MGMT_10x6B//电源管理,典型值:
0x00(正常启用)
#defineMPU9250_PWR_MGMT_20x6C
#defineMPU9250_FIFO_COUNTH0x72
#defineMPU9250_FIFO_COUNTL0x73
#defineMPU9250_FIFO_R_W0x74
#defineMPU9250_WHO_AM_I0x75//ID寄存器(默认数值0x71,只读)
#defineMPU9250_XA_OFFSET_H0x77
#defineMPU9250_XA_OFFSET_L0x78
#defineMPU9250_YA_OFFSET_H0x7A
#defineMPU9250_YA_OFFSET_L0x7B
#defineMPU9250_ZA_OFFSET_H0x7D
#defineMPU9250_ZA_OFFSET_L0x7E
//
#defineMPU9250_I2C_READ0x80
//Magnetometerregistermaps
#defineMPU9250_AK8963_WIA0x00
#defineMPU9250_AK8963_INFO0x01
#defineMPU9250_AK8963_ST10x02
#defineMPU9250_AK8963_XOUT_L0x03
#defineMPU9250_AK8963_XOUT_H0x04
#defineMPU9250_AK8963_YOUT_L0x05
#defineMPU9250_AK8963_YOUT_H0x06
#defineMPU9250_AK8963_ZOUT_L0x07
#defineMPU9250_AK8963_ZOUT_H0x08
#defineMPU9250_AK8963_ST20x09
#defineMPU9250_AK8963_CNTL0x0A
#defineMPU9250_AK8963_CNTL20x0B
#defineMPU9250_AK8963_RSV0x0B//DONOTACCESS
#defineMPU9250_AK8963_ASTC0x0C
#defineMPU9250_AK8963_TS10x0D//DONOTACCESS
#defineMPU9250_AK8963_TS20x0E//DONOTACCESS
#defineMPU9250_AK8963_I2CDIS0x0F
#defineMPU9250_AK8963_ASAX0x10
#defineMPU9250_AK8963_ASAY0x11
#defineMPU9250_AK8963_ASAZ0x12
#defineMPU9250_AK8963_I2C_ADDR0x0C
#defineMPU9250_AK8963_POWER_DOWN0x10
#defineMPU9250_AK8963_FUSE_ROM_ACCESS0x1F
#defineMPU9250_AK8963_SINGLE_MEASUREMENT0x11
#defineMPU9250_AK8963_CONTINUOUS_MEASUREMENT0x16//MODE2刷新速率100Hz
#defineMPU9250_AK8963_DATA_READY(0x01)
#defineMPU9250_AK8963_DATA_OVERRUN(0x02)
#defineMPU9250_AK8963_OVERFLOW(0x80)
#defineMPU9250_AK8963_DATA_ERROR(0x40)
#defineMPU9250_AK8963_CNTL2_SRST0x01
//
#defineMPU9250_I2C_SLV4_EN0x80
#defineMPU9250_I2C_SLV4_DONE0x40
#defineMPU9250_I2C_SLV4_NACK0x10
//
#defineMPU9250_I2C_IF_DIS(0x10)
#defineMPU9250_I2C_MST_EN(0x20)
#defineMPU9250_FIFO_RST(0x04)
#defineMPU9250_FIFO_ENABLE(0x40)
//
#defineMPU9250_RESET0x80
#defineMPU9250_CLOCK_MASK0xF8
#defineMPU9250_CLOCK_INTERNAL0x00
#defineMPU9250_CLOCK_PLL0x01
#defineMPU9250_CLOCK_PLLGYROZ0x03
#defineMPU9250_FS_SEL_MASK0xE7
#defineMPU9250_SLEEP_MASK0x40
//
#defineMPU9250_XYZ_GYRO0xC7
#defineMPU9250_XYZ_ACCEL0xF8
//
#defineMPU9250_RAW_RDY_EN(0x01)
#defineMPU9250_RAW_DATA_RDY_INT(0x01)
#defineMPU9250_FIFO_OVERFLOW(0x10)
//
#defineMPU9250_INT_ANYRD_2CLEAR(0x10)
#defineMPU9250_LATCH_INT_EN(0x20)
//
#defineMPU9250_MAX_FIFO(1024)
#defineMPU9250_FIFO_SIZE_1024(0x40)
#defineMPU9250_FIFO_SIZE_2048(0x80)
#defineMPU9250_FIFO_SIZE_4096(0xC0)
#defineMPU9250_TEMP_OUT(0x80)
#defineMPU9250_GYRO_XOUT(0x40)
#defineMPU9250_GYRO_YOUT(0x20)
#defineMPU9250_GYRO_ZOUT(0x10)
#defineMPU9250_ACCEL(0x08)
//////////////////////////////////////////////////////////////////////////
#defineSMPLRT_DIV0
#defineMPU9250_SPIx_ADDR0x00
/******************************************************************************/
#defineMPU9250_SPI_ReadWriteByte(x)SPI1_ReadWriteByte(x)
#defineMPU9250_CS_LGPIO_ResetBits(GPIOA,GPIO_Pin_4)
#defineMPU9250_CS_HGPIO_SetBits(GPIOA,GPIO_Pin_4)
/******************************************************************************/
//////////////////////////////////////////////////////////////////////////
enumMPU9250_GYRO_DLPF
{
MPU9250_GYRO_DLPF_250HZ=0,
MPU9250_GYRO_DLPF_184HZ,
MPU9250_GYRO_DLPF_92HZ,
MPU9250_GYRO_DLPF_41HZ,
MPU9250_GYRO_DLPF_20HZ,
MPU9250_GYRO_DLPF_10HZ,
MPU9250_GYRO_DLPF_5HZ,
MPU9250_GYRO_DLPF_3600HZ,
NUM_GYRO_DLPF
};
enumMPU9250_GYRO_FSR
{
MPU9250_FSR_250DPS=0,
MPU9250_FSR_500DPS,
MPU9250_FSR_1000DPS,
MPU9250_FSR_2000DPS,
MPU9250_NUM_GYRO_FSR
};
enumMPU9250_ACCEL_DLPF
{
MPU9250_ACCEL_DLPF_460HZ=0,
MPU9250_ACCEL_DLPF_184HZ,
MPU9250_ACCEL_DLPF_92HZ,
MPU9250_ACCEL_DLPF_41HZ,
MPU9250_ACCEL_DLPF_20HZ,
MPU9250_ACCEL_DLPF_10HZ,
MPU9250_ACCEL_DLPF_5HZ,
MPU9250_ACCEL_DLPF_460HZ2,
MPU9250_NUM_ACCEL_DLPF
};
enumMPU9250_ACCEL_FSR
{
MPU9250_FSR_2G=0,
MPU9250_FSR_4G,
MPU9250_FSR_8G,
MPU9250_FSR_16G,
MPU9250_NUM_ACCEL_FSR
};
enumMPU9250_CLK
{
MPU9250_CLK_INTERNAL=0,
MPU9250_CLK_PLL,
MPU9250_NUM_CLK
};
//MPU9250_SPI_Write
//MPU9250的SPI写一个字节函数
//reg_addr寄存器地址data要写入的数据
voidMPU9250_SPI_Write(u8reg_addr,u8data)
{
MPU9250_CS_L;
MPU9250_SPI_ReadWriteByte(reg_addr);
MPU9250_SPI_ReadWriteByte(data);
MPU9250_CS_H;
}
//MPU9250_SPI_Writes
//MPU9250的SPI写多个字节函数
//reg_addr寄存器地址len字节数*data要写入的数据存放的数组
voidMPU9250_SPI_Writes(u8reg_addr,u8len,u8*data)
{
u32i=0;
MPU9250_CS_L;
MPU9250_SPI_ReadWriteByte(reg_addr);
while(i{
MPU9250_SPI_ReadWriteByte(data[i++]);
}
MPU9250_CS_H;
}
//MPU9250_SPI_Read
//MPU9250的读一个字节函数
//reg_addr寄存器地址
u8MPU9250_SPI_Read(u8reg_addr)
{
u8dummy=0;
u8data=0;
MPU9250_CS_L;
MPU9250_SPI_ReadWriteByte(0x80|reg_addr);
data=MPU9250_SPI_ReadWriteByte(dummy);
MPU9250_CS_H;
returndata;
}
//MPU9250_SPI_Reads
//MPU9250的读多个字节函数
//reg_addr寄存器地址len读取字节数*data数据存放数组
voidMPU9250_SPI_Reads(u8reg_addr,u8len,u8*data)
{
u32i=0;
u8dummy=0x00;
MPU9250_CS_L;
MPU9250_SPI_ReadWriteByte(MPU9250_I2C_READ|reg_addr);
while(idata[i++]=MPU9250_SPI_ReadWriteByte(dummy);
}
MPU9250_CS_H;
}
//以下四个函数,作用是设置磁力计
intMPU9250_AK8963_SPI_Read(u8akm_addr,u8reg_addr,u8*data)
{
u8status=0;
u32timeout=0;
MPU9250_SPI_Writes(MPU9250_I2C_SLV4_REG,1,®_addr);
delay_ms
(1);
reg_addr=akm_addr|MPU9250_I2C_READ;
MPU9250_SPI_Writes(MPU9250_I2C_SLV4_ADDR,1,®_addr);
delay_ms
(1);
reg_addr=MPU9250_I2C_SLV4_EN;
MPU9250_SPI_Writes(MPU9250_I2C_SLV4_CTRL,1,®_addr);
delay_ms
(1);
do
{
if(timeout++>50)
{
return-2;
}
MPU9250_SPI_Reads(MPU9250_I2C_MST_STATUS,1,&status);
delay_ms
(1);
}
while((status&MPU9250_I2C_SLV4_DONE)==0);
MPU9250_SPI_Reads(MPU9250_I2C_SLV4_DI,1,data);
return0;
}
intMPU9250_AK8963_SPI_Reads(u8akm_addr,u8reg_addr,u8len,u8*data)