[DemoCode下载] 315Mhz无线模块单片机软解码接收程序

[复制链接]
 楼主| neeringstu 发表于 2016-7-2 22:01 | 显示全部楼层 |阅读模式
  1. # include<reg52.h>
  2. # define uint unsigned int
  3. # define uchar unsigned char

  4. sbit DATA=P3^2;
  5. sbit rs=P2^6;          //1602引脚定义
  6. sbit rw=P2^5;
  7. sbit e=P2^7;
  8. uchar dat,num;

  9. uchar code table[]="0123456789ABCDEF";
  10. uchar code table1[]="Receive:";        //液晶固定部分显示
  11. uchar code table2[]="Re_data:0x";

  12. void write_data (uchar dat);         //1602写数据
  13. void write_com (uchar com);         //1602写命令
  14. unsigned char pow(unsigned char n,unsigned char m);        //n的m次方函数
  15. uchar receive(void);                          //接收处理函数  
  16. void gd();                        //液晶固定部分显示

  17. void delay (uint xms)        //1602延时
  18. {
  19. uint i,j;
  20. for (i = xms; i > 0; i--)
  21. for (j = 110; j > 0; j--);
  22. }

  23. void delay1(unsigned char t)//延时程序
  24. {
  25. unsigned char n;
  26. for(;t>0;t--)
  27. for(n=40;n>0;n--);  
  28. }

  29. unsigned char pow(unsigned char n,unsigned char m)//n的m次方函数
  30. {
  31. unsigned  char t,result=1;
  32. for(t=0;t<m;t++){result=n*result;}
  33. return result;
  34. }

  35. void init_1602()
  36. {
  37.     e = 0;        //1602初始化
  38. write_com (0x38);
  39.     write_com (0x0c);
  40. write_com (0x06);
  41. write_com (0x01);
  42. gd();
  43. }

  44. /*1602液晶代码部分        ------------------------------ */
  45. void write_com (uchar com)        //写命令
  46. {
  47. rs = 0;
  48. rw = 0;
  49. P0 = com;
  50. delay (5);
  51. e = 1;
  52. delay (5);
  53. e = 0;
  54. }

  55. void write_data (uchar dat)         //写数据
  56. {
  57. rs = 1;
  58. rw = 0;
  59. P0 = dat;
  60. delay (5);
  61. e = 1;
  62. delay (5);
  63. e = 0;
  64. }

  65. void gd()        //液晶固定部分显示
  66. {
  67.     write_com(0x80);
  68. for(num=0;num<8;num++)
  69. {
  70.    write_data(table1[num]);
  71. delay(5);
  72. }
  73. write_com(0x80+0x40);
  74. for(num=0;num<10;num++)
  75. {
  76.    write_data(table2[num]);
  77. delay(5);
  78. }
  79. }

  80. uchar receive(void)//接收处理函数
  81. {
  82. unsigned char guid=0,result[12],i,key=0,res=0,t,time=0;
  83. while(1)//捕获前导命令
  84. {
  85. while(DATA==1){t++;if(t>=90){delay1(100);return 0;}}//防止错误数据导致的死循环
  86. if(t>=60&&t<95){t=0;key++;time=0;if(key>3)break;}//获得前导命令跳出循环,清除计时信号
  87. else if(time>100){delay1(100);return 0;}//长0,错误信号返回0
  88. else {t=0;time++;}//计时垒加,清除t
  89. }  
  90. t=0;
  91. time=0;
  92. for(i=1;i<13;)             //校验码及数据的接收共12位数据
  93. {
  94. while(DATA==1){t++;if(t>=95){delay1(100);return 0;}}//防止错误信号导致的死循环
  95. if(t>=60&&t<95){t=0;i=1;time=0;}//去除多余的前导命令
  96. else if(t>=28&&t<60){result[i-1]=1;i++;time=0;}//捕获数据1
  97. else if(t>0&&t<27){result[i-1]=0;i++;time=0;}//捕获数据0
  98. if(time>100)return 0; //消除长0的干扰确保数据正确
  99. t=0;   //清零
  100. time++;//计时      
  101. }
  102. if(result[0]==1&&result[1]==0&&result[2]==1&&result[3]==0)//判断校验码
  103. for(i=0;i<8;i++){res+=pow(2,i)*result[11-i];}//将结果转换为十进制数据
  104. return res;//返回得到的结果
  105. }


 楼主| neeringstu 发表于 2016-7-2 22:02 | 显示全部楼层
  1. void display(uchar dat)        //液晶数据显示
  2. {
  3.     uchar a,b;
  4. a=dat/16;
  5. b=dat%16;
  6. if(a>9)
  7. a=a+0;
  8. if(b>9)
  9. b=b+0;
  10. write_com(0x80+0x4A);
  11. write_data(table[a]);delay(5);
  12. write_data(table[b]);delay(5);
  13. }

  14. void main()
  15. {
  16. init_1602();        //1602初始化
  17. while(1)
  18. {
  19. dat=receive();
  20. if(dat)                                  //显示
  21. {
  22.       write_com(0x80+0x08);
  23.   write_data('O');delay(5);
  24.   write_data('K');delay(5);
  25.   write_data('!');delay(5);
  26.   display(dat);
  27. }
  28. else
  29. {
  30.       write_com(0x80+0x08);
  31.   write_data('N');delay(5);
  32.   write_data('O');delay(5);
  33.   write_data('!');delay(5);
  34.   write_com(0x80+0x4A);
  35.   write_data(' ');delay(5);
  36.   write_data(' ');delay(5);
  37. }
  38. }
  39. }
玛尼玛尼哄 发表于 2016-7-3 16:10 | 显示全部楼层
到底是什么模块,也不介绍介绍。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

35

主题

235

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部