這個暑假學習了STM32,感覺功能十分強大,確實比51單片機高了幾個檔次。然后就在糾結做個什么作品出來,一開始的時候,本來是想做個平衡小車的,但是時間確實比較緊,還有別的事情要做,以及PID算法也沒有過多的了解過,就用MPU6050加上SIM800C做了一個老人摔倒報警裝置。然后用NRF24L01進行通信,將MPU的數據傳輸到帶有SIM800C的32C8T6上,在中斷種檢測是否摔倒,然后向指定的號碼(在程序中可改)報警。第一個是NRF和MPU的數據采集和傳輸代碼,第二個是NRF和SIM的數據收集判斷和報警裝置的代碼,還用到了NRF的一發多收和多發一收,以及收發切換的模式,有需要的老哥自行下載。
單片機源程序如下:
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "timer.h"
- #include "led.h"
- #include "mpu6050.h"
- #include "inv_mpu.h"
- #include "inv_mpu_dmp_motion_driver.h"
- #include "24l01.h"
-
- extern u8 TX_ADDRESS[TX_ADR_WIDTH];
- extern u8 RX_ADDRESS[RX_ADR_WIDTH];
- //串口1發送1個字符
- //c:要發送的字符
- void usart1_send_char(u8 c)
- {
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); //循環發送,直到發送完畢
- USART_SendData(USART1,c);
- }
- //傳送數據給匿名四軸上位機軟件(V2.6版本)
- //fun:功能字. 0XA0~0XAF
- //data:數據緩存區,最多28字節!!
- //len:data區有效數據個數
- void usart1_niming_report(u8 fun,u8*data,u8 len)
- {
- u8 send_buf[32];
- u8 i;
- if(len>28)return; //最多28字節數據
- send_buf[len+3]=0; //校驗數置零
- send_buf[0]=0X88; //幀頭
- send_buf[1]=fun; //功能字
- send_buf[2]=len; //數據長度
- for(i=0;i<len;i++)send_buf[3+i]=data[i]; //復制數據
- for(i=0;i<len+3;i++)send_buf[len+3]+=send_buf[i]; //計算校驗和
- for(i=0;i<len+4;i++)usart1_send_char(send_buf[i]); //發送數據到串口1
- }
- //發送加速度傳感器數據和陀螺儀數據
- //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(0XA1,tbuf,12);//自定義幀,0XA1
- }
- //通過串口1上報結算后的姿態數據給電腦
- //aacx,aacy,aacz:x,y,z三個方向上面的加速度值
- //gyrox,gyroy,gyroz:x,y,z三個方向上面的陀螺儀值
- //roll:橫滾角.單位0.01度。 -18000 -> 18000 對應 -180.00 -> 180.00度
- //pitch:俯仰角.單位 0.01度。-9000 - 9000 對應 -90.00 -> 90.00 度
- //yaw:航向角.單位為0.1度 0 -> 3600 對應 0 -> 360.0度
- void usart1_report_imu(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz,short roll,short pitch,short yaw)
- {
- u8 tbuf[28];
- u8 i;
- for(i=0;i<28;i++)tbuf[i]=0;//清0
- 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;
- tbuf[18]=(roll>>8)&0XFF;
- tbuf[19]=roll&0XFF;
- tbuf[20]=(pitch>>8)&0XFF;
- tbuf[21]=pitch&0XFF;
- tbuf[22]=(yaw>>8)&0XFF;
- tbuf[23]=yaw&0XFF;
- usart1_niming_report(0XAF,tbuf,28);//飛控顯示幀,0XAF
- }
- int main(void)
- {
- u8 report=1; //默認開啟上報
- u16 t=0;
- u32 step_cnt;
- u32 res;
- u16 i;
- u8 address1[5]={0x34,0x43,0x10,0x10,0x02};
- u8 address2[5]={0x34,0x43,0x10,0x10,0x03};
- u8 tmp_buf[33];
- float pitch,roll,yaw; //歐拉角
- short aacx,aacy,aacz; //加速度傳感器原始數據
- short gyrox,gyroy,gyroz; //陀螺儀原始數據
- short temp; //溫度
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設置NVIC中斷分組2:2位搶占優先級,2位響應優先級
- uart_init(500000); //串口初始化為500000
- delay_init(); //延時初始化
- LED_Init(); //初始化與LED連接的硬件接口
- MPU_Init(); //初始化MPU6050
- NRF24L01_Init();
-
-
- /*u16 led0pwmval=0;
- u16 a=17000;
- u8 dir=1;
- delay_init(); //延時函數初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設置NVIC中斷分組2:2位搶占優先級,2位響應優先級
- uart_init(115200); //串口初始化為115200
- LED_Init(); //LED端口初始化
- TIM3_PWM_Init(19999,71); // 72分頻,最高頻率20000
- while(1)
- {
- delay_ms(500);
- if(dir)led0pwmval+=500;
- else led0pwmval-=500;
- if(led0pwmval>18000)dir=0;
- if(led0pwmval==0)dir=1;
- a+=500;
- if(a>19500) a=17500;
- TIM_SetCompare2(TIM3,a); //以45°為基本轉動單位
- LED0=!LED0;
- delay_ms(200);
- } */
- while(mpu_dmp_init()&&NRF24L01_Check())
- {
- delay_ms(200);
- }
- while(1)
- {
- NRF24L01_TX_Mode();
- if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
- {
- temp=MPU_Get_Temperature(); //得到溫度值
- MPU_Get_Accelerometer(&aacx,&aacy,&aacz); //得到加速度傳感器數據
- MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz); //得到陀螺儀數據
- res=dmp_get_pedometer_step_count(&step_cnt);
- if(res)step_cnt=0;
- if(report)mpu6050_send_data(aacx,aacy,aacz,gyrox,gyroy,gyroz);//用自定義幀發送加速度和陀螺儀原始數據
- if(report)usart1_report_imu(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll*100),(int)(pitch*100),(int)(yaw*10));
- //LED0=!LED0;
- //delay_ms(200);
- if(t%3==0)
- {
- for(i=0;i<5;i++)
- {
- TX_ADDRESS[i]=address1[i];
- RX_ADDRESS[i]=address1[i];
- }
- tmp_buf[0]=temp/1000+'0';
- tmp_buf[1]=temp/100%10+'0';
- tmp_buf[2]=temp/10%10+'0';
- tmp_buf[3]=temp%10+'0';
- temp=pitch*10;
- if(temp<0)
- {
- temp=-temp;
- tmp_buf[4]='-';
- }
- else
- tmp_buf[4]='+';
- tmp_buf[5]=temp/1000+'0';
- tmp_buf[6]=temp/100%10+'0';
- tmp_buf[7]=temp/10%10+'0';
- tmp_buf[8]=temp%10+'0';
-
- temp=roll*10;
- if(temp<0)
- {
- temp=-temp;
- tmp_buf[9]='-';
- }
- else
- tmp_buf[9]='+';
- tmp_buf[10]=temp/1000+'0';
- tmp_buf[11]=temp/100%10+'0';
- tmp_buf[12]=temp/10%10+'0';
- tmp_buf[13]=temp%10+'0';
-
- temp=yaw*10;
- if(temp<0)
- {
- temp=-temp;
- tmp_buf[14]='-';
- }
- else
- tmp_buf[14]='+';
- tmp_buf[15]=temp/1000+'0';
- tmp_buf[16]=temp/100%10+'0';
- tmp_buf[17]=temp/10%10+'0';
- tmp_buf[18]=temp%10+'0';
-
- temp=step_cnt;
- if(temp<0)
- {
- temp=-temp;
- tmp_buf[19]='-';
- }
- else
- tmp_buf[19]='+';
- tmp_buf[20]=temp/1000+'0';
- tmp_buf[21]=temp/100%10+'0';
- tmp_buf[22]=temp/10%10+'0';
- tmp_buf[23]=temp%10+'0';
- tmp_buf[27]=0+'0';//校驗位
-
- NRF24L01_TxPacket(tmp_buf);//向NRF1傳輸數據(航向角數據)
- //delay_ms(10);
-
-
- LED0=!LED0;
- delay_ms(100);
- }
- if(t%3==1)
- {
- for(i=0;i<5;i++)
- {
- TX_ADDRESS[i]=address2[i];
- RX_ADDRESS[i]=address2[i];
- }
- tmp_buf[0]=temp/1000+'0';
- tmp_buf[1]=temp/100%10+'0';
- tmp_buf[2]=temp/10%10+'0';
- tmp_buf[3]=temp%10+'0';
-
- temp=aacx*10;
- if(temp<0)
- {
- temp=-temp;
- tmp_buf[4]='-';
- }
- else
- tmp_buf[4]='+';
- tmp_buf[5]=temp/10000+'0';
- tmp_buf[6]=temp/1000%10+'0';
- tmp_buf[7]=temp/100%10+'0';
- tmp_buf[8]=temp/10%10+'0';
- tmp_buf[9]=temp%10+'0';
-
- temp=aacy*10;
- if(temp<0)
- {
- temp=-temp;
- tmp_buf[10]='-';
- }
- else
- tmp_buf[10]='+';
- tmp_buf[11]=temp/10000+'0';
- tmp_buf[12]=temp/1000%10+'0';
- tmp_buf[13]=temp/100%10+'0';
- tmp_buf[14]=temp/10%10+'0';
- tmp_buf[15]=temp%10+'0';
-
- temp=aacz*10;
- if(temp<0)
- {
- temp=-temp;
- tmp_buf[16]='-';
- }
- else
- tmp_buf[16]='+';
- tmp_buf[17]=temp/10000+'0';
- tmp_buf[18]=temp/1000%10+'0';
- tmp_buf[19]=temp/100%10+'0';
- tmp_buf[20]=temp/10%10+'0';
- tmp_buf[21]=temp%10+'0';
-
- temp=step_cnt;
- if(temp<0)
- {
- temp=-temp;
- tmp_buf[22]='-';
- }
- else
- tmp_buf[22]='+';
- tmp_buf[23]=temp/1000+'0';
- tmp_buf[24]=temp/100%10+'0';
- tmp_buf[25]=temp/10%10+'0';
- tmp_buf[26]=temp%10+'0';
- tmp_buf[27]=1+'0';//校驗位
-
- NRF24L01_TxPacket(tmp_buf);//向NRF2傳輸數據(加速度數據)
-
- LED0=!LED0;
- delay_ms(100);
- }
- t++;
- if(t==100)t=0;
- //LED0=!LED0;
- //delay_ms(200);
- }
- //LED0=!LED0;
- //delay_ms(200);
- }
- }
復制代碼
所有資料51hei提供下載:
MPU6050-NRF24L01.7z
(246.32 KB, 下載次數: 166)
2019-9-27 18:12 上傳
點擊文件名下載附件
程序1 下載積分: 黑幣 -5
SIM800C.7z
(224.43 KB, 下載次數: 161)
2019-9-27 18:12 上傳
點擊文件名下載附件
程序2 下載積分: 黑幣 -5
|