求助:傳感器初始化順序調整了也沒有變化,while(1)中循有if{if{}},為何執(zhí)行不同,是delay_ms的問題嗎?
C語言程序如下:
#include "stdio.h"//標準輸入輸出庫
#include "string.h"//字符串庫
#include "stdlib.h"//常用的系統(tǒng)函數庫
#include "sys.h"//系統(tǒng)中斷分組庫
#include "delay.h"//延時函數庫
#include "usart.h"//串口設置庫
#include "mpu6050.h"//MPU6050驅動庫
#include "inv_mpu.h"//陀螺儀驅動庫
#include "inv_mpu_dmp_motion_driver.h" //DMP姿態(tài)解讀庫
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; //發(fā)送間隔變量
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中斷優(yōu)先級分組函數
delay_init(); //延時函數初始化
uart_init(115200); //串口初始化為115200
Init_Hx711();//初始化HX711
delay_ms(2000);//等待感器穩(wěn)定
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) //主循環(huán)
{
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);
}
}
|