STM32F103C8驱动MPU6050姿态与tofsense报警 (六)

主函数



int main(void) 
{    
    //RCC_Configuration(); //时钟设置
    
    //BUZZER_BEEP1();//蜂鸣器音1
        //BUZZER_BEEP1();//蜂鸣器音1
    //delay_ms(50);
    SYS_Init();//系统初始化总函数
    while(1)   //主循环
    {
     // BUZZER_BEEP1();//蜂鸣器音1
        MPU_Read();    //MPU6050数据读取
        DATA_Report(); //MPU6050数据上报
    }
}
/**
  * @brief  系统初始化总函数
  * @param  无
  * @retval 无
  */
void SYS_Init(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  //中断优先级分组函数
    delay_init();                                     //延时函数初始化      
    uart_init(115200);                                  //串口初始化为115200
    LED_Init();                                            //初始化与LED连接的硬件接口
    BUZZER_Init();//蜂鸣器初始化
    
    MPU_Init();                                         //初始化MPU6050
    while(mpu_dmp_init())                            //初始化mpu_dmp库
     {
        LED1 = !LED1;delay_ms(50);LED1 = !LED1;     //LED闪烁指示
        printf("Initialization failed!\r\n");        //串口初始化失败上报
    }
    printf("Initialization succeed!\r\n");            //串口初始化成功上报
    delay_ms(999);                                    //延时初界面显示
    mpu6050.flag = 0;                               //采集成功标志位初始化
    mpu6050.speed = 0;                                //上报速度初始化
}
/**
  * @brief  MPU6050数据读取函数
  * @param  无
  * @retval 无
  */
void MPU_Read(void)
{
    
    if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)//dmp处理得到数据,对返回值进行判断
    { 
        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;                            //采集成功标志位设置为无效
    }    
}
/**
  * @brief  MPU6050数据上报
  * @param  无
  * @retval 无
  */
void DATA_Report(void)
{
    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    
        printf("%d.%d,",temp/10,temp%10); //通过串口1输出pitch
        if (temp/10 >= 10 && temp/10 <= 30) 
        {  
            BUZZER_BEEP1();//蜂鸣器音1
        }
        temp=roll*10;                            //赋temp为roll
        if(temp<0)                                //对数据正负判断,判断为负时
        {
            temp=-temp;                            //对负数据取反    
        }
        else                                    //判断为正时
        {
        }
        if (temp/10 >= 5 && temp/10 <= 30) 
        {  
            BUZZER_BEEP1();//蜂鸣器音1
        }
        //printf("roll:%d.%d,",temp/10,temp%10);//通过串口1输出roll
        printf("%d.%d,",temp/10,temp%10);//通过串口1输出yaw    
        temp=yaw*10;                           //赋temp为yaw
        if(temp<0)                                //对数据正负判断,判断为负时
        {
            temp=-temp;                            //对负数据取反
        }
        else                                    //判断为正时
        {
        }
        //printf("yaw:%d.%d,",temp/10,temp%10);//通过串口1输出yaw    
        printf("%d.%d",temp/10,temp%10);//通过串口1输出roll
//        printf("gyrox:%d,gyroy:%d,gyroz:%d,aacx:%d,aacy:%d,aacz:%d\r\n",gyrox,gyroy,gyroz,aacx,aacy,aacz);//上报角速度数据,角加速度数据
            printf("\r\n");        
        LED1=!LED1;                                             //LED闪烁
        mpu6050.flag = 0;                                    //采集成功标志位设置为无效
    }
    else ;                                                        //防卡死
}

main.h

#include "stdio.h"//标准输入输出库
#include "string.h"//字符串库
#include "stdlib.h"//常用的系统函数库
#include "sys.h"//系统中断分组库
#include "delay.h"//延时函数库
#include "usart.h"//串口设置库
#include "led.h"//LED驱动库
#include "mpu6050.h"//MPU6050驱动库
#include "inv_mpu.h"//陀螺仪驱动库
#include "inv_mpu_dmp_motion_driver.h" //DMP姿态解读库
#include "buzzer.h"

char tmp_buf[33];            //字符串数组
float pitch,roll,yaw;         //欧拉角:俯仰角,偏航角,滚转角
short aacx,aacy,aacz;        //加速度传感器原始数据  angular acceleration
short gyrox,gyroy,gyroz;    //陀螺仪原始数据  gyroscope
short temp;                    //温度


struct MPU6050                //MPU6050结构体
{
    u8 flag;                //采集成功标志位
    u8 speed;                //上报速度
}mpu6050;                    //唯一结构体变量


int main(void);                //主函数
void SYS_Init(void);        //系统初始化总函数
void MPU_Read(void);        //MPU6050数据读取函数    
void DATA_Report(void);        //MPU6050数据上报    

你可能感兴趣的:(stm32,单片机,嵌入式硬件)