日韩欧美视频第二区,秋霞成人午夜鲁丝一区二区三区,美女日批视频在线观看,av在线不卡免费

電子開發(fā)網(wǎng)

電子開發(fā)網(wǎng)電子設(shè)計 | 電子開發(fā)網(wǎng)Rss 2.0 會員中心 會員注冊
搜索: 您現(xiàn)在的位置: 電子開發(fā)網(wǎng) >> 電子開發(fā) >> EDA開發(fā)應(yīng)用 >> Keilc >> 正文

keil c51紅外遙控解碼程序

作者:佚名    文章來源:本站原創(chuàng)    點擊數(shù):    更新時間:2011-3-3

    本keil c51程序適用uPC1621/uPC1622及兼容的紅外遙控器芯片,占用外部中斷0和定時器1,以中斷方式解碼,節(jié)省系統(tǒng)資源,以查詢方式檢測遙控信號是否有效.

解碼思路:
    紅外線經(jīng)一體化接受頭解碼放到后送到單片機的外部中斷0,單片機設(shè)置外部中斷下降沿觸發(fā),T0和T1為16位定時器,T0在系統(tǒng)啟動后定時5ms.T1在外部中斷0啟動后開始定時,初值為0,每次在INT0中斷后先讀T1計數(shù)值,并重設(shè)初值為0,而且判斷T1的計數(shù)值,

代碼
  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 個字
[ 查看全部 ] 網(wǎng)友評論
推薦文章
最新推薦
熱門文章
關(guān)于我們 - 聯(lián)系我們 - 廣告服務(wù) - 友情鏈接 - 網(wǎng)站地圖 - 版權(quán)聲明 - 在線幫助 - 文章列表
返回頂部
刷新頁面
下到頁底
晶體管查詢
主站蜘蛛池模板: 耒阳市| 水富县| 商都县| 平邑县| 来宾市| 浏阳市| 乐清市| 鄱阳县| 饶阳县| 临澧县| 邹城市| 二连浩特市| 东方市| 杭锦后旗| 沁源县| 海南省| 巴里| 明溪县| 双柏县| 永宁县| 资兴市| 郴州市| 青神县| 泾川县| 阿鲁科尔沁旗| 理塘县| 恩平市| 通渭县| 绥德县| 湄潭县| 阳谷县| 原平市| 玛纳斯县| 新河县| 丹寨县| 巨野县| 手机| 特克斯县| 咸阳市| 通榆县| 深水埗区|