[mw_shl_code=c,true]
#include "mpu9150.h"
unsigned char PWR_MGMT_1_BITS=0x00,PWR_MGMT_2_BITS=0x00,ACC_CONFIG_BITS=0x00,GYR_CONFIG_BITS=0x00,CONFIG_BITS=0x00,SMPRT_DIV_BITS=0x00;
/********************************************************************************************************
** 函数名称: MPU_Init
** 功能描述: MPU9150初始化
********************************************************************************************************/
Status IMU_Init(void)
{
/*Step1*/
// Set_Bit(PWR_MGMT_1_BITS,DEVICE_RESET);/*复位IMU*/
// if (I2C_OK!=I2C1_Write_NBytes(SlvAddr,PWR_MGMT_1,1,&
WR_MGMT_1_BITS)){return Error;}
// Delay_ms(0xffff);
/*Step2*/
Clr_All_BITs(PWR_MGMT_1_BITS);/*唤醒IMU*/
if (I2C_OK!=I2C1_Write_NBytes(SlvAddr,PWR_MGMT_1,1,&
WR_MGMT_1_BITS)){return Error;}
/*Step3*/
Set_Bit(PWR_MGMT_1_BITS,CLKSEL_0);/*PLL,使用陀螺仪X轴做参考比默认内部8MHz系统时钟源精确点*/
if (I2C_OK!=I2C1_Write_NBytes(SlvAddr,PWR_MGMT_1,1,&
WR_MGMT_1_BITS)){return Error;}
/*Step3*/
if (I2C_OK!=I2C1_Write_NBytes(SlvAddr,PWR_MGMT_2,1,&
WR_MGMT_2_BITS)){return Error;}
/*Step3*/
Set_Bit(SMPRT_DIV_BITS,4);/*200Hz设置陀螺仪采样分频系数 Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV)*/
if (I2C_OK!=I2C1_Write_NBytes(SlvAddr,SMPLRT_DIV,1,&SMPRT_DIV_BITS)){return Error;}
/*Step4*/
Set_Bit(CONFIG_BITS,DLPF_CFG_2);/*设置DLPF数字低通滤波器ACC【Bandwidth(21Hz) Delay(8.5ms)】,GYR【Bandwidth(20Hz) Delay(8.3ms)】,Fs (1kHz)*/
if (I2C_OK!=I2C1_Write_NBytes(SlvAddr,CONFIG,1,&CONFIG_BITS)){return Error;}
/*Step3*/
Set_Bit(GYR_CONFIG_BITS,FS_SEL_0|FS_SEL_1);/*默认11b,GYR量程±2000°/s*/
if (I2C_OK!=I2C1_Write_NBytes(SlvAddr,GYR_CONFIG,1,&GYR_CONFIG_BITS)){return Error;}
/*Step4*/
Clr_All_BITs(ACC_CONFIG_BITS);/*设置00b,ACC量程±2G*/
ACC_CONFIG_BITS=HPF_0HZ;/*设置00b, */
if (I2C_OK!=I2C1_Write_NBytes(SlvAddr,ACC_CONFIG,1,&ACC_CONFIG_BITS)){return Error;}
return Success;
}
/********************************************************************************************************
** 函数名称: Get_ACC_Raw
** 功能描述: 获取ACC原始值
********************************************************************************************************/
Status Get_ACC_Raw(short *raw)
{
unsigned char tmp[6];
if (I2C_OK!=I2C1_Read_NBytes(SlvAddr,ACC_XOUT_H,6,tmp))
{
return Error;
}
raw[0] = (short)(tmp[0] << 8 | tmp[1]);
raw[1] = (short)(tmp[2] << 8 | tmp[3]);
raw[2] = (short)(tmp[4] << 8 | tmp[5]);
//printf("%d,",raw[0]);
return Success;
}
/********************************************************************************************************
** 函数名称: Get_GYR_Raw
** 功能描述: 获取GYR原始值
********************************************************************************************************/
Status Get_GYR_Raw(short *raw)
{
unsigned char tmp[6];
if (I2C_OK!=I2C1_Read_NBytes(SlvAddr,GYR_XOUT_H,6,tmp))
{
return Error;
}
raw[0] = (short)(tmp[0] << 8 | tmp[1]);
raw[1] = (short)(tmp[2] << 8 | tmp[3]);
raw[2] = (short)(tmp[4] << 8 | tmp[5]);
return Success;
}
///********************************************************************************************************
//** 函数名称: Get_TMP_Ra
//** 功能描述: 获取TMP原始值
//********************************************************************************************************/
//Status Get_TMP_Raw(short *raw)
//{
// unsigned char tmp[2];
// if (I2C_OK!=I2C1_Read_NBytes(SlvAddr,TMP_OUT_H,2,tmp))
// {
// return Error;
// }
// *raw= (tmp[0] << 8) | tmp[1];
// return Success;
//}
/********************************************************************************************************
** 函数名称: Delay
** 功能描述: 粗略延时
********************************************************************************************************/
void Delay_ms(unsigned int tick)
{
unsigned char j,k;
for(j=0;j<tick;j++)
for(k=0;k<15;k++);
}
[/mw_shl_code]