找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STC15F单片机红外接收(遥控风扇)源码

[复制链接]
跳转到指定楼层
楼主
  1. //////STC15F104////////////
  2. #include <STC15F2K60S2.H>
  3. #include "74HC595.H"     //包含74HC595头文件
  4. #define uchar unsigned char
  5. #define uint unsigned int
  6. #define T50MS (65536-50000) //12T模式

  7. /*************        用户系统配置        **************/

  8. #define MAIN_Fosc                12000000L        //定义主时钟, 模拟串口和红外接收会自动适应。5~36MHZ

  9. #define D_TIMER0                125                        //选择定时器时间, us, 红外接收要求在60us~250us之间

  10. #define        User_code                0xFD02                //定义红外接收用户码


  11. /*************        以下宏定义用户请勿修改        **************/
  12. #define freq_base                        (MAIN_Fosc / 1200)
  13. #define Timer0_Reload                (65536 - (D_TIMER0 * freq_base / 10000))

  14. /*************        本地变量声明        **************/
  15. bit                P_IR_RX_temp;                //Last sample
  16. bit                B_IR_Sync;                        //已收到同步标志
  17. uchar        IR_SampleCnt;                //采样计数
  18. uchar        IR_BitCnt;                        //编码位数
  19. uchar        IR_UserH;                        //用户码(地址)高字节
  20. uchar        IR_UserL;                        //用户码(地址)低字节
  21. uchar        IR_data;                        //数据原码
  22. uchar        IR_DataShit;                //数据反码

  23. bit                B_IrUserErr;                //User code error flag
  24. bit                B_IR_Press;                        //Key press flag,include repeat key.
  25. uchar        IR_code;                        //IR code        红外键码

  26. sbit KG    =P3^1;
  27. sbit FMQ   =P3^0;
  28. sbit P_IR_RX    =P3^2;           //定义红外接收输入端口
  29. uchar code SMG[11]={0x77,0x60,0x3b,0x7a,0x6c,0x5e,0x5f,0x70,0x7f,0x7e, 0x00};//0, 1, 2, 3,4, 5, 6, 7, 8, 9, off
  30. uint i2,shi,fen,miao;
  31. uint i1,ontime,offtime;                          
  32. uchar k,data1=0x01;
  33. bit b;
  34. //uchar a[4];    //储存用户码、用户反码与键数据码、键数据反码

  35. /*************        本地函数声明        **************/
  36. void        Tx1Send(uchar dat);
  37. uchar        HEX2ASCII(uchar dat);
  38. void        InitTimer(void);
  39. void        PrintString(unsigned char code *puts);
  40. //////////////////////////////////////////
  41. void delay1ms(void)
  42. {
  43.    unsigned char i,j;      
  44.          for(i=0;i<10;i++)
  45.           for(j=0;j<33;j++)
  46.            ;                 
  47. }
  48. ////////////////////////////////////////////
  49. void delay(unsigned char n)
  50. {
  51.    unsigned char i;
  52.         for(i=0;i<n;i++)
  53.            delay1ms();
  54. }
  55. ////////////////////////////////////////////
  56. void beep()                //蜂鸣器响一声函数
  57. {
  58.    FMQ=0;
  59.    delay(250);       //延时
  60.    FMQ=1;           //关闭蜂鸣器     
  61. }
  62. ////////////////////////////////////////////
  63. void display(void)                //显示定时时间。
  64. {      
  65.         if(shi==0)           //当定时时间为0时,不显示
  66.         {
  67.                 Ser_IN(SMG[10]);
  68.             Par_OUT();
  69.         }         
  70.         else
  71.         {
  72.                 Ser_IN(SMG[shi]);
  73.                 Par_OUT();
  74.         }
  75. }

  76. /////////////////////////////////////////////////////////////
  77. void Model_2(void)
  78. {
  79.         ontime=40;
  80.         offtime=40;
  81. }
  82. /////////////////////////////////////////////////////////////
  83. void Model_3(void)
  84. {
  85.         ontime=100;
  86.         offtime=40;
  87. }
  88. /////////////////////////////////////////////////////////////
  89. void Model_4(void)
  90. {
  91.         ontime=200;
  92.         offtime=80;
  93. }
  94. /////////////////////////////////////////////////////////////
  95. void Model_5(void)
  96. {
  97.         ontime=700;
  98.         offtime=300;
  99. }
  100. /////////////////////////////////////////////////////////////
  101. //判断接收的编码,执行相应功能
  102. void _data_(void)
  103. {
  104.         switch(IR_data)
  105.         {
  106.                 case 0x01:k++;if(k==6) k=1;i1=0;b=1;beep();break;    //开机及模式调节。蜂鸣器响一声 提示解码成功,模式切换时i1必须清零
  107.                 case 0x05:shi++; if(shi==10) shi=0;beep();break;         //定时时间1~9小时
  108.                 case 0x09:break;                         //此功能暂时无效
  109.                 case 0x0d:k=0;beep();break;                                                         //关机
  110.         }
  111.         IR_data=0x00;           //必须清零
  112. }
  113. ////////////////////////////////////////////
  114. void main(void)         //主函数
  115. {

  116.         IE   = 0x82;   //开总中断和T0中断允许
  117.         IE2  = 0x04;   //T2中断允许
  118.         TMOD = 0;                //for STC15Fxxx系列        Timer0 as 16bit reload timer.
  119.         TH0  = Timer0_Reload / 256;
  120.         TL0  = Timer0_Reload % 256;
  121.         T2L  = T50MS;  //初始化计时值5ms中断一次
  122.     T2H  = T50MS >> 8;
  123.         TR0  =1;

  124.         k    =0;

  125.         while(1)
  126.         {
  127.                 switch(k)
  128.                 {
  129.                         case 0: AUXR=0x00;         //k=0时T2关闭中断
  130.                                         i1=0;                 //记得计时清零
  131.                                         shi=0;                 //关闭风扇时,定时时长也清零
  132.                                         KG=1;                 //关闭风扇
  133.                                         break;
  134.                         case 1: AUXR=0x10;         //k不等于0时打开中断
  135.                                         break;
  136.                         case 2: AUXR=0x10;         //k=2~5时启动相应模式
  137.                                         Model_2();
  138.                                         break;
  139.                         case 3: AUXR=0x10;
  140.                                         Model_3();
  141.                                         break;
  142.                         case 4: AUXR=0x10;
  143.                                         Model_4();
  144.                                         break;
  145.                         case 5: AUXR=0x10;
  146.                                         Model_5();
  147.                                         break;
  148.                 }
  149.                
  150.                 display();
  151.                
  152.         }                        
  153. }
  154. /*************红外接收部分程序是引用STC官方的资料******************************/
  155. /******************** 红外采样时间宏定义, 用户不要随意修改        *******************/

  156. #if ((D_TIMER0 <= 250) && (D_TIMER0 >= 60))
  157.         #define        D_IR_sample                        D_TIMER0                //定义采样时间,在60us~250us之间
  158. #endif

  159. #define D_IR_SYNC_MAX                (15000/D_IR_sample)        //SYNC max time
  160. #define D_IR_SYNC_MIN                (9700 /D_IR_sample)        //SYNC min time
  161. #define D_IR_SYNC_DIVIDE        (12375/D_IR_sample)        //decide data 0 or 1
  162. #define D_IR_DATA_MAX                (3000 /D_IR_sample)        //data max time
  163. #define D_IR_DATA_MIN                (600  /D_IR_sample)        //data min time
  164. #define D_IR_DATA_DIVIDE        (1687 /D_IR_sample)        //decide data 0 or 1
  165. #define D_IR_BIT_NUMBER                32                                        //bit number

  166. //*******************************************************************************************
  167. //**************************** IR RECEIVE MODULE ********************************************

  168. void IR_RX_HT6121(void)
  169. {
  170.         uchar        SampleTime;

  171.         IR_SampleCnt++;                                                        //Sample + 1

  172.         F0 = P_IR_RX_temp;                                                //Save Last sample status
  173.         P_IR_RX_temp = P_IR_RX;                                        //Read current status
  174.         if(F0 && !P_IR_RX_temp)                                        //Last sample is high,and current sample is low, so is fall edge
  175.         {
  176.                 SampleTime = IR_SampleCnt;                        //get the sample time
  177.                 IR_SampleCnt = 0;                                        //Clear the sample counter

  178.                          if(SampleTime > D_IR_SYNC_MAX)                B_IR_Sync = 0;        //large the Maxim SYNC time, then error
  179.                 else if(SampleTime >= D_IR_SYNC_MIN)                                        //SYNC
  180.                 {
  181.                         if(SampleTime >= D_IR_SYNC_DIVIDE)
  182.                         {
  183.                                 B_IR_Sync = 1;                                        //has received SYNC
  184.                                 IR_BitCnt = D_IR_BIT_NUMBER;        //Load bit number
  185.                         }
  186.                 }
  187.                 else if(B_IR_Sync)                                                //has received SYNC
  188.                 {
  189.                         if(SampleTime > D_IR_DATA_MAX)                B_IR_Sync=0;        //data samlpe time to large
  190.                         else
  191.                         {
  192.                                 IR_DataShit >>= 1;                                        //data shift right 1 bit
  193.                                 if(SampleTime >= D_IR_DATA_DIVIDE)        IR_DataShit |= 0x80;        //devide data 0 or 1
  194.                                 if(--IR_BitCnt == 0)                                //bit number is over?
  195.                                 {
  196.                                         B_IR_Sync = 0;                                        //Clear SYNC
  197.                                         if(~IR_DataShit == IR_data)                //判断数据正反码
  198.                                         {
  199.                                                 if((IR_UserH == (User_code / 256)) &&
  200.                                                         IR_UserL == (User_code % 256))
  201.                                                                 B_IrUserErr = 0;        //User code is righe
  202.                                                 else        B_IrUserErr = 1;        //user code is wrong
  203.                                                       
  204.                                                 IR_code      = IR_data;
  205.                                                 B_IR_Press   = 1;                        //数据有效
  206.                                                 _data_();
  207.                                         }
  208.                                 }
  209.                                 else if((IR_BitCnt & 7)== 0)                //one byte receive
  210.                                 {
  211.                                         IR_UserL = IR_UserH;                        //Save the User code high byte
  212.                                         IR_UserH = IR_data;                                //Save the User code low byte
  213.                                         IR_data  = IR_DataShit;                        //Save the IR data byte
  214.                                 }
  215.                         }
  216.                 }
  217.         }
  218. }

  219. /********************** Timer0中断函数************************/
  220. void timer0 (void) interrupt 1
  221. {
  222.         IR_RX_HT6121();
  223. }


复制代码



全部资料51hei下载地址:
红外接收(遥控风扇).zip (37.56 KB, 下载次数: 80)



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

沙发
ID:424598 发表于 2018-11-22 07:44 | 只看该作者
非常好的程序,实测可用.怎么没用顶呢?
回复

使用道具 举报

板凳
ID:647132 发表于 2020-1-9 22:23 | 只看该作者
学习了,谢谢楼主,
回复

使用道具 举报

地板
ID:734967 发表于 2020-5-2 16:56 | 只看该作者
直接下载进去好像不得用  太复杂也不利于学习
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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