MM32射频解码(产品检验,可靠稳定)
MM32射频解码
本人曾负责开发风扇灯产品,故将射频解码的算法和程序进行总结,以便日后学习完善,也与大家分享。射频模块与MM32的通信协议为类似于EV1527的协议,每帧发送32位数据,前20位为地址码,接着4位为相应的操作码,最后一个字节的高4位为动作码,协议最后1位为同步位,为0。下图为1527协议的编码方式。
下表为定义的产品遥控码格式。产品具备冷暖灯及调光功能,风扇的正反转,以及APP的Wifi控制。
射频解码程序如下,经过检验,稳定可靠,完成产品级别的交付。
// MM32射频解码uint8_t RF;uint8_t HW,LW; //高低电平宽度uint8_t Code_x; //接受到第几位编码uint8_t FRemote1,FRemote2,FRemote3,FRemote4; //第一次接收到遥控编码,这四个依次从高到低存放遥控码的32位uint8_t Encode1,Encode2,Encode3,Encode4;//解码寄存器uint8_t SRemote1,SRemote2,SRemote3,SRemote4; //第二次接收到遥控编码uint8_t rf_ok1,rf_ok2,rf_ok; //解码过程中的临时接收成功标志,接收到一个完整的遥控命令后置1,通知解码程序可以解码了uint8_t old_level; //保存上一次查询到的电平状态uint8_t Syn;//接收到同步码时置1uint8_t Decode_ok;//判断按键结束,规定时间内未接收到同步码时置1uint16_t s;//二次接收的规定时间uint16_t time;uint8_t rf_data[4]; //接受码存放uint8_t Code_check=1;//对码标志uint8_t num_y=0;//长按YELLOW计数uint8_t num_w=0;//长按WHITE计数uint8_t num_o=0;//长按OFF计数void TIM2_IRQHandler(void){ if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); RF = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5); if (!RF) { LW++; old_level=0; } else { HW++; if (!old_level) //检测电平从低到高的跳变,已检测到一个(高-低)完整电平周期 { if (((HW>=3)&&(HW<=5))&&((LW>=120)&&(LW<=130))) //同步码 {Syn = 1 ;time=640;Decode_ok=0; Code_x = 0; FRemote1=0; FRemote2=0; FRemote3=0; FRemote4=0; } else if((Syn)&&((LW>=10)&&(LW<=14)))//判0 { Code_x++;if(Code_x>31) { if(!rf_ok1) //rf_ok1临时接收成功 { //将接收到的编码复制到解码寄存器中Encode1=FRemote1;Encode2=FRemote2;Encode3=FRemote3;Encode4=FRemote4;rf_ok1=1; //通知解码子程序可以解码Syn=0;s=1000; }else{//将接收到的编码复制到解码寄存器中 SRemote1=FRemote1; SRemote2=FRemote2; SRemote3=FRemote3; SRemote4=FRemote4; rf_ok2=1;//通知解码子程序可以解码 Syn=0; } }} else if ((Syn)&&((LW>=2)&&(LW<=6))) //判1 { switch (Code_x) { case 0 : { FRemote1=FRemote1 | 0x80; break; }case 1 : { FRemote1=FRemote1 | 0x40; break; } case 2 : { FRemote1=FRemote1 | 0x20; break; } case 3 : { FRemote1=FRemote1 | 0x10; break; } case 4 : { FRemote1=FRemote1 | 0x08; break; } case 5 : { FRemote1=FRemote1 | 0x04; break; } case 6 : { FRemote1=FRemote1 | 0x02; break; } case 7 : { FRemote1=FRemote1 | 0x01; break; } case 8 : { FRemote2=FRemote2 | 0x80; break; } case 9 : { FRemote2=FRemote2 | 0x40; break; } case 10: { FRemote2=FRemote2 | 0x20; break; } case 11: { FRemote2=FRemote2 | 0x10; break; } case 12: { FRemote2=FRemote2 | 0x08; break; } case 13: { FRemote2=FRemote2 | 0x04; break; } case 14: { FRemote2=FRemote2 | 0x02; break; } case 15: { FRemote2=FRemote2 | 0x01; break; } case 16: { FRemote3=FRemote3 | 0x80; break; } case 17: { FRemote3=FRemote3 | 0x40; break; } case 18: { FRemote3=FRemote3 | 0x20; break; } case 19: { FRemote3=FRemote3 | 0x10; break; } case 20: { FRemote3=FRemote3 | 0x08; break; } case 21: { FRemote3=FRemote3 | 0x04; break; } case 22: { FRemote3=FRemote3 | 0x02; break; } case 23: { FRemote3=FRemote3 | 0x01; break; } case 24: { FRemote4=FRemote4 | 0x80; break; } case 25: { FRemote4=FRemote4 | 0x40; break; } case 26: { FRemote4=FRemote4 | 0x20; break; } case 27: { FRemote4=FRemote4 | 0x10; break; } case 28: { FRemote4=FRemote4 | 0x08; break; } case 29: { FRemote4=FRemote4 | 0x04; break; } case 30: { FRemote4=FRemote4 | 0x02; break; } case 31: { FRemote4=FRemote4 | 0x01; if(!rf_ok1) {Encode1=FRemote1;Encode2=FRemote2;Encode3=FRemote3;Encode4=FRemote4; rf_ok1=1; Syn=0; break; } else {SRemote1=FRemote1;SRemote2=FRemote2;SRemote3=FRemote3;SRemote4=FRemote4; rf_ok2=1; Syn=0; break; } } }Code_x++; } else {Code_x=0; Syn=0;FRemote1=0;FRemote2=0; FRemote3=0; HW=1;LW=0;Decode_ok=1; } LW=0;HW=1;} old_level=1; } if(Decode_ok){ if(rf_ok1) //规定时间内接收到两帧相同的数据才有效{s--;if(!s) rf_ok1=0;if(rf_ok2) {if((Encode1==SRemote1)&&(Encode2==SRemote2)&&(Encode3==SRemote3)){rf_ok=1;rf_ok1=0;rf_ok2=0; time=200;}else{rf_ok=0;rf_ok1=0;rf_ok2=0; } } }if(rf_ok) //判断是否接收成功{TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);rf_ok=0; rf_data[0]=Encode1;rf_data[1]=Encode2;rf_data[2]=Encode3;rf_data[3]=Encode4;/*YELLOW 接收*/if((rf_data[2]&0x05)==0x05){num_y++;num_w=0;num_o=0;}/*OFF 接收*/else if((rf_data[2]&0x01)==0x01){num_o++;num_y=0;num_w=0;}/*WHITE 接收*/else if((rf_data[2]&0x06)==0x06){num_w++;num_o=0;num_y=0;}/*其余键接收*/else{num_y=0;num_w=0;num_o=0;}/*产测*/if(((rf_data[2]&0x01)==0x01)&&(num_o>=20)&&(Code_check==0)) {mcu_start_wifitest();}/*配网*/if(((rf_data[2]&0x06)==0x06)&&(num_w>=20)&&(Code_check==0)) {//mcu_set_wifi_mode(SMART_CONFIG);mcu_reset_wifi();}/*对码*/if(((rf_data[2]&0x05)==0x05)&&(num_y>=5)&&(Code_check==1)) {REMOTE_ID1 = rf_data[0];REMOTE_ID2 = rf_data[1];REMOTE_ID3 = rf_data[2]&0xf0;Code_check = 0;Buzz_Ring(500);}else//正常遥控过程{Remote_Scan();//接收码校验OperationCode_Send();}TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);} } }}