|
上電之后需按一下復(fù)位
IMG_20201216_151412.jpg (4.78 MB, 下載次數(shù): 87)
下載附件
2020-12-16 15:15 上傳
單片機源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "lcd.h"
- #include "usart.h"
- #include "mpu6050.h"
- #include "usmart.h"
- #include "inv_mpu.h"
- #include "inv_mpu_dmp_motion_driver.h"
- #include "oled_iic.h"
- /*******************
- OLED接線定義:
- VCC--3.3V/5V
- GND--GND
- SCL--PB8
- SDA--PB9
- MPU6050接線定義:
- VCC--5V
- GND--GND
- SCL--PB6
- SDA--PB7
-
-
- 2020/12/16 15:10
- *************************/
- ////串口1發(fā)送1個字符
- //c:要發(fā)送的字符
- void usart1_send_char(u8 c)
- {
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); //循環(huán)發(fā)送,直到發(fā)送完畢
- USART_SendData(USART1,c);
- }
- //傳送數(shù)據(jù)給匿名四軸上位機軟件(V2.6版本)
- //fun:功能字. 0XA0~0XAF
- //data:數(shù)據(jù)緩存區(qū),最多28字節(jié)!!
- //len:data區(qū)有效數(shù)據(jù)個數(shù)
- void usart1_niming_report(u8 fun,u8*data,u8 len)//匿名上位機協(xié)議
- {
- u8 send_buf[32];
- u8 i;
- if(len>28)return; //最多28字節(jié)數(shù)據(jù)
- send_buf[len+4]=0; //校驗數(shù)置零
- send_buf[0]=0XAA; //幀頭
- send_buf[1]=0XAA; //幀頭
- send_buf[2]=fun; //功能字
- send_buf[3]=len; //數(shù)據(jù)長度
- for(i=0;i<len;i++)send_buf[4+i]=data[i]; //復(fù)制數(shù)據(jù)
- for(i=0;i<len+4;i++)send_buf[len+4]+=send_buf[i]; //計算校驗和
- for(i=0;i<len+5;i++)usart1_send_char(send_buf[i]); //發(fā)送數(shù)據(jù)到串口1
- }
- //發(fā)送加速度傳感器數(shù)據(jù)和陀螺儀數(shù)據(jù)
- //aacx,aacy,aacz:x,y,z三個方向上面的加速度值
- //gyrox,gyroy,gyroz:x,y,z三個方向上面的陀螺儀值
- void mpu6050_send_data(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz)
- {
- u8 tbuf[12];
- tbuf[0]=(aacx>>8)&0XFF;
- tbuf[1]=aacx&0XFF;
- tbuf[2]=(aacy>>8)&0XFF;
- tbuf[3]=aacy&0XFF;
- tbuf[4]=(aacz>>8)&0XFF;
- tbuf[5]=aacz&0XFF;
- tbuf[6]=(gyrox>>8)&0XFF;
- tbuf[7]=gyrox&0XFF;
- tbuf[8]=(gyroy>>8)&0XFF;
- tbuf[9]=gyroy&0XFF;
- tbuf[10]=(gyroz>>8)&0XFF;
- tbuf[11]=gyroz&0XFF;
- usart1_niming_report(0X02,tbuf,12);//飛機傳感器數(shù)據(jù),0X02
- }
- //roll:橫滾角.單位0.01度。 -18000 -> 18000 對應(yīng) -180.00 -> 180.00度
- //pitch:俯仰角.單位 0.01度。-9000 - 9000 對應(yīng) -90.00 -> 90.00 度
- //yaw:航向角.單位為0.1度 0 -> 3600 對應(yīng) 0 -> 360.0度
- void usart1_report_imu(short roll,short pitch,short yaw)
- {
- u8 tbuf[28];
- u8 i;
- for(i=0;i<28;i++)tbuf[i]=0;//清0
- tbuf[0]=(roll>>8)&0XFF;
- tbuf[1]=roll&0XFF;
- tbuf[2]=(pitch>>8)&0XFF;
- tbuf[3]=pitch&0XFF;
- tbuf[4]=(yaw>>8)&0XFF;
- tbuf[5]=yaw&0XFF;
- usart1_niming_report(0X01,tbuf,28);//飛機姿態(tài)等基本信息,0X01
- }
-
- int main(void)
- {
- // u8 report=1; //默認(rèn)開啟上報
- float pitch,roll,yaw; //歐拉角
- short aacx,aacy,aacz; //加速度傳感器原始數(shù)據(jù)
- short gyrox,gyroy,gyroz; //陀螺儀原始數(shù)據(jù)
- u8 t,m;
- short temp;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設(shè)置NVIC中斷分組2:2位搶占優(yōu)先級,2位響應(yīng)優(yōu)先級
- uart_init(500000); //串口初始化為500000
- delay_init(); //延時初始化
- usmart_dev.init(72); //初始化USMART
- LED_Init(); //初始化與LED連接的硬件接口
- KEY_Init(); //初始化按鍵
- MPU_Init(); //初始化MPU6050
- mpu_dmp_init();
- OLED_Init(); //OLED屏幕初始化
- OLED_Clear();
-
- //顯示pitch和小數(shù)點
- OLED_ShowCH(1,0,"pitch");
- OLED_ShowCH(60+27,0,".");
- //顯示roll和小數(shù)點
- OLED_ShowCH(1,2,"roll");
- OLED_ShowCH(60+27,2,".");
- //顯示yaw和小數(shù)點
- OLED_ShowCH(1,4,"yaw");
- OLED_ShowCH(60+27,4,".");
-
- t = mpu_dmp_init() ;
- while( mpu_dmp_init() )//mpu6050自檢
- {
-
- OLED_ShowCH(0,0," error");
- OLED_ShowNum(0,3,t,2,1);
- }
- while(1)
- {
-
- if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
- {
- LED0=0;
- MPU_Get_Accelerometer(&aacx,&aacy,&aacz); //得到加速度傳感器數(shù)據(jù)
- MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz); //得到陀螺儀數(shù)據(jù)
- // if(report)mpu6050_send_data(aacx,aacy,aacz,gyrox,gyroy,gyroz);//用自定義幀發(fā)送加速度和陀螺儀原始數(shù)據(jù)
- // if(report)usart1_report_imu((int)-(roll*100),(int)(pitch*100),(int)-(yaw*100));//左右
- // temp=MPU_Get_Temperature(); //獲取溫度值
-
- if((m%10)==0)
- {
- temp = pitch * 10;
- if(temp<0)
- {
- OLED_ShowCH(60-7,0,"-");//顯示負(fù)號
- temp = -temp;//將temp改為正數(shù)
- }
- else OLED_ShowCH(60-7,0," ");//不顯示負(fù)號
-
- OLED_ShowNum(60+35,0,temp%10,1,16); //顯示小數(shù)部
- OLED_ShowNum(60,0,temp/10,3,16); //顯示整數(shù)部分
-
- temp = roll * 10;
- if(temp<0)
- {
- OLED_ShowCH(60-7,2,"-");//顯示負(fù)號
- temp = -temp;//將temp改為正數(shù)
- }
- else OLED_ShowCH(60-7,2," ");//不顯示負(fù)號
-
- OLED_ShowNum(60+35,2,temp%10,1,16); //顯示小數(shù)部
- OLED_ShowNum(60,2,temp/10,3,16); //顯示整數(shù)部分
-
- temp = yaw * 10;
- if(temp<0)
- {
- OLED_ShowCH(60-7,4,"-");//顯示負(fù)號
- temp = -temp;//將temp改為正數(shù)
- }
- else OLED_ShowCH(60-7,4," ");//不顯示負(fù)號
-
- OLED_ShowNum(60+35,4,temp%10,1,16); //顯示小數(shù)部
- OLED_ShowNum(60,4,temp/10,3,16); //顯示整數(shù)部分
- }
- }
- m++;
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
C8T6+MPU6050+OLED(IIC).7z
(278.71 KB, 下載次數(shù): 507)
2020-12-16 15:47 上傳
點擊文件名下載附件
|
評分
-
查看全部評分
|