单片机红外线接收算法

红外线遥控目前在市面上用得比较的广泛,如遥控电视,遥控灯具,遥控玩具等等。

这里主要介绍一下红外遥控接收代码的编写,先看红外接收代码:

  1 void user_gpio_init(void)
  2 {
  3   /*!< Output push-pull, low level, 10MHz */
  4   GPIO_Init(GPIOC, GPIO_PIN_LNIB, GPIO_MODE_OUT_PP_LOW_SLOW);//FI 0..3
  5   GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_SLOW);//BI
  6   /*!< Input pull-up, external interrupt */
  7   GPIO_Init(GPIOC, GPIO_PIN_7, GPIO_MODE_IN_PU_IT);   //Data
  8   /*@brief  Set the external interrupt sensitivity of the selected port.*/
  9   /*!< Interrupt on Falling edge only */
 10   EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC, EXTI_SENSITIVITY_FALL_ONLY);
 11 }
 12 //interrupt routine
 13 void portC_isr(void)
 14 {
 15   static u8 one_byte;
 16   static u8 index;
 17   static u8 bits_of_byte;
 18   switch(IR_State)
 19   {
 20   case IDL:
 21     {
 22       time_125us=0;
 23       IR_State++;
 24       break;
 25     }
 26   case START:
 27     {
 28       if(time_125us>104)//ok , ready to receive data
 29       {
 30         time_125us=0;
 31         one_byte = 0;
 32         index=0;
 33         bits_of_byte = 0;
 34         IR_State++;
 35       }
 36       else IR_State--;
 37       break;
 38     }
 39   case DATA:
 40     {
 41       if((time_125us<14)&&(time_125us>6))//0
 42       {
 43         one_byte <<=1;
 44         bits_of_byte++;
 45       }
 46       else if((time_125us>14)&&(time_125us<23))//1
 47       {
 48         one_byte<<=1;
 49         one_byte+=1;
 50         bits_of_byte++;
 51       }
 52       else
 53       {
 54         IR_State = IDL;
 55       }
 56       time_125us=0;
 57       if(bits_of_byte >= 8)                     // got one byte ready
 58       {
 59         g_u8arr_buf[index++]=one_byte;
 60         time_release_125us=0;
 61         if(index>=4)
 62         {
 63           //__disable_interrupt();
 64           if ((g_u8arr_buf[3]==0x13)&&(g_u8arr_buf[2]==0xEC)&&(g_u8arr_buf[1]==0xff)&&g_u8arr_buf[0]==0x00)//0x00FFEC13
 65           {
 66             if(g_motor_busy!=0x00)
 67             {
 68               MotorStop;
 69               g_motor_busy=0;
 70             }
 71           }
 72           else if ((g_u8arr_buf[3]==0x53)&&(g_u8arr_buf[2]==0xAC)&&(g_u8arr_buf[1]==0xff)&&g_u8arr_buf[0]==0x00)//0x00FFAC53
 73           {
 74             //time_release_125us=0;
 75             if(g_motor_busy!=0x5a)
 76             {
 77               MotorForward;
 78               g_motor_busy=0x5a;
 79             }
 80           }
 81           else if ((g_u8arr_buf[3]==0x33)&&(g_u8arr_buf[2]==0xCC)&&(g_u8arr_buf[1]==0xff)&&g_u8arr_buf[0]==0x00)//0x00FFCC33
 82           {
 83             //time_release_125us=0;
 84             if(g_motor_busy!=0x3c)
 85             {
 86               MotorBackward;
 87               g_motor_busy=0x3c;
 88             }
 89           }
 90           else
 91           {
 92             if(g_motor_busy!=0x00)
 93             {
 94               MotorStop;
 95               g_motor_busy=0;
 96             }
 97           }
 98           IR_State = IDL;
 99           //__enable_interrupt();
100         }
101         bits_of_byte=0;
102         one_byte=0;
103       }
104       break;
105     }
106   default:
107     {
108       IR_State = IDL;
109       break;
110     }
111   }
112 }

红外编码常用格式为:报头为13.5ms信号,之后才开始传送数据。数据的编码:1ms左右代表0或者1,   2ms左右代表1或者0。这个主要看自己的定义了。

这个算法的重点是调好定时器的精度,以及红外线连续读取的灵敏度。

猜你喜欢

转载自www.cnblogs.com/lumao1122-Milolu/p/11411130.html