IIC地址控制
static void MPU_ADDR_CTRL(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB, GPIO_Pin_14);
}
IIC引脚初始化
void MPU_IIC_Init(void)
{
#if ENABLE_I2C_PB10_PB11
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_11);
#else
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_OUT ;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_15);
#endif
}
MPU初始化
static uint8_t MPU_Basic_Init(void)
{
uint8_t res = 0;
MPU_ADDR_CTRL();
MPU_IIC_Init();
MPU_Delay_ms(10);
MPU_Write_Byte(MPU9250_ADDR, MPU_PWR_MGMT1_REG, 0X80);
MPU_Delay_ms(100);
MPU_Write_Byte(MPU9250_ADDR, MPU_PWR_MGMT1_REG, 0X00);
MPU_Set_Gyro_Fsr(1);
MPU_Set_Accel_Fsr(0);
MPU_Set_Rate(50);
MPU_Write_Byte(MPU9250_ADDR, MPU_INT_EN_REG, 0X00);
MPU_Write_Byte(MPU9250_ADDR, MPU_USER_CTRL_REG, 0X00);
MPU_Write_Byte(MPU9250_ADDR, MPU_FIFO_EN_REG, 0X00);
MPU_Write_Byte(MPU9250_ADDR, MPU_INTBP_CFG_REG, 0X82);
res = MPU_Read_Byte(MPU9250_ADDR, MPU_DEVICE_ID_REG);
printf("MPU9250 RD ID=0x%02X\n", res);
if (res == MPU9250_ID)
{
MPU_Write_Byte(MPU9250_ADDR, MPU_PWR_MGMT1_REG, 0X01);
MPU_Write_Byte(MPU9250_ADDR, MPU_PWR_MGMT2_REG, 0X00);
MPU_Set_Rate(50);
}
else
return 1;
res = MPU_Read_Byte(AK8963_ADDR, MAG_WIA);
printf("AK8963 RD ID=0x%02X\n", res);
if (res == AK8963_ID)
{
MPU_Write_Byte(AK8963_ADDR, MAG_CNTL1, 0X11);
}
else
return 1;
MPU_Delay_ms(30);
return 0;
}