找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2120|回复: 1
收起左侧

分享一个STC单片机的NEC遥控解码程序与线路

[复制链接]
ID:685007 发表于 2020-3-14 18:39 | 显示全部楼层 |阅读模式
解码NEC遥控,由串口输出,采用STC超小型单片机
分享了线路图,原代码,HEX码
51hei.png
单片机源程序如下:
  1. #include <main.h>
  2. //#include <reg52.h>
  3. #include <stdio.h>

  4. void Timer2Init(void)                //30??@24.000MHz
  5. {
  6.         AUXR &= 0xFB;                //?????12T??
  7.         T2L = 0xA0;                //??????
  8.         T2H = 0x15;                //??????
  9.         AUXR |= 0x10;                //???2????
  10.         EA=1;
  11.         IE2=0x04|IE2;
  12.         TR2=1;
  13. }





  14. /*----------------------------
  15. UART 中断服务程序
  16. -----------------------------*/
  17. void Uart() interrupt 4 using 1
  18. {
  19.     if (RI)
  20.     {
  21.         RI = 0;                 //清除RI位
  22.         P0 = SBUF;              //P0显示串口数据
  23.         P22 = RB8;              //P2.2显示校验位
  24.     }
  25.     if (TI)
  26.     {
  27.         TI = 0;                 //清除TI位
  28.         busy = 0;               //清忙标志
  29.     }
  30. }

  31. /*----------------------------
  32. 发送串口数据
  33. ----------------------------*/
  34. void SendData(BYTE dat)
  35. {
  36.     while (busy);               //等待前面的数据发送完成
  37.     ACC = dat;                  //获取校验位P (PSW.0)
  38.     if (P)                      //根据P来设置校验位
  39.     {
  40. #if (PARITYBIT == ODD_PARITY)
  41.         TB8 = 0;                //设置校验位为0
  42. #elif (PARITYBIT == EVEN_PARITY)
  43.         TB8 = 1;                //设置校验位为1
  44. #endif
  45.     }
  46.     else
  47.     {
  48. #if (PARITYBIT == ODD_PARITY)
  49.         TB8 = 1;                //设置校验位为1
  50. #elif (PARITYBIT == EVEN_PARITY)
  51.         TB8 = 0;                //设置校验位为0
  52. #endif
  53.     }
  54.     busy = 1;
  55.     SBUF = ACC;                 //写数据到UART数据寄存器
  56. }


  57. /*----------------------------
  58. 发送字符串
  59. ----------------------------*/
  60. void SendString(char *s)
  61. {
  62.     while (*s)                  //检测字符串结束标志
  63.     {
  64.         SendData(*s++);         //发送当前字符
  65.     }
  66. }




  67. void Sendint(unsigned int DATA)
  68. {unsigned char UPDATA,LODATA;
  69.         LODATA=DATA&0xff;
  70.         UPDATA=(DATA&0xff00)>>8;
  71.         SendData(UPDATA);
  72.         SendData(LODATA);
  73.         SendData(0x0D);
  74.         SendData(0x0A);
  75. }


  76. /*****************************************************************************************************
  77. Function name:   Delay
  78. Input:           t
  79. output:          None
  80. By:              Tao JC
  81. Date:            06-Apr2005
  82. *******************************************************************************************************/

  83. void   delay(unsigned long t)
  84. {
  85.         t*=10;
  86.         while (t--)        ;
  87. }
  88. /**********************************************************************************************************
  89. *******************************************************************************************************/


  90. void init_counter(void)
  91. {

  92. //        TCON=0x55;
  93. //        IE=0x87;
  94.         IP=0x01;
  95. //        TH0=0x00;
  96. //        TL0=0x00;
  97.         TH0=RELOAD_HI;    //reload timer               reset timer
  98.         TL0=RELOAD_LO;    //reload timer               reset timer
  99.         
  100.         TR0=1;                  //start timer


  101. //        T2CON=0x00;
  102. //        ET2=1;
  103. //        RCAP2H=0x00;
  104. //        RCAP2L=0x00;
  105. //        TR2=1;
  106. }



  107. /*****************************************************************************************************
  108. Function name:   Time_out
  109. Input:           RELOAD_HI,RELOAD_LO
  110. output:          Counter value
  111. By:              Tao JC
  112. Date:            22-Jan-2006
  113. Function:        count the pluse width of the IR input
  114. *******************************************************************************************************/


  115. void Time_out() interrupt 1{
  116.         TR0=0;     
  117. //  RED_LED=0;
  118. //         GRN_LED=0;
  119.         //if timer out, stop timer
  120. /*        
  121. if(PowerKeyPress)
  122. counter++;
  123. if (counter >=60)
  124. {PWR_BTN=0;
  125.         GRN_LED=0;
  126.         RED_LED=0;
  127. counter=0;
  128. PowerKeyPress=0;
  129. }

  130.         counter++;
  131.         if(counter<=300)
  132.         {RED_LED=0;
  133.          GRN_LED=0;
  134.         }
  135.         if(counter>=600)
  136.         {RED_LED=1;
  137.          GRN_LED=1;
  138.          counter=0;        
  139.         }
  140. */        
  141.         TH0=RELOAD_HI;                         //reset timer
  142.         TL0=RELOAD_LO;                         //reset timer
  143. //        TH0=0xff;                         //reset timer
  144. //        TL0=0xff;                         //reset timer

  145.         Pulse_Qty = Pulse_index;
  146.         if (Pulse_Qty ==4)
  147.         {
  148.                 EX0=0;
  149.                 Cont_Key_Pressed = 1;
  150.         }
  151.         Pulse_index=0;                         //clear data
  152.         // Output_Switch=1;                    //output data is allowed
  153.         TR0=1;
  154.         Remote_DataType = 0;        // 1: Key Pressed, 2: Repeat Key Pressed
  155.         Remote_Data = 0x00ff;
  156.         
  157. }

  158. void ExtInt0Init(void)                //30??@24.000MHz
  159. {
  160.     IT0 = 0;                    //??INT0????? (1:???? 0:???????)
  161.     EX0 = 1;                    //??INT0??
  162.     EA = 1;
  163. }

  164. /*****************************************************************************************************
  165. Function name:   IR interrupt routine
  166. Input:           IR_IN
  167. output:          DATA_OUT
  168. By:              Tao JC
  169. Date:            15-May-2011  modify to fit the NEC code
  170. Function:        if any clock on the P3.3, trig the DATA-OUT
  171. *******************************************************************************************************/
  172. void IR_int() interrupt 0
  173. {
  174.         TR0=0;            //stop timer
  175.   EX0=0;
  176.         RED_LED=0;
  177.         
  178.         // Lead code = 9ms + 4.5ms, 1 cycle >= 0.5us
  179.         T_Pulse[Pulse_index]=(TH0-RELOAD_HI)*256 +(TL0-RELOAD_LO);                // get the counter value
  180. //        if(T_Pulse[Pulse_index]>(128*NECT))                                                        // NECT = 250,
  181.         if(T_Pulse[Pulse_index]>(80*NECT))                                                                // NECT = 250,
  182.         {                                                                                                                                                                                                        // 80*250 = 20000, 20000*0.5 = 10ms
  183.                                                                                                                                                                                                                 // 128 *250 = 32000,
  184.                 Pulse_index=0;
  185.                 Remote_DataType = 0;        // 1: Key Pressed, 2: Repeat Key Pressed
  186.                 Remote_Data = 0x00ff;
  187. /*                if (First_Int == 1)
  188.                 {
  189.                         First_Int = 0;
  190.                         Pulse_index=1;
  191.                         T_Pulse[Pulse_index] = 0x4666;
  192.                 }
  193. */               
  194.         }
  195.         else
  196.         {
  197.                 if(T_Pulse[Pulse_index]>(64*NECT))                                                        // 64 *250 = 16000,16000*0.5 = 8ms
  198.                 {                                                                                                                                                                                                // 1 cycle = 0.5625
  199.                         Pulse_index=1;
  200.                         //T_Pulse[Pulse_index]=(TH0-RELOAD_HI)*256 +(TL0-RELOAD_LO);
  201.                 }
  202.         }
  203.         
  204.         if(Pulse_index<=68)
  205.         {        
  206.                 Pulse_index++;
  207.                 //printf("pulse index= %bu \n", Pulse_index );
  208.         }

  209.         if(Pulse_index>=68)
  210.         {
  211.                 Output_Switch=1;
  212.                 RED_LED=1;
  213.                 //printf("output switch=1\n");
  214.                 Pulse_index=0;
  215.         }

  216.         TH0=RELOAD_HI;    //reload timer               reset timer
  217.         TL0=RELOAD_LO;    //reload timer               reset timer

  218.         XOR=!XOR;                        //wait for next pulse
  219.         //GRN_LED=XOR;
  220.         IP=0x01;          //set interrupt priority =1

  221. //TR0=1;           //start timer

  222.         if (Output_Switch == 1)
  223.         {
  224.     EX0=0;
  225.           TR0=0;           //stop timer
  226.         }
  227.         else
  228.         {
  229.                 EX0=1;
  230.                 TR0=1;
  231.         }

  232. }



  233. void Timer2_init_baud()
  234. {        
  235. ACC = P_SW1;
  236. ACC &= ~(S1_S0 | S1_S1);    //S1_S0=0 S1_S1=0
  237. P_SW1 = ACC;                //(P3.0/RxD, P3.1/TxD)  

  238. #if (PARITYBIT == NONE_PARITY)
  239.     SCON = 0x50;                //8位可变波特率
  240. #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
  241.     SCON = 0xda;                //9位可变波特率,校验位初始为1
  242. #elif (PARITYBIT == SPACE_PARITY)
  243.     SCON = 0xd2;                //9位可变波特率,校验位初始为0
  244. #endif

  245.     T2L = (65536 - (FOSC/4/BAUD));   //设置波特率重装值
  246.     T2H = (65536 - (FOSC/4/BAUD))>>8;
  247.     AUXR = 0x14;                //T2为1T模式, 并启动定时器2
  248.     AUXR |= 0x01;               //选择定时器2为串口1的波特率发生器
  249.     ES = 1;                     //使能串口1中断
  250.     EA = 1;

  251. }




  252. /**********************************************************************
  253. Function name: timer 2 interrupt
  254. Input:         internal generate 31.25ms timer signal
  255. Output:        toggle BLINK
  256. By:            Tao JC
  257. Date:          31-May-2005
  258. ****************************************************************************/


  259. void system_tick(void) interrupt 12{
  260.         TF2=0;
  261.         TR2=0;      //stop timer
  262.         RCAP2H=RELOAD_HI_T2;   //reload timer high
  263.         RCAP2L=RELOAD_LO_T2;    //Reload timer low
  264.         TR2=1;             //start timer 0
  265.         

  266.         if(Second_Count)
  267.         {
  268.                 Second_Count--;
  269.                 if(Second_Count==0)
  270.                 {
  271.                         Second_Count=ONOFFDURATION;
  272.                         GRN_LED=1;
  273.                         RED_LED=1;
  274.                         PWR_BTN=0;
  275.                 } // 1.25 s, change step once
  276.         }

  277. }
  278.         
  279. /******************************************************************************************************/
  280. /*****************************************************************************************************/


  281. void Timer0Init(void)                //30??@24.000MHz
  282. {
  283.         AUXR &= 0x7F;                //?????12T??
  284.         TMOD &= 0xF0;                //???????
  285.         TL0 = 0xA0;                //??????
  286.         TH0 = 0x15;                //??????
  287.         TF0 = 0;                //??TF0??
  288.         TR0 = 1;                //???0????
  289.         ET0=1;
  290. }




  291. void System_Init()
  292. {
  293. //init_uart();
  294.         Timer0Init();
  295.         Timer2Init();
  296.         ExtInt0Init();
  297.         KeyInput=0xffff;
  298.         P0=0xFF;
  299.         UART_INT=0;
  300.         PWR_BTN=0;
  301.   TR2=1;

  302. //  init_counter();
  303.         
  304.         
  305.         Timer2_init_baud();

  306.         
  307.         SendString("****Baud 115200 **********\r\n");
  308.         SendString("**IR SENSOR HW Version 1.0***\r\n");
  309.         SendString("**IR SENSOR FW Version 1.4***\r\n");
  310.         SendString("****************************\r\n");

  311. //        GRN_LED=0;
  312. //        RED_LED=0;
  313.         First_Int = 1;
  314.         Cont_Key_Pressed = 0;
  315.         
  316.   Pulse_index=0;
  317.   XOR=1;              //reset the 74LS86
  318.   Output_Switch=0;
  319.   Error_Flag=0;

  320.         Remote_DataType = 0;        // 1: Key Pressed, 2: Repeat Key Pressed
  321.         Remote_Data = 0x00ff;
  322.         
  323.         Repeat_Key_Counter = 0;






  324. }

  325. char xlat2char(int x)
  326. {
  327.         char res = 0xff;
  328.         if (x>=0 && x<=9){
  329.                 res = x+'0';
  330.         } else if(x >= 0xA && x <= 0xF) {
  331.                 res = 'a' + (x-0xA);
  332.         }
  333.         
  334.         return res;
  335. }


  336. void main(void)                         //主程序
  337. {
  338.         unsigned char i=0;
  339.   unsigned int IR_Data;
  340.         unsigned char Temp_Data;
  341.   //unsigned char Error_Pattern;

  342.         System_Init();
  343.         EA=1;
  344.         KeyInput_valid_flag = 0;

  345.   while(1)
  346.   {


  347.                 if (Output_Switch==1)
  348.                 {
  349.             //RED_LED=0;
  350.                         //Lead code check
  351.                         // Lead code = 9ms + 4.5ms
  352. /*
  353.                         printf("location 1,Pulse width= %x \n",T_Pulse[1]);        // 0x4646
  354.                         printf("location 2,Pulse width= %x \n",T_Pulse[2]);        // 0x108a
  355.                         printf("location 3,Pulse width= %x \n",T_Pulse[3]);        // 0x4646
  356.                         printf("location 4,Pulse width= %x \n",T_Pulse[4]);        // 0x108a
  357.                         printf("location 5,Pulse width= %x \n",T_Pulse[5]);        // 0x4646
  358.                         printf("location 6,Pulse width= %x \n",T_Pulse[6]);        // 0x108a
  359.                         printf("location 7,Pulse width= %x \n",T_Pulse[7]);        // 0x4646
  360.                         printf("location 8,Pulse width= %x \n",T_Pulse[8]);        // 0x108a
  361.                         printf("location 9,Pulse width= %x \n",T_Pulse[9]);        // 0x4646
  362.                         printf("location 10,Pulse width= %x \n",T_Pulse[10]);        // 0x108a
  363.                         printf("location 11,Pulse width= %x \n",T_Pulse[11]);        // 0x4646
  364.                         printf("location 12,Pulse width= %x \n",T_Pulse[12]);        // 0x108a
  365.                         printf("location 13,Pulse width= %x \n",T_Pulse[13]);        // 0x4646
  366.                         printf("location 14,Pulse width= %x \n",T_Pulse[14]);        // 0x108a
  367.                         printf("location 15,Pulse width= %x \n",T_Pulse[15]);        // 0x4646
  368.                         printf("location 16,Pulse width= %x \n",T_Pulse[16]);        // 0x108a
  369.                         printf("location 17,Pulse width= %x \n",T_Pulse[17]);        // 0x4646
  370.                         printf("location 18,Pulse width= %x \n",T_Pulse[18]);        // 0x108a
  371.                         printf("location 19,Pulse width= %x \n",T_Pulse[19]);        // 0x4646
  372.                         printf("location 20,Pulse width= %x \n",T_Pulse[20]);        // 0x108a
  373.                         printf("location 21,Pulse width= %x \n",T_Pulse[21]);        // 0x4646
  374.                         printf("location 22,Pulse width= %x \n",T_Pulse[22]);        // 0x108a
  375.                         printf("location 23,Pulse width= %x \n",T_Pulse[23]);        // 0x4646
  376.                         printf("location 24,Pulse width= %x \n",T_Pulse[24]);        // 0x108a
  377.                         printf("location 25,Pulse width= %x \n",T_Pulse[25]);        // 0x4646
  378.                         printf("location 25,Pulse width= %x \n",T_Pulse[26]);        // 0x108a
  379.                         printf("location 27,Pulse width= %x \n",T_Pulse[27]);        // 0x4646
  380.                         printf("location 28,Pulse width= %x \n",T_Pulse[28]);        // 0x108a
  381.                         printf("location 29,Pulse width= %x \n",T_Pulse[29]);        // 0x4646
  382.                         printf("location 30,Pulse width= %x \n",T_Pulse[30]);        // 0x108a
  383.                         printf("location 31,Pulse width= %x \n",T_Pulse[31]);        // 0x4646
  384.                         printf("location 32,Pulse width= %x \n",T_Pulse[32]);        // 0x108a
  385.                         printf("location 33,Pulse width= %x \n",T_Pulse[33]);        // 0x4646
  386.                         printf("location 34,Pulse width= %x \n",T_Pulse[34]);        // 0x108a
  387. */
  388.         
  389.                         if(T_Pulse[1]>(64*NECT))
  390.                         {
  391.                                 if(T_Pulse[2]>(48*NECT)||T_Pulse[2]<(14*NECT))
  392.                                 {
  393.                                         Error_Flag=1;
  394.                                         //printf("error location 2,Pulse width= %x \n",T_Pulse[2]);
  395.                                         //    IR_Data=0xff;
  396.                                         Output_Switch=0;
  397.                                         KeyInput_valid_flag = 0;
  398.                                 }
  399.                         }
  400.                         else
  401.                         {
  402.                                 Error_Flag=1;
  403.                                 //printf("error location 1,pulse width= %x \n",T_Pulse[1] );
  404.                                 // IR_Data=0xff;
  405.                                 Output_Switch=0;
  406.                                 KeyInput_valid_flag = 0;
  407.                         }

  408.                         // NEC decoder
  409.                         i=3;
  410.                         T_Pulse[1]=0;
  411.                         T_Pulse[2]=0;
  412.                         while(i<=65&&Output_Switch)
  413.                         {
  414.                                 // #define SHORTT 1350 = 0.56ms,
  415.                                 // #define LONGT 3000 = 1.69ms
  416.                                 // Data 0 = 0.56ms + 0.565ms = 1.125ms
  417.                                 // Data 1 = 0.56ms + 1.69ms = 2.25ms
  418.                                 if(T_Pulse[i]<SHORTT)               
  419.                                 {
  420.                                         T_Pulse[i]=0;
  421.                         
  422.                                         if(T_Pulse[i+1]<SHORTT)
  423.                                                 Temp_Data=0;
  424.                                         else
  425.                                                 if(T_Pulse[i+1]>LONGT)
  426.                                                         Temp_Data=1;
  427.                                                                
  428.                                         IR_Data=IR_Data<<1;
  429.                                         IR_Data=Temp_Data+IR_Data;
  430.                                         T_Pulse[i+1]=0;
  431.                                         i=i+2;
  432.                                         KeyInput_valid_flag = 1;
  433.                                 }
  434.                                 else                                                                                           //error handle
  435.                                 {
  436.                                         IR_Data=0xff;
  437.                                         Pulse_index=0;
  438.                                         Remote_DataType = 0;        // 1: Key Pressed, 2: Repeat Key Pressed
  439.                                         Remote_Data = 0x00ff;
  440.                                         Output_Switch=0;
  441.                                         //printf("error location 3, Pulse width = %bu,%x \n", i,T_Pulse[i]);
  442.                                         KeyInput_valid_flag = 0;
  443.                                 }
  444.                         }  

  445.                         Pulse_index=0;  //clear the data buffer
  446.                         Remote_DataType = 0;        // 1: Key Pressed, 2: Repeat Key Pressed
  447.                         Remote_Data = 0x00ff;
  448.                                 
  449.                         KeyInput=IR_Data;
  450.                         //printf("KeyInput = %x \n", KeyInput);
  451.                         
  452.                         if (KeyInput_valid_flag == 1)
  453.                         {        
  454. //                                printf("%04x \n", KeyInput);
  455.                                 Repeat_Key_Counter = 0;
  456.                                 Cont_Key_Pressed = 0;
  457.                         }
  458.                         UART_INT=1;
  459.         //                GRN_LED=0;
  460.                         if(KeyInput!=0xff)
  461.                         Output_Switch=0;
  462.                         
  463.                         TH0=RELOAD_HI;    //reload timer               reset timer
  464.                         TL0=RELOAD_LO;    //reload timer               reset timer
  465.                    IP=0x01;          //set interrupt priority =1
  466.                         TR0=1;
  467.                         EX0=1;
  468.                 }
  469.                 else
  470.                 {
  471. /*
  472.                         printf("Output_Switch = 0, Pulse_Qty= %x \n",Pulse_Qty);        //
  473. //                printf("Output_Switch = 0, Remote_test,Pulse width= %x,First_Int = %x \n",Remote_test,First_Int);        //

  474.                         printf("Output_Switch = 0, location 1,Pulse width= %x \n",T_Pulse[1]);        // 0x4646
  475.                         printf("Output_Switch = 0, location 2,Pulse width= %x \n",T_Pulse[2]);        // 0x108a

  476.                         printf("Output_Switch = 0, location 3,Pulse width= %x \n",T_Pulse[3]);        // 0x4646
  477.                         printf("Output_Switch = 0, location 4,Pulse width= %x \n",T_Pulse[4]);        // 0x108a
  478. */
  479.                
  480.                
  481.                 if (Cont_Key_Pressed == 1)
  482.                 {
  483.                         if (Repeat_Key_Counter <= 0)
  484.                         {
  485.                                 Repeat_Key_Counter++;        
  486.                                 Cont_Key_Pressed = 0;

  487.                                 T_Pulse[1] =0;
  488.                                 T_Pulse[2] =0;
  489.                                 T_Pulse[3] =0;
  490.                                 T_Pulse[4] =0;

  491.                                 Pulse_index=0;  //clear the data buffer
  492.                                        
  493.                                 UART_INT=1;
  494.                                 
  495.                                 TH0=RELOAD_HI;    //reload timer               reset timer
  496.                                 TL0=RELOAD_LO;    //reload timer               reset timer
  497.                                 IP=0x01;          //set interrupt priority =1
  498.                                 TR0=1;
  499.                                 EX0=1;
  500.                                 
  501.                         }
  502.                         else
  503.                         {
  504.                                 Cont_Key_Pressed = 0;
  505.                                 T_Pulse[1] =0;
  506.                                 T_Pulse[2] =0;
  507.                                 T_Pulse[3] =0;
  508.                                 T_Pulse[4] =0;

  509.                                 Pulse_index=0;  //clear the data buffer                        
  510.         //                KeyInput=IR_Data;
  511.                                 KeyInput=0x55aa;
  512. //      printf("KeyInput = %x \n", KeyInput);
  513. ……………………

  514. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
code.zip (74.19 KB, 下载次数: 21)
回复

使用道具 举报

ID:686739 发表于 2020-3-15 00:33 来自手机 | 显示全部楼层
学习一下
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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