求助:传感器初始化顺序调整了也没有变化,while(1)中循有if{if{}},为何执行不同,是delay_ms的问题吗?
C语言程序如下:
#include "stdio.h"//标准输入输出库
#include "string.h"//字符串库
#include "stdlib.h"//常用的系统函数库
#include "sys.h"//系统中断分组库
#include "delay.h"//延时函数库
#include "usart.h"//串口设置库
#include "mpu6050.h"//MPU6050驱动库
#include "inv_mpu.h"//陀螺仪驱动库
#include "inv_mpu_dmp_motion_driver.h" //DMP姿态解读库
float Weight = 0;
u8 tmp_buf[33]; //字符串数组
struct MPU6050 //MPU6050结构体
{
u8 flag; //采集成功标志位
u8 speed; //上报速度
}mpu6050; //唯一结构体变量
float pitch,roll,yaw;
short aacx,aacy,aacz; //加速度传感器原始数据
short gyrox,gyroy,gyroz; //陀螺仪原始数据
short temp; //温度
int main(void)
{
unsigned short timeCount = 0; //发送间隔变量
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断优先级分组函数
delay_init(); //延时函数初始化
uart_init(115200); //串口初始化为115200
Init_Hx711();//初始化HX711
delay_ms(2000);//等待感器稳定
Get_Maopi();
delay_ms(100);
MPU_Init(); //初始化MPU6050
while(mpu_dmp_init()) //初始化mpu_dmp库
{
printf("It's NO\r\n"); //串口初始化失败上报
}
printf("It's OK\r\n"); //串口初始化成功上报
delay_ms(999); //延时初界面显示
mpu6050.flag = 0; //采集成功标志位初始化
mpu6050.speed = 0; //上报速度初始化
while(1) //主循环
{
if(timeCount == 0)
{
/********** 压力传感器获取数据**************/
Weight = Get_Weight();
printf("%0.3f N \r\n",Weight*9.8); //串口显示重力
/********** mpu6050传感器获取数据**************/
mpu_dmp_get_data( &pitch,&roll,&yaw);
temp=MPU_Get_Temperature(); //得到温度值
MPU_Get_Accelerometer(&aacx,&aacy,&aacz); //得到加速度传感器数据
MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz); //得到陀螺仪数据
mpu6050.speed++; //上报速度自加
if(mpu6050.speed == 4) //上报速度阈值设置
{
mpu6050.flag = 1; //采集成功标志位设置为有效
mpu6050.speed = 0; //上报速度归零
}
else //采集不成功
{
mpu6050.flag = 0; //采集成功标志位设置为无效
}
if(mpu6050.flag == 1) //采集成功时
{
if(temp<0) //对数据正负判断,判断为负时
{
temp=-temp; //对负数据取反
}
else //判断为正时
{
}
printf("temp:%d.%d,",temp/100,temp%10); //通过串口1输出温度
temp=pitch*10; //赋temp为pitch
if(temp<0) //对数据正负判断,判断为负时
{
temp=-temp; //对负数据取反
}
else //判断为正时
{
}
printf("pitch:%d.%d,",temp/10,temp%10); //通过串口1输出pitch
temp=roll*10; //赋temp为roll
if(temp<0) //对数据正负判断,判断为负时
{
temp=-temp; //对负数据取反
}
else //判断为正时
{
}
printf("roll:%d.%d,",temp/10,temp%10);//通过串口1输出roll
temp=yaw*10; //赋temp为yaw
if(temp<0) //对数据正负判断,判断为负时
{
temp=-temp; //对负数据取反
}
else //判断为正时
{
}
printf("yaw:%d.%d,",temp/10,temp%10);//通过串口1输出yaw
printf("aacx:%d,aacy:%d,aacz:%d\r\n",aacx,aacy,aacz);//上报角速度数据,角加速度数据
mpu6050.flag = 0; //采集成功标志位设置为无效
}
else ; //防卡死
}
timeCount++;
delay_ms(10);
}
}
|