MPU6050与51单片机通信并计算俯仰角,反转角,航向角的计算代码,波特率9600
时间: 2023-06-01 15:04:40 浏览: 168
以下是MPU6050与51单片机通信并计算俯仰角、反转角、航向角的计算代码,波特率为9600:
```
#include <reg51.h>
#define MPU_ADDR 0x68 // MPU6050的I2C地址
#define PI 3.14159265358979323846 // 圆周率
sbit SDA = P1^7; // I2C通信SDA线
sbit SCL = P1^6; // I2C通信SCL线
void delay_ms(unsigned int ms) // 延时函数
{
unsigned int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 110; j++);
}
void I2C_Start() // I2C通信开始
{
SDA = 1;
SCL = 1;
delay_ms(1);
SDA = 0;
delay_ms(1);
SCL = 0;
}
void I2C_Stop() // I2C通信结束
{
SDA = 0;
SCL = 1;
delay_ms(1);
SDA = 1;
delay_ms(1);
}
void I2C_WriteByte(unsigned char dat) // I2C通信写入一个字节
{
unsigned char i;
for (i = 0; i < 8; i++)
{
SDA = (bit)(dat & 0x80);
SCL = 1;
delay_ms(1);
SCL = 0;
dat <<= 1;
delay_ms(1);
}
SDA = 1;
SCL = 1;
delay_ms(1);
SCL = 0;
}
unsigned char I2C_ReadByte() // I2C通信读取一个字节
{
unsigned char i, dat = 0;
SDA = 1;
for (i = 0; i < 8; i++)
{
SCL = 1;
delay_ms(1);
dat = (dat << 1) | SDA;
SCL = 0;
delay_ms(1);
}
return dat;
}
void I2C_Ack() // I2C通信发送ACK信号
{
SDA = 0;
delay_ms(1);
SCL = 1;
delay_ms(1);
SCL = 0;
delay_ms(1);
SDA = 1;
}
void I2C_NAck() // I2C通信发送NAck信号
{
SDA = 1;
delay_ms(1);
SCL = 1;
delay_ms(1);
SCL = 0;
delay_ms(1);
}
void I2C_WriteReg(unsigned char reg, unsigned char dat) // I2C通信写入寄存器
{
I2C_Start();
I2C_WriteByte(MPU_ADDR << 1);
I2C_WriteByte(reg);
I2C_WriteByte(dat);
I2C_Stop();
}
unsigned char I2C_ReadReg(unsigned char reg) // I2C通信读取寄存器
{
unsigned char dat;
I2C_Start();
I2C_WriteByte(MPU_ADDR << 1);
I2C_WriteByte(reg);
I2C_Start();
I2C_WriteByte((MPU_ADDR << 1) | 1);
dat = I2C_ReadByte();
I2C_NAck();
I2C_Stop();
return dat;
}
void I2C_ReadRegs(unsigned char reg, unsigned char *buf, unsigned char len) // I2C通信读取多个寄存器
{
unsigned char i;
I2C_Start();
I2C_WriteByte(MPU_ADDR << 1);
I2C_WriteByte(reg);
I2C_Start();
I2C_WriteByte((MPU_ADDR << 1) | 1);
for (i = 0; i < len-1; i++)
{
buf[i] = I2C_ReadByte();
I2C_Ack();
}
buf[len-1] = I2C_ReadByte();
I2C_NAck();
I2C_Stop();
}
void InitMPU() // 初始化MPU6050
{
I2C_WriteReg(0x6B, 0x00); // 退出睡眠模式
I2C_WriteReg(0x1B, 0x08); // 设置陀螺仪满刻度范围为±500°/s
I2C_WriteReg(0x1C, 0x08); // 设置加速度计满刻度范围为±4g
}
double GetAngle(unsigned char H, unsigned char L) // 获取角度值
{
short raw = (H << 8) | L; // 组合高位和低位
double angle = (double)raw / 16384.0; // 计算角度值
return angle;
}
void main()
{
unsigned char buf[14]; // 存放MPU6050数据的缓存
double ax, ay, az, gx, gy, gz; // 加速度计和陀螺仪的原始数据
double pitch, roll, yaw; // 俯仰角、反转角、航向角
unsigned char i;
InitMPU(); // 初始化MPU6050
TMOD = 0x20; // 定时器1工作在方式2,8位自动重装载
TH1 = 0xFD; // 波特率9600,定时器1初值
TL1 = 0xFD; // 波特率9600,定时器1初值
SCON = 0x50; // 串口工作在模式1,波特率可变
PCON = 0x00; // 波特率可变
TR1 = 1; // 启动定时器1
while (1)
{
I2C_ReadRegs(0x3B, buf, 14); // 读取MPU6050数据
ax = GetAngle(buf[0], buf[1]); // 获取加速度计X轴数据
ay = GetAngle(buf[2], buf[3]); // 获取加速度计Y轴数据
az = GetAngle(buf[4], buf[5]); // 获取加速度计Z轴数据
gx = GetAngle(buf[8], buf[9]); // 获取陀螺仪X轴数据
gy = GetAngle(buf[10], buf[11]); // 获取陀螺仪Y轴数据
gz = GetAngle(buf[12], buf[13]); // 获取陀螺仪Z轴数据
pitch = atan2(ax, sqrt(ay*ay + az*az)) * 180.0 / PI; // 计算俯仰角
roll = atan2(ay, sqrt(ax*ax + az*az)) * 180.0 / PI; // 计算反转角
yaw = atan2(-gz, sqrt(gx*gx + gy*gy)) * 180.0 / PI; // 计算航向角
SBUF = pitch; // 发送俯仰角数据
while (!TI); // 等待发送完成
TI = 0; // 清除发送完成标志
for (i = 0; i < 100; i++); // 等待一段时间
SBUF = roll; // 发送反转角数据
while (!TI); // 等待发送完成
TI = 0; // 清除发送完成标志
for (i = 0; i < 100; i++); // 等待一段时间
SBUF = yaw; // 发送航向角数据
while (!TI); // 等待发送完成
TI = 0; // 清除发送完成标志
for (i = 0; i < 100; i++); // 等待一段时间
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)