|
這個暑假學(xué)習(xí)了STM32,感覺功能十分強(qiáng)大,確實(shí)比51單片機(jī)高了幾個檔次。然后就在糾結(jié)做個什么作品出來,一開始的時候,本來是想做個平衡小車的,但是時間確實(shí)比較緊,還有別的事情要做,以及PID算法也沒有過多的了解過,就用MPU6050加上SIM800C做了一個老人摔倒報警裝置。然后用NRF24L01進(jìn)行通信,將MPU的數(shù)據(jù)傳輸?shù)綆в蠸IM800C的32C8T6上,在中斷種檢測是否摔倒,然后向指定的號碼(在程序中可改)報警。第一個是NRF和MPU的數(shù)據(jù)采集和傳輸代碼,第二個是NRF和SIM的數(shù)據(jù)收集判斷和報警裝置的代碼,還用到了NRF的一發(fā)多收和多發(fā)一收,以及收發(fā)切換的模式,有需要的老哥自行下載。
單片機(jī)源程序如下:
- #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發(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ù)給匿名四軸上位機(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)
- {
- u8 send_buf[32];
- u8 i;
- if(len>28)return; //最多28字節(jié)數(shù)據(jù)
- send_buf[len+3]=0; //校驗(yàn)數(shù)置零
- send_buf[0]=0X88; //幀頭
- send_buf[1]=fun; //功能字
- send_buf[2]=len; //數(shù)據(jù)長度
- for(i=0;i<len;i++)send_buf[3+i]=data[i]; //復(fù)制數(shù)據(jù)
- for(i=0;i<len+3;i++)send_buf[len+3]+=send_buf[i]; //計算校驗(yàn)和
- for(i=0;i<len+4;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(0XA1,tbuf,12);//自定義幀,0XA1
- }
- //通過串口1上報結(jié)算后的姿態(tài)數(shù)據(jù)給電腦
- //aacx,aacy,aacz:x,y,z三個方向上面的加速度值
- //gyrox,gyroy,gyroz:x,y,z三個方向上面的陀螺儀值
- //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 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; //默認(rèn)開啟上報
- 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; //加速度傳感器原始數(shù)據(jù)
- short gyrox,gyroy,gyroz; //陀螺儀原始數(shù)據(jù)
- short temp; //溫度
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設(shè)置NVIC中斷分組2:2位搶占優(yōu)先級,2位響應(yīng)優(yōu)先級
- 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(); //延時函數(shù)初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設(shè)置NVIC中斷分組2:2位搶占優(yōu)先級,2位響應(yīng)優(yōu)先級
- 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°為基本轉(zhuǎn)動單位
- 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); //得到加速度傳感器數(shù)據(jù)
- MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz); //得到陀螺儀數(shù)據(jù)
- res=dmp_get_pedometer_step_count(&step_cnt);
- if(res)step_cnt=0;
- if(report)mpu6050_send_data(aacx,aacy,aacz,gyrox,gyroy,gyroz);//用自定義幀發(fā)送加速度和陀螺儀原始數(shù)據(jù)
- 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';//校驗(yàn)位
-
- NRF24L01_TxPacket(tmp_buf);//向NRF1傳輸數(shù)據(jù)(航向角數(shù)據(jù))
- //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';//校驗(yàn)位
-
- NRF24L01_TxPacket(tmp_buf);//向NRF2傳輸數(shù)據(jù)(加速度數(shù)據(jù))
-
- LED0=!LED0;
- delay_ms(100);
- }
- t++;
- if(t==100)t=0;
- //LED0=!LED0;
- //delay_ms(200);
- }
- //LED0=!LED0;
- //delay_ms(200);
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
MPU6050-NRF24L01.7z
(246.32 KB, 下載次數(shù): 166)
2019-9-27 18:12 上傳
點(diǎn)擊文件名下載附件
程序1 下載積分: 黑幣 -5
SIM800C.7z
(224.43 KB, 下載次數(shù): 161)
2019-9-27 18:12 上傳
點(diǎn)擊文件名下載附件
程序2 下載積分: 黑幣 -5
|
評分
-
查看全部評分
|