本帖最后由 sf116 于 2021-9-2 17:12 编辑
基于i2c_master_polling例程封装多字节读写函数,
static en_result_t I2C_MPU_Read(uint8_t u8DevAddr, uint8_t u8MemAddr, uint8_t *pu8Data, uint32_t u32Size, uint32_t u32TimeOut)
{
en_result_t enRet;
enRet = I2C_Master_Transmit(u8DevAddr, &u8MemAddr, 1, u32TimeOut);
if(Ok == enRet)
{
enRet = I2C_Master_Receive(u8DevAddr, pu8Data, u32Size, u32TimeOut);
}
return enRet;
}
static en_result_t I2C_MPU_Write(uint16_t u8DevAddr, uint8_t u8MemAddr, uint8_t *pu8Data, uint32_t u32Size, uint32_t u32TimeOut)
{
en_result_t enRet;
uint8_t u8TxBuf[50], ucIndex = 0;
if(u32Size > 49)//修改发送最大数据长度
{
enRet = Error;
return enRet;
}
u8TxBuf[0] = u8MemAddr;
for(ucIndex = 0; ucIndex < u32Size; ucIndex++)
{
u8TxBuf[ucIndex + 1] = pu8Data[ucIndex];
}
I2C_Master_Transmit(u8DevAddr, u8TxBuf, u32Size + 1, u32TimeOut);
return enRet;
}
//IIC写一个字节
//reg:寄存器地址
//data:数据
//返回值:0,正常
// 其他,错误代码
en_result_t MPU_Write_Byte(uint8_t reg,uint8_t data)
{
en_result_t res;
res = I2C_MPU_Write(MPU_ADDRESS, reg, &data, 1u, TIMEOUT);
return res;
}
//IIC读一个字节
//reg:寄存器地址
//返回值:读到的数据
uint8_t MPU_Read_Byte(uint8_t reg)
{
uint8_t res;
I2C_MPU_Read(MPU_ADDRESS, reg, &res, 1u, TIMEOUT);
//printf("\r\n MPU %X", res);
return res;
}
typedef uint8_t u8;
typedef uint16_t u16;
//设置MPU6050陀螺仪传感器满量程范围
//fsr:0,±250dps;1,±500dps;2,±1000dps;3,±2000dps
//返回值:0,设置成功
// 其他,设置失败
u8 MPU_Set_Gyro_Fsr(u8 fsr)
{
return MPU_Write_Byte(MPU_GYRO_CFG_REG,fsr<<3);//设置陀螺仪满量程范围
}
//设置MPU6050加速度传感器满量程范围
//fsr:0,±2g;1,±4g;2,±8g;3,±16g
//返回值:0,设置成功
// 其他,设置失败
u8 MPU_Set_Accel_Fsr(u8 fsr)
{
return MPU_Write_Byte(MPU_ACCEL_CFG_REG,fsr<<3);//设置加速度传感器满量程范围
}
//设置MPU6050的数字低通滤波器
//lpf:数字低通滤波频率(Hz)
//返回值:0,设置成功
// 其他,设置失败
u8 MPU_Set_LPF(u16 lpf)
{
u8 data=0;
if(lpf>=188)data=1;
else if(lpf>=98)data=2;
else if(lpf>=42)data=3;
else if(lpf>=20)data=4;
else if(lpf>=10)data=5;
else data=6;
return MPU_Write_Byte(MPU_CFG_REG,data);//设置数字低通滤波器
}
//设置MPU6050的采样率(假定Fs=1KHz)
//rate:4~1000(Hz)
//返回值:0,设置成功
// 其他,设置失败
u8 MPU_Set_Rate(u16 rate)
{
u8 data;
if(rate>1000)rate=1000;
if(rate<4)rate=4;
data=1000/rate-1;
data=MPU_Write_Byte(MPU_SAMPLE_RATE_REG,data); //设置数字低通滤波器
return MPU_Set_LPF(rate/2); //自动设置LPF为采样率的一半
}
//得到温度值
//返回值:温度值(扩大了100倍)
short MPU_Get_Temperature(void)
{
u8 buf[2];
short raw;
float temp;
//MPU_Read_Len(MPU_ADDR,MPU_TEMP_OUTH_REG,2,buf);
I2C_MPU_Read(MPU_ADDRESS, MPU_TEMP_OUTH_REG, buf, 2u, TIMEOUT);
raw=((u16)buf[0]<<8)|buf[1];
temp=36.53+((double)raw)/340;
return temp*100;;
}
//得到陀螺仪值(原始值)
//gx,gy,gz:陀螺仪x,y,z轴的原始读数(带符号)
//返回值:0,成功
// 其他,错误代码
u8 MPU_Get_Gyroscope(short *gx,short *gy,short *gz)
{
u8 buf[6];
en_result_t res;
//res=MPU_Read_Len(MPU_ADDR,MPU_GYRO_XOUTH_REG,6,buf);
res = I2C_MPU_Read(MPU_ADDRESS, MPU_GYRO_XOUTH_REG, buf, 6u, TIMEOUT);
if(res== Ok)
{
*gx=((u16)buf[0]<<8)|buf[1];
*gy=((u16)buf[2]<<8)|buf[3];
*gz=((u16)buf[4]<<8)|buf[5];
}
return res;;
}
//得到加速度值(原始值)
//gx,gy,gz:陀螺仪x,y,z轴的原始读数(带符号)
//返回值:0,成功
// 其他,错误代码
u8 MPU_Get_Accelerometer(short *ax,short *ay,short *az)
{
u8 buf[6];
en_result_t res;
//res=MPU_Read_Len(MPU_ADDR,MPU_ACCEL_XOUTH_REG,6,buf);
res = I2C_MPU_Read(MPU_ADDRESS, MPU_ACCEL_XOUTH_REG, buf, 6u, TIMEOUT);
if(res==Ok)
{
*ax=((u16)buf[0]<<8)|buf[1];
*ay=((u16)buf[2]<<8)|buf[3];
*az=((u16)buf[4]<<8)|buf[5];
}
return res;;
}
uint8_t MPU_Init(void)
{
uint8_t res;
//MPU_IIC_Init();//初始化IIC总线
MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X80); //复位MPU6050
Ddl_Delay1ms(100);
MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X00); //唤醒MPU6050
MPU_Set_Gyro_Fsr(3); //陀螺仪传感器,±2000dps
MPU_Set_Accel_Fsr(0); //加速度传感器,±2g
MPU_Set_Rate(50); //设置采样率50Hz
MPU_Write_Byte(MPU_INT_EN_REG,0X00); //关闭所有中断
MPU_Write_Byte(MPU_USER_CTRL_REG,0X00); //I2C主模式关闭
MPU_Write_Byte(MPU_FIFO_EN_REG,0X00); //关闭FIFO
MPU_Write_Byte(MPU_INTBP_CFG_REG,0X80); //INT引脚低电平有效
res=MPU_Read_Byte(MPU_DEVICE_ID_REG);
if(res==MPU_ADDR)//器件ID正确
{
printf("\r\n MPU_ADDR %X", res);
MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X01); //设置CLKSEL,PLL X轴为参考
MPU_Write_Byte(MPU_PWR_MGMT2_REG,0X00); //加速度与陀螺仪都工作
MPU_Set_Rate(50); //设置采样率为50Hz
}else return 1;
return 0;
}
调用:
main:
/* BSP initialization */
BSP_CLK_Init();
BSP_LED_Init();
BSP_KEY_Init();
DDL_PrintfInit(BSP_PRINTF_DEVICE, BSP_PRINTF_BAUDRATE, BSP_PRINTF_PortInit);
printf("\r\n DDL_PrintfInit");
/* Initialize I2C port*/
PORT_SetFunc(I2C_SCL_PORT, I2C_SCL_PIN, I2C_GPIO_SCL_FUNC, Disable);
PORT_SetFunc(I2C_SDA_PORT, I2C_SDA_PIN, I2C_GPIO_SDA_FUNC, Disable);
/* Enable I2C Peripheral*/
PWC_Fcg1PeriphClockCmd(I2C_FCG_USE, Enable);
/* Initialize I2C peripheral and enable function*/
Master_Initialize();
MPU_Init();
while(1)
{
BSP_LED_Toggle(LED_GREEN);
Ddl_Delay1ms(500ul);
MPU_Get_Accelerometer(&aacx,&aacy,&aacz); //得到加速度传感器数据
MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz); //得到陀螺仪数据
printf("\r\n Temp %d Acc %d %d %d Gyro %d %d %d",
MPU_Get_Temperature(), aacx, aacy, aacz, gyrox, gyroy, gyroz);
}
调试结果通过UART查看PE6Log:
|
|