这个暑假学习了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)
SIM800C.7z
(224.43 KB, 下载次数: 161)
|