初级会员
- 积分
- 95
- 金钱
- 95
- 注册时间
- 2018-10-13
- 在线时间
- 40 小时
|
1金钱
#include "myiic.h"
#include "mpu6050.h"
u8 MPU_Write_Byte(u8 reg,u8 data)
{
IIC_Start();
IIC_Send_Byte(MPU_ADDR << 1 | WRITE_CMD); //发送器件地址+写命令
if(IIC_Wait_Ack() == False)
{
IIC_Stop();
return False;
}
IIC_Send_Byte(reg); //写寄存器地址
IIC_Wait_Ack();
IIC_Send_Byte(data); //发送数据
if(IIC_Wait_Ack() == False)
{
IIC_Stop();
return False;
}
IIC_Stop();
return True;
}
//reg:寄存器地址
u8 MPU_Read_Byte(u8 reg)
{
u8 res;
IIC_Start();
IIC_Send_Byte(MPU_ADDR << 1 | WRITE_CMD);
IIC_Wait_Ack();
IIC_Send_Byte(reg);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(MPU_ADDR << 1 | READ_CMD);
IIC_Wait_Ack();
res = IIC_Read_Byte(0); //读取数据,发送nACK
IIC_Stop();
return res;
}
//reg:寄存器地址
u8 MPU_Write_Len(u8 addr ,u8 reg,u8 len, u8 *buf)
{
u8 i;
IIC_Start();
IIC_Send_Byte((addr << 1) | WRITE_CMD);
if(IIC_Wait_Ack() == False)
{
IIC_Stop();
return False;
}
IIC_Send_Byte(reg);
IIC_Wait_Ack();
for(i = 0; i < len; i++)
{
IIC_Send_Byte(buf[i]);
if(IIC_Wait_Ack() == False)
{
IIC_Stop();
return False;
}
}
IIC_Stop();
return True;
}
u8 MPU_Read_Len(u8 addr,u8 reg,u8 len,u8 *buf)
{
IIC_Start();
IIC_Send_Byte((addr<<1)|0);//发送器件地址+写命令
if(IIC_Wait_Ack() == False) //等待应答
{
IIC_Stop();
printf("false\r\n");
return False;
}
IIC_Send_Byte(reg); //写寄存器地址
IIC_Wait_Ack(); //等待应答
IIC_Start();
IIC_Send_Byte((addr<<1)|1);//发送器件地址+读命令
IIC_Wait_Ack(); //等待应答
while(len)
{
if(len==1)*buf=IIC_Read_Byte(0);//读数据,发送nACK
else *buf=IIC_Read_Byte(1); //读数据,发送ACK
printf(" buf = %d\r\n",*buf);
len--;
buf++;
}
IIC_Stop(); //产生一个停止条件
return True;
}
//得到陀螺仪值(原始值)
//gx,gy,gz:陀螺仪x,y,z轴的原始读数(带符号)
u8 MPU_Get_Gyroscope(short *gx,short *gy,short *gz)
{
u8 buf[6];
u8 res;
res = MPU_Read_Len(MPU_ADDR,MPU_GYRO_XOUTH_REG,6,buf);
if(res == True)
{
*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轴的原始读数(带符号)
u8 MPU_Get_Accelerometer(short *ax,short *ay,short *az)
{
u8 buf[6];
u8 res;
res= MPU_Read_Len(MPU_ADDR,MPU_ACCEL_XOUTH_REG,6,buf);
if(res == True)
{
*ax = ((u16)buf[0] << 8) | buf[1];
*ay = ((u16)buf[2] << 8) | buf[3];
*az = ((u16)buf[4] << 8) | buf[5];
}
return res;
}
//得到温度值
//返回值:温度值(扩大了100倍)
short MPU_Get_temprature(void)
{
u8 buf[2];
short raw;
float temp;
MPU_Read_Len(MPU_ADDR,MPU_TEMP_OUTH_REG,2,buf);
raw = ((u16)buf[0] << 8 |buf[1]);
temp = 36.53 +((double)raw/340);
return temp*100;
}
//设置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 = 10000 / (rate - 1);
data = MPU_Write_Byte(MPU_SAMPLE_RATE_REG,data);
return MPU_Set_LPF(rate / 2);
}
u8 MPU_Init(void)
{
u8 res;
IICPort_Init();
MPU_Write_Byte(MPU_PWR_MGMT1_REG,0x80); //复位mpu6050
delay_ms(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); //主IIC模式关闭
MPU_Write_Byte(MPU_FIFO_EN_REG,0x00); //关闭FIFO
MPU_Write_Byte(MPU_INTBP_CFG_REG,0x80); //INF引脚低电平有效
res = MPU_Read_Byte(MPU_DEVICE_ID_REG);
printf("ID = %X\r\n",res);
if(res == MPU_ADDR) //器件ID正确
{
MPU_Write_Byte(MPU_PWR_MGMT1_REG,0x01); //设置CLKSEL,PLL,X轴为参考
MPU_Write_Byte(MPU_PWR_MGMT2_REG,0x00); //加速度与陀螺仪都工程
MPU_Set_Rate(50);
}
else
{
return False;
}
return True;
}
#include "stm32f10x.h"
#include "mpu6050.h"
#include "myiic.h"
int main(void)
{
short aacx,aacy,aacz;
short gyrox,gyroy,gyroz;
float pitch,roll,yaw;
u8 res;
u8 id;
u8 i = 1;
delay_init();
uart_init(115200);
IICPort_Init();
MPU_Init();
while(1)
{
if(i)
{
res = MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz);
printf("res = %d\r\n",res);
printf("gyrox= %d,gyroy = %d,gyroz = %d",gyrox,gyroy,gyroz);
i--;
}
}
}
|
-
|