找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 22457|回复: 75
打印 上一主题 下一主题
收起左侧

433和315无线模块的软件编解码遥控程序

  [复制链接]
跳转到指定楼层
楼主
ID:144396 发表于 2016-10-25 19:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
pic单片机的软件编码解码程序:
  1. /******************************************************************************/
  2. /****************************** 遥控器接收程序 ********************************/
  3. /******************************************************************************/
  4. #include <pic.h>
  5. #include <pic1687x.h>
  6. #define remote_geshu 10
  7. /******************************************************************************/
  8. union BIT_16
  9. {
  10.   int TIMER1_REG;
  11.   unsigned char REG[2];
  12. }
  13. union BIT_32
  14. {
  15.   unsigned long data_temp_long;
  16.   unsigned char data_temp_byte[4];
  17.   }
  18. /******************************************************************************/
  19. static union BIT_16 TIMER1_TEMP;//16位定时器1
  20. static union BIT_32 data_temp;
  21. /******************************************************************************/
  22. static volatile unsigned char rec_status @ 97;
  23. static unsigned char data_cout;//接收的遥控器码位数
  24. static unsigned char data;//接收的4位数据
  25. static unsigned int h_pulse;//高电平宽度
  26. static unsigned int l_pulse;//低电平宽度
  27. static unsigned char remote_cout;//遥控器数量
  28. static unsigned char remote_numb;//遥控器编号
  29. /******************************************************************************/
  30. static unsigned char TIMER15S1;//清除学习码按键长按时间
  31. static unsigned char TIMER15S2;//学习等待时间
  32. static unsigned char TIMER15S3;//遥控器数据缓冲时间
  33. static unsigned char TIMER15S4;//LED显示时间
  34. static unsigned char TIMER15S5;//
  35. static unsigned char TIMER15S6;//
  36. static unsigned char TIMER15S7;//
  37. static unsigned char TIMER15S8;//
  38. /******************************************************************************/
  39. static bit head @ ((unsigned)(&rec_status)*8+(0));//同步头标志位
  40. static bit learn @ ((unsigned)(&rec_status)*8+(1));//学习标志位
  41. static bit recieved @((unsigned)(&rec_status)*8+(2));//接收完成标志位
  42. static bit remote_button_status @((unsigned)(&rec_status)*8+(3));//遥控器按键标志位
  43. static bit first_click_status @((unsigned)(&rec_status)*8+(4));//遥控器按键单击标志位
  44. /******************************************************************************/
  45. /********************************** 数据接收 **********************************/
  46. /******************************************************************************/
  47. unsigned char data_read(void)
  48. {
  49.   if(h_pulse>l_pulse)
  50.    {
  51.     if((l_pulse>200)&&(l_pulse<1000))
  52.      {
  53.       if(h_pulse<(l_pulse<<2)) return 1;//数据为1
  54.      }
  55.     return 2;//无效的数据
  56.    }
  57.   else if(h_pulse<l_pulse)
  58.    {
  59.     if((h_pulse>200)&&(h_pulse<1000))
  60.      {
  61.       if(l_pulse<(h_pulse<<2)) return 0;//数据为0
  62.      }
  63.     return 2;//无效的数据
  64.    }
  65. }
  66. /******************************************************************************/
  67. void clr_head(void)//清除寄存器
  68. {
  69.   data_cout=0;
  70.   head=0;
  71. }
  72. /******************************************************************************/
  73. #pragma interrupt_level 1
  74. void check_data(void)//检测数据是否正确
  75. {
  76.   if(head)
  77.    {
  78.     switch (data_read())
  79.      {
  80.       case 0:(data_temp.data_temp_long)<<=1;;data_cout++;break;
  81.       case 1:(data_temp.data_temp_long)<<=1;(data_temp.data_temp_long)++;;data_cout++;break;               

  82. default:clr_head();break;
  83.      }
  84.     if(data_cout>23)
  85.      {
  86.       INTE=0;
  87.       recieved=1;
  88.       clr_head();//
  89.      }
  90.    }
  91. }
  92. /******************************************************************************/
  93. #pragma interrupt_level 1
  94. void check_head(void)
  95. {
  96.   if((!head)&&(!recieved))//
  97.    {
  98.     if ((h_pulse>300)&&(h_pulse<1000))
  99.      {
  100.       if((l_pulse>h_pulse*27)&&(l_pulse<h_pulse*35))       {
  101.         head=1;
  102.       }
  103.      }
  104.    }
  105. }   //end******************>>
  106. /******************************************************************************/
  107. #pragma interrupt_level 1
  108. void interrupt level_h_l(void)
  109. {
  110. /* if(RAIF)//如果是电平中断
  111.    {        PORTA=PORTA;
  112.     RAIF=0;//
  113.     TIMER1_TEMP.REG[0]=TMR1L;
  114.     TIMER1_TEMP.REG[1]=TMR1H;        TMR1H=0;
  115.     TMR1L=0;
  116.     if(RA4)//如果是低电平中断
  117.      {            l_pulse=TIMER1_TEMP.TIMER1_REG;
  118.       check_data();
  119.       check_head();
  120.      }
  121.     else//如果是高电平中断
  122.      {
  123.       h_pulse=TIMER1_TEMP.TIMER1_REG;
  124.      }   
  125.    }*/
  126.   if(INTF)
  127.    {
  128.     INTF=0;
  129.     TIMER1_TEMP.REG[0]=TMR1L;
  130.     TIMER1_TEMP.REG[1]=TMR1H;
  131.     TMR1H=0;
  132.     TMR1L=0;
  133.     if(INTEDG)//低电平宽度
  134.          {
  135.       INTEDG=0;
  136.       l_pulse=TIMER1_TEMP.TIMER1_REG;
  137.       check_data();
  138.             check_head();
  139.      }
  140.     else
  141.      {
  142.       INTEDG=1;
  143.       h_pulse=TIMER1_TEMP.TIMER1_REG;          }
  144.    }
  145.   if(T0IF)//
  146.    {
  147.     T0IF=0;
  148.     TIMER15S1++;
  149.     TIMER15S2++;
  150.     TIMER15S3++;        TIMER15S4++;
  151.     TIMER15S5++;
  152.     TIMER15S6++;
  153.     TIMER15S7++;
  154.     TIMER15S8++;
  155.    } }
  156. /******************************************************************************/
  157. /********************************* 遥控器学习**********************************/
  158. /******************************************************************************/
  159. void clr_learn_reg(void)
  160. {
  161.   unsigned char n;
  162.   di();
  163.   for (n=1;n==remote_cout*4;n++)
  164.    {
  165.     eeprom_write(n,0);
  166.    }
  167.   eeprom_write(70,0);    eeprom_write(71,0);
  168.   ei(); }
  169. /******************************************************************************/
  170. unsigned char compare_data(unsigned char eep_addr)
  171. {
  172.   unsigned char n;
  173.   union BIT_32 addr_data;      for(n=0;n<4;n++)
  174.    {
  175.     addr_data.data_temp_byte[n]=eeprom_read(eep_addr+n);
  176.    }       if((data_temp.data_temp_long&0x00fffff0)==(addr_data.data_temp_long&0x00fffff0))          {
  177.        return 1;//地址匹配返回1
  178.      }
  179.        return 0;//地址不匹配返回0  }
  180. /******************************************************************************/
  181. unsigned char compare_all_data(void)
  182. {
  183.   unsigned char n;
  184.   for (n=0;n<remote_cout;n++)//在已经学习好的遥控器地址里比较有没有相同的
  185.    {
  186.     if (compare_data(n*4+1))//如果有相同的遥控器          {
  187.       return 1;
  188.      }
  189.    }
  190.       return 0;//没有一个地址是相同的  }
  191. /******************************************************************************/
  192. //读遥控器的数量
  193. void read_remote_cout(void)
  194.   {
  195.     remote_cout=eeprom_read(71);//读出已经学习的遥控器总数量
  196.     if(remote_cout>20)remote_cout=0; //如果EEPROM是空的则为0    }
  197. /******************************************************************************/
  198. void check_learn_pro(void)
  199. {
  200.   unsigned char n;
  201.   if(learn)
  202.    {
  203.     learn=0;        remote_numb=eeprom_read(70);//读出现在可以覆盖掉哪个遥控器的编号              

  204. read_remote_cout();////读遥控器的数量
  205.     if(remote_numb>(remote_geshu-1))remote_numb=0;//如果遥控器的编号已经是最大的了 则从小开始
  206.     if((remote_cout==0)||!compare_all_data())//如果还没有遥控器学习或没有相同地址的遥控器学习
  207.      {
  208.       di();            for(n=0;n<4;n++)
  209.        {
  210.         eeprom_write (remote_numb*4+n+1,data_temp.data_temp_byte[n]);
  211.        }                     remote_numb++;
  212.       if(remote_cout<remote_geshu+1)remote_cout++;//已经学习好的遥控器数量
  213.       eeprom_write(71,remote_cout);//保存已经学习好的遥控器总数量              eeprom_write

  214. (70,remote_numb);//保存已学习的遥控起编号
  215.       ei();               }
  216.    } }
  217. /******************************************************************************/
  218. void check_out_pro(void)
  219. {
  220.   read_remote_cout();//读遥控器的数量
  221.   if(compare_all_data())
  222.    {        data=data_temp.data_temp_byte[0]&0x0f;//
  223.     TIMER15S3=0;
  224.     TIMER15S4=0;
  225.     RC4=1;
  226.    }  }
  227. /******************************************************************************/
  228. void decode_init(void)//接收初始化
  229. {
  230.   OPTION=0x87;
  231.   RBPU=0;
  232.   TMR0=0;
  233.   T0IE=1;//使能定时器0中断   INTE=1;
  234.   TMR1ON=1;//
  235.   PORTC=0x00;//
  236.   TRISC=0x00;//
  237.   ei();//开放全局中断  }
  238. /******************************************************************************/
  239. void check_remote_recieved(void)//检测有无新的数据
  240. {
  241.   if(!RB1)//
  242.    {
  243.     if(TIMER15S1>30)//是否长按了3秒
  244.      {
  245.       learn=0;//
  246.       clr_learn_reg();
  247.       RC4=0;
  248.      }
  249.     else
  250.      {           learn=1;//
  251.       TIMER15S2=0;
  252.       TIMER15S4=0;
  253.       RC4=1;
  254.      }
  255.    }
  256.   else
  257.    {        TIMER15S1=0;
  258.     if(TIMER15S2>60)
  259.      {
  260.       learn=0;//
  261.      }
  262.    }
  263.   if(TIMER15S3>2)//数据保持时间
  264.    {
  265.     data=0;        TIMER15S3=0;
  266.    }
  267.   if(TIMER15S4>3)//LED显示时间
  268.    {
  269.     RC4=0;
  270.    }
  271.   if(recieved)
  272.    {
  273.     TIMER15S3=0;
  274.     recieved=0;
  275.     TIMER15S4=0;
  276.     RC4=1;
  277.     check_learn_pro();        check_out_pro();  
  278.    }
  279.   INTE=1; }
  280. /******************************************************************************/
  281. /********************************* 控制部分程序 *******************************/
  282. /******************************************************************************/
  283. void control_init(void)
  284. {
  285.       ; }
  286. /******************************************************************************//*********

  287. ************************声音部分程序*********************************/
  288. /******************************************************************************/
  289. /******************************************************************************/
  290. unsigned char delay(unsigned int nus)
  291. {
  292.   for(;nus>0;nus--)
  293.    {
  294.     if (recieved==1) return 0;
  295.     asm("nop");   } }
  296. /******************************************************************************/
  297. unsigned char soud_one_fre(unsigned int cout,unsigned int delay_time)//发音程序
  298. {
  299.   if(recieved==1)   return 0;
  300.   INTE=0;
  301.   for (;cout>0;cout--)
  302.    {
  303.     RC6=!RC6;
  304.     delay(delay_time);
  305.    }   RC6=0;
  306.   INTE=1; }
  307. /******************************************************************************/
  308. /*********************************急促的声音***********************************/
  309. /******************************************************************************/
  310. unsigned char soud_one_fre0(unsigned int time,unsigned int delay_time)
  311. {
  312.   for (;time>0;time--)
  313.    {
  314.     if(recieved==1) return 0;
  315.     soud_one_fre (1000,10);
  316.     delay(delay_time);
  317.    } }
  318. /******************************************************************************/
  319. /*********************************救护的声音***********************************/
  320. /******************************************************************************/
  321. unsigned char soud_tow_fre0(unsigned int time)//
  322. {
  323.   for (;time>0;time--)
  324.    {
  325.     if(recieved==1) return 0;
  326.     soud_one_fre(900,10);        soud_one_fre(800,30);
  327.    }  }
  328. /******************************************************************************//*********

  329. ***************************低-高音***********************************/
  330. /******************************************************************************/
  331. unsigned char soud_many_fre0(unsigned int time,unsigned int cout)
  332. {
  333.   for (;time>0;time--)
  334.    {
  335.     unsigned int delay_time;
  336.     for(delay_time=50;delay_time>10;delay_time--)
  337.      {
  338.       if(recieved==1) return 0;            soud_one_fre(cout,delay_time);
  339.      }
  340.    }  }
  341. /******************************************************************************/
  342. /************************************低-高音1**********************************/
  343. /******************************************************************************/
  344. unsigned char soud_many_fre2(unsigned int time,unsigned int cout)
  345. {
  346.   for (;time>0;time--)
  347.    {
  348.     unsigned int delay_time;
  349.     for(delay_time=30;delay_time>20;delay_time--)
  350.      {
  351.       if(recieved==1) return 0;
  352.       soud_one_fre(cout,delay_time);
  353.      }
  354.    }  }
  355. /******************************************************************************/
  356. /**********************************高—低音************************************/
  357. /******************************************************************************/
  358. unsigned char soud_many_fre1(unsigned int time,unsigned int cout)
  359. {  for (;time>0;time--)
  360.    {
  361.     unsigned int delay_time;
  362.     for (delay_time=10;delay_time<50;delay_time++)
  363.      {
  364.       if(recieved==1) return 0;
  365.       soud_one_fre(cout,delay_time);
  366.      }
  367.    }  }
  368. /******************************************************************************/
  369. /**********************************高—低音1***********************************/
  370. /******************************************************************************/
  371. unsigned char soud_many_fre3(unsigned int time,unsigned int cout)
  372. {
  373.   for (;time>0;time--)
  374.    {
  375.     unsigned int delay_time;
  376.     for(delay_time=20;delay_time<30;delay_time++)
  377.      {
  378.       if(recieved==1) return 0;
  379.       soud_one_fre(cout,delay_time);
  380.      }
  381.    }  }
  382. /******************************************************************************/
  383. /**********************************汽车报警声**********************************/
  384. /******************************************************************************/
  385. unsigned char soud_low_high_low(unsigned int cout)
  386. {
  387.   for (;cout>0;cout--)
  388.    {
  389.     if(recieved==1) return 0;
  390.     soud_many_fre0(1,10);
  391.     soud_many_fre2(1,10);
  392.    }  }
  393. /******************************************************************************/
  394. unsigned char remote_check(void)//遥控器按键检测
  395. {
  396.   if(data)//
  397.    {
  398.     if(remote_button_status)        {
  399.        return 0;
  400.       }
  401.     else
  402.       {                remote_button_status=1;
  403.        return data;//遥控器数据有效
  404.       }
  405.    }
  406.   else
  407.      {         remote_button_status=0;
  408.       return 0;
  409.      }  }
  410. /******************************************************************************/
  411. void check_remote_command(void)
  412. {
  413.   if(TIMER15S5>10)
  414.    {
  415.     first_click_status=0;//
  416.    }
  417.   switch (remote_check())
  418.    {
  419.     case 0x01:
  420.      {            RC0=!RC0;     //
  421.       soud_one_fre0(1,6000);
  422.       soud_tow_fre0(5);
  423.       delay(10000);
  424.         soud_many_fre0(5,40);
  425.       delay(10000);
  426.       soud_many_fre2(10,100);
  427.       delay(10000);        soud_many_fre1(5,40);
  428.       delay(10000);
  429.       soud_many_fre3(10,150);
  430.       delay(10000);
  431.       soud_low_high_low(5);
  432.       delay(10000);
  433.       soud_one_fre0(2,10000);            break;
  434.      }
  435.     case 0x02:RC1=!RC1;break;
  436.     case 0x04:RC2=!RC2;break;
  437.     case 0x08:
  438.      {
  439.       if(first_click_status==1)
  440.        {
  441.         RC3=!RC3;
  442.         first_click_status=0;//          break;
  443.        }
  444.       else
  445.        {
  446.         first_click_status=1;
  447.         TIMER15S5=0;           break;
  448.        }
  449.      }
  450.    default:break;
  451.    }
  452.   }
  453. /******************************************************************************/
  454. void control_pro(void)
  455. {
  456.   check_remote_command();
  457. }
复制代码

以上代码下载: 433和315无线模块的软件编解码遥控程序.zip (9.97 KB, 下载次数: 1289)

评分

参与人数 2黑币 +10 收起 理由
蓝色海阳 + 5 赞一个!
qq792500412 + 5 赞一个!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏39 分享淘帖 顶7 踩
回复

使用道具 举报

来自 2#
ID:34298 发表于 2017-12-2 21:35 | 只看该作者
我下载看到的程序说明:433和315无线模块的软件编解码遥控程序
1:单片机软件解码PT2240
2:选用PIC16F877A作为软件解码芯片
3:单片机时钟频率选用外部4MHZ晶振
4:选用外部中断脚作为编码信号脚输入脚
5:可解PT2240芯片(8脚的学习型编码芯片 编码地址位:2的20次方 重复几率100万分之一)
6:功能有:遥控器学习(DEMO上的S9作为学习按纽) 清除记忆(长按DEMO上的S9即可清除遥控器

地址的记忆)
7:学习遥控器数量可以设定(可根据EEPROM的大小 随便设定)
8:输出功能(有三路是 单击遥控器双稳,可以通过PORTC上的LED可以看到结果.还有一路是 双击遥控器双稳)
9:可选用315MHZ/433MHZ的超再生/超外差接收模块
回复

使用道具 举报

板凳
ID:117846 发表于 2017-2-4 19:48 | 只看该作者
多谢,正想要这个。。。
回复

使用道具 举报

地板
ID:47634 发表于 2017-2-5 22:09 | 只看该作者
很好很好,正在找这个资料
回复

使用道具 举报

5#
ID:163070 发表于 2017-2-7 15:47 来自手机 | 只看该作者
这个真需要,呵呵
回复

使用道具 举报

6#
ID:130231 发表于 2017-3-30 15:21 | 只看该作者
实用,好资料!感谢你的分享
回复

使用道具 举报

7#
ID:159139 发表于 2017-6-24 11:07 来自手机 | 只看该作者
谢谢,正想研究一下这方面。
回复

使用道具 举报

8#
ID:68356 发表于 2017-6-24 19:36 | 只看该作者
好东西啊,收藏备用
回复

使用道具 举报

9#
ID:153381 发表于 2017-6-30 09:06 | 只看该作者
好东西,正想要的!!!!!
回复

使用道具 举报

10#
ID:225510 发表于 2017-8-8 16:31 | 只看该作者
需要需要....
回复

使用道具 举报

11#
ID:226512 发表于 2017-8-10 11:42 | 只看该作者
怎么样可以下载啊
回复

使用道具 举报

12#
ID:112242 发表于 2017-8-10 21:11 | 只看该作者
这是个好资料 先下载再收藏
回复

使用道具 举报

13#
ID:165276 发表于 2017-8-12 16:07 | 只看该作者
找了好久都没找到,感谢楼主分享,闲置的两个模块可以用了
回复

使用道具 举报

14#
ID:137005 发表于 2017-9-3 21:06 | 只看该作者
看看,谢谢分享!
回复

使用道具 举报

15#
ID:232585 发表于 2017-9-16 09:42 | 只看该作者
有没有模块直接接单片机的资料啊  就是不用编解码芯片那种
回复

使用道具 举报

16#
ID:30057 发表于 2017-9-18 02:59 | 只看该作者
很好很好,确实不错
回复

使用道具 举报

17#
ID:238563 发表于 2017-10-11 13:17 | 只看该作者
感谢分享,不错哈哈哈
回复

使用道具 举报

18#
ID:196311 发表于 2017-10-12 09:19 | 只看该作者
下来看看,谢谢分享
回复

使用道具 举报

19#
ID:231936 发表于 2017-10-16 10:54 | 只看该作者
给个电路图就好了,我有315无线模块,正想用。
回复

使用道具 举报

20#
ID:241631 发表于 2017-10-21 18:07 | 只看该作者
黑币不够啊
回复

使用道具 举报

21#
ID:241631 发表于 2017-10-21 18:07 | 只看该作者
这个很想要
回复

使用道具 举报

22#
ID:245799 发表于 2017-11-3 19:34 | 只看该作者
没黑币
回复

使用道具 举报

23#
ID:51142 发表于 2017-11-11 23:59 | 只看该作者

下来看看,谢谢分享
回复

使用道具 举报

24#
ID:244951 发表于 2017-11-13 08:23 | 只看该作者
很好,正在找这个资料
回复

使用道具 举报

25#
ID:198864 发表于 2017-11-14 13:28 | 只看该作者
太帅了。
回复

使用道具 举报

26#
ID:249289 发表于 2017-11-14 13:56 | 只看该作者
正在找这个资料
回复

使用道具 举报

27#
ID:250288 发表于 2017-11-17 08:00 来自手机 | 只看该作者
谁给下程序谢谢,791736464@qq.com
回复

使用道具 举报

28#
ID:255706 发表于 2017-12-1 19:46 | 只看该作者
学习了
回复

使用道具 举报

29#
ID:196383 发表于 2017-12-3 13:54 | 只看该作者
多谢,正想要这个
回复

使用道具 举报

30#
ID:269870 发表于 2018-1-1 20:40 | 只看该作者
好东西!多多分享
回复

使用道具 举报

31#
ID:263813 发表于 2018-1-3 17:00 | 只看该作者
有这个模块一直不会玩
回复

使用道具 举报

32#
ID:291978 发表于 2018-4-9 12:28 | 只看该作者
看看怎样,谢谢楼主了
回复

使用道具 举报

33#
ID:281432 发表于 2018-6-11 09:36 | 只看该作者
看看怎样,谢谢楼主了
回复

使用道具 举报

34#
ID:352802 发表于 2018-6-16 11:19 | 只看该作者
试试看看 这个好用吧
回复

使用道具 举报

35#
ID:32627 发表于 2018-6-21 19:24 | 只看该作者
好资料!感谢你的分享
回复

使用道具 举报

36#
ID:363903 发表于 2018-7-3 13:40 | 只看该作者
多谢 ,学习了                             
回复

使用道具 举报

37#
ID:281301 发表于 2018-7-8 08:55 | 只看该作者
感谢你的分享
回复

使用道具 举报

38#
ID:372027 发表于 2018-7-15 16:40 | 只看该作者
赞一个,希望能派上用场
回复

使用道具 举报

39#
ID:290014 发表于 2018-7-17 10:15 | 只看该作者
好东西,正想要的!!!!!
回复

使用道具 举报

40#
ID:348201 发表于 2018-9-17 21:26 | 只看该作者
程序只有解码没有编码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表