电子开发网

电子开发网电子设计 | 电子开发网Rss 2.0 会员中心 会员注册
搜索: 您现在的位置: 电子开发网 >> 电子开发 >> EDA开发应用 >> Keilc >> 正文

keil c51红外遥控解码程序

作者:佚名    文章来源:本站原创    点击数:    更新时间:2011-3-3

    本keil c51程序适用uPC1621/uPC1622及兼容的红外遥控器芯片,占用外部中断0和定时器1,以中断方式解码,节省系统资源,以查询方式检测遥控信号是否有效.

解码思路:
    红外线经一体化接受头解码放到后送到单片机的外部中断0,单片机设置外部中断下降沿触发,T0和T1为16位定时器,T0在系统启动后定时5ms.T1在外部中断0启动后开始定时,初值为0,每次在INT0中断后先读T1计数值,并重设初值为0,而且判断T1的计数值,

代码
  1. //Fosc=11.0592MHz   
  2. // states for and variables IR data processing ;   
  3. typedef enum{    
  4.              IR_idle,    
  5.              IR_waitstart,             
  6.              IR_getaddr,             
  7.              IR_getaddrinv,             
  8.              IR_getdata,             
  9.              IR_getdatainv             
  10.             }_IRstate;                  
  11.   
  12. _IRstate IRstate = IR_idle;   
  13.   
  14. unsigned char IRaddr=0xff;   
  15. unsigned char _IRaddr=0xff;   
  16. unsigned char IRdata=0xff;   
  17. unsigned char _IRdata=0xff;   
  18. unsigned char IR_repeat=0;   
  19. unsigned char IR_ready=0;   
  20. unsigned char  IR_poweron=0;   
  21. //bit ir_done=0;   
  22. // time constants   
  23. unsigned int IRtimer=0; // IR timeout    
  24.   
  25. //cpu初始化   
  26. void cpu_init(void)   
  27. {   
  28.     TMOD=0X11; // T0 and T1 十六位定时                   
  29.     TH0=0xee;  //fosc=11.0592M,timer=5ms   
  30.     TL0=0x00;    
  31.     TR0=1; // run timer 0;   
  32.     TF0=0;   
  33.   
  34.     ET0=1;  // enable tmr 0 overflow interrupt   
  35.     IT0=1; // int0 edge sensitive   
  36.     EX0=1; //  enable "int0"   
  37.     EA=1;   // global interupt enable    
  38. }   
  39.   
  40. //T0中断   
  41. void tmrint() interrupt 1   
  42. {   
  43.     TH0=0xee;   
  44.     TL0=0x00;    
  45.     if (IRtimer)     //IR接收超时   
  46.         --IRtimer; //    
  47.     else  
  48.     {   
  49.         IRstate=IR_idle;   
  50. //        IR_poweron=0;   
  51.     }   
  52. }   
  53.   
  54. //Fosc=11.0592MHz   
  55. #define msec_12p5  0x2d00   
  56. #define msec_15  0x3600   
  57. #define msec_9  0x2066   
  58. //#define msec_9  0x1066   
  59. #define msec_2p5  0x900   
  60. #define msec_0p9  0x33d   
  61. #define msec_1p68  0x610   
  62.   
  63.   
  64. //void IRint() interrupt 0(void)   
  65.   
  66. //When the IR receive pin goes low and interrupt is generated    
  67. // IR is collected by starting timer 2 in the first falling edge of the pin   
  68. // then on every other falling edge, the timer value is saved and the timer restarted .     
  69. // the captured time is then used to get the IR data    
  70. // a "start of data" is 13.5Msec,a "1" is 2.25Msec,a "0" is 1.12 msec and a "repeat" is 11.25msec.   
  71. // the counter increments at 1.085 Usec   
  72. // I allow a fairly large tolerance to time jitter but there are no false triggers seen.   
  73.   
  74. void IRint() interrupt 0   
  75. {   
  76.     static unsigned char bits;   
  77.     unsigned short time;   
  78.     switch(IRstate)   
  79.     {   
  80.         case IR_idle:   
  81.             TL1=0;   
  82.             TH1=0;   
  83.             TR1=1;   
  84.             IRstate=IR_waitstart;   
  85.             IRtimer=26;   
  86.             break;   
  87.         case IR_waitstart: //P2_4=!P2_4;   
  88.             TR1=0;   
  89.             time=TH1;   
  90.             time =(time <<8)+TL1;;   
  91.             TL1=0;   
  92.             TH1=0;   
  93.             TR1=1;   
  94.             if ((time > msec_12p5)&&(time < msec_15)) // greater than 12.5Msec & less than 15 msec = start code    
  95.             {       
  96.                 IRaddr=0;   
  97.                 _IRaddr=0;   
  98.                 IRdata=0;   
  99.                 _IRdata=0;   
  100.                 bits=1;   
  101.                 IRstate=IR_getaddr;   
  102.             }   
  103.             else if ((time > msec_9)&&(time <  msec_12p5))// less than 12.5Msec  and greater than 9 msec =Repeat code    
  104.             {        
  105.                 IR_repeat=2;   
  106.                 IRstate=IR_idle;   
  107.             }   
  108.             else    
  109.             {           // to short, bad data just go to idle    
  110.                 IRstate=IR_idle;                   
  111.             }                   
  112.             break;   
  113.         case IR_getaddr:    // P2_4=!P2_4;   
  114.             TR1=0;   
  115.             time=TH1;   
  116.             time =(time <<8)+TL1;;   
  117.             TL1=0;   
  118.             TH1=0;   
  119.             TR1=1;   
  120.             if ((time>msec_2p5)||(time<msec_0p9))// if  > 2.5msec or shorter than .9Msec bad data , go to idle    
  121.             {      
  122.                 IRstate=IR_idle;   
  123.                 break;    
  124.             }   
  125.             if (time>msec_1p68)// greater than 1.68Msec is a 1   
  126.             {         
  127.                 IRaddr|= bits;   
  128.             }   
  129.             bits=bits<<1;   
  130.             if (!bits)   
  131.             {   
  132.                 IRstate=IR_getaddrinv;                   
  133.                 bits=1;   
  134.             }   
  135.             break;                 
  136.         case IR_getaddrinv:  //P2_4=!P2_4;   
  137.             TR1=0;   
  138.             time=TH1;   
  139.             time =(time <<8)+TL1;;   
  140.             TL1=0;   
  141.             TH1=0;   
  142.             TR1=1;   
  143.             if ((time>msec_2p5)||(time<msec_0p9))// if  > 2.5msec or shorter than .9Msec bad data , go to idle    
  144.             {      
  145.                 IRstate=IR_idle;   
  146.                 break;    
  147.             }   
  148.             if (time>msec_1p68)// greater than 1.68Msec is a 1    
  149.             {        
  150.                 _IRaddr|= bits;   
  151.             }   
  152.             bits=bits<<1;   
  153.             if (!bits)   
  154.             {   
  155.                 IRstate=IR_getdata;;                   
  156.                 bits=1;   
  157.             }   
  158.             break;                 
  159.         case IR_getdata:   
  160.             TR1=0;   
  161.             time=TH1;   
  162.             time =(time <<8)+TL1;;   
  163.             TL1=0;   
  164.             TH1=0;   
  165.             TR1=1;   
  166.             if ((time>msec_2p5)||(time<msec_0p9))// if  > 2.5msec or shorter than .9Msec bad data , go to idle   
  167.             {       
  168.                 IRstate=IR_idle;   
  169.                 break;    
  170.             }   
  171.             if (time>msec_1p68)// greater than 1.68Msec is a 1   
  172.             {         
  173.                 IRdata|= bits;   
  174.             }   
  175.             bits=bits<<1;   
  176.             if (!bits)   
  177.             {   
  178.                 IRstate=IR_getdatainv;                   
  179.                 bits=1;   
  180.             }   
  181.             break;                 
  182.         case IR_getdatainv:   
  183.             TR1=0;   
  184.             time=TH1;   
  185.             time =(time <<8)+TL1;;   
  186.             TL1=0;   
  187.             TH1=0;   
  188.             TR1=1;   
  189.             if ((time>msec_2p5)||(time<msec_0p9)) // if  > 2.5msec or shorter than .9Msec bad data , go to idle    
  190.             {      
  191.                 IRstate=IR_idle;   
  192.                 break;    
  193.             }   
  194.             if (time>msec_1p68)// greater than 1.68Msec is a 1    
  195.             {        
  196.                 _IRdata|= bits;   
  197.             }   
  198.             bits=bits<<1;   
  199.             if (!bits)         // we have it all , now we make sure it is a NEC code from the CHS IR transmitter   
  200.             {                   // make sure address,~address are correct , data ,~data are correct and address is 0.   
  201.                 IR_ready=((IRaddr^_IRaddr)==0xff)&&((IRdata^_IRdata)==0xff)&&(IRaddr==0);   
  202.                 if(IR_ready)   
  203.                 {   
  204.                     IRstate=IR_idle;   
  205.                 }       
  206.             }   
  207.             break;                 
  208.         default:   
  209.             IRstate=IR_idle;   
  210.             break;   
  211.     }   
  212. }   
  213.   
  214. void main(void)   
  215. {   
  216.     cpu_init();   
  217.     while(1)   
  218.     {   
  219.         if(IR_ready)   
  220.         {   
  221.             IR_ready=0;   
  222.             switch(IRdata)   
  223.             {   
  224.                 case 0x45:        //1   
  225.                     //your code   
  226.                     break;   
  227.                 case 0x44:        //3   
  228.                     //your code   
  229.                     break;   
  230.                 case 0x43:       //4   
  231.                     //your code   
  232.                     break;   
  233.                 case 0x08:        //prev   
  234.                     //your code   
  235.                     break;   
  236.                 case 0x5a:        //next   
  237.                     //your code   
  238.                     break;   
  239.                 default:   
  240.                     break;   
  241.          &n bsp;  }   
  242.         }   
  243.     }   
  244. }   

Tags:keil,红外,遥控解码,程序  
责任编辑:admin
请文明参与讨论,禁止漫骂攻击,不要恶意评论、违禁词语。 昵称:
1分 2分 3分 4分 5分

还可以输入 200 个字
[ 查看全部 ] 网友评论
推荐文章
最新推荐
热门文章
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 在线帮助 - 文章列表
返回顶部
刷新页面
下到页底
晶体管查询