红外线遥控器原理及编程

版权声明:如需转载请标注 https://blog.csdn.net/weixin_40973138/article/details/88360454

红外线遥控器内部有发射不同对应红外线信号的芯片,而其接收端则是一个光感二极管,该二极管可根据遥控器发射的红外线信号进行相应的电压改变,但是是反相的

故我们只需要在单片机中配置好外部触发中断,然后将二极管连接上相应的中断管脚即可实现引脚电平触发中断

数据格式:

遥控器发出的信号包括三部分:

  1. Leader Code
  2. 系统识别码(共16bits,包括8bits,和反相的8bits)
  3. 操作码(共16bits,包括8bits,和反相的8bits)

接收头接收到的电平也是如此格式,只不过是反相的,其具体电平为:

__________________               _______   _   _   _   _   _   _   _   _   ____   ____   ____   ____   ____   ____   ____   ____                                 ___________________________________               _______   ___________________________________________________________
                  |_____________|       |_| |_| |_| |_| |_| |_| |_| |_| |_|    |_|    |_|    |_|    |_|    |_|    |_|    |_|    |x x x x x x x x|x x x x x x x x|                                   |_____________|       |_|                                                                    
	              |<-    9ms  ->|<4.5ms>|
                  |----leader code------|-----custom code 8bit----------|-----------------custom code' 8bit---------------------| key data 8bit |key data'8bit  |<---------------40ms---------------><----9ms-----><2.1ms>--|--------------- 此时的高电平超过40ms,然后出现9ms的低电平,2.1ms的高电平 连发码----------------

0和1均以0.56ms的低电平开始(实际测量是500us的样子),不同的是后面出现的高电平,
如果高电平是0.56ms(实际测量是500us的样子),则表示0,如果高电平是1.68ms(0.56*3=1.68)则表示1

0.56ms:|_|
      _   _   _ 
0:     |_| |_| |
      _   ____   ____  
1:     |_|    |_|    |_

写代码的时候只需要检测高电平的时间即可。

信号处理

配置外部中断、引脚使能等代码未贴出

遥控器上每个按键所发出的操作码都是不同的,我们便可根据接收到的操作码执行相应的程序。

注意:最好将系统时钟设置为最高等级。

/* We should ensure the piority of Systick is more than other interrupt */
NVIC_SetPriority(SysTick_IRQn, 0);
void EXTI3_IRQHandler(void)
{
  u8 Current_Bit = 0, Leader_code_flag = 0;
  u32 Pulse_Time = 0;
	Frame_Data = 0;

  if(EXTI_GetITStatus(REMOTE_EXTI_LINE))
  {
    while(1)
    {
      if(Get_BitData() == 1)
      {
		/* Compute the high volt time, to judge what kinds */
        Pulse_Time = Get_HighVolt_Time();
				
		/* If the time more than 5ms, we can judge it's out of time */
        if(Pulse_Time >= 500)
        {
          break;
        }
		/* If the time is between 4ms to 5ms, we can judge it's the leader code */
        else if((Pulse_Time>=400) && (Pulse_Time<500)) /* 4ms-5ms: The leader code */
        {
					/* Set the leader code flag */
          Leader_code_flag = 1;
        }
		/* If the time is between 0.2ms to 1ms, we can judge it's the 0 data bit */
        else if((Pulse_Time>=20) && (Pulse_Time<100)) /* 0.52ms(0.2ms-1ms): 0 bit */
        {
          Current_Bit = 0;
        }
		/* If the time is between 1ms to 2ms, we can judge it's the 1 data bit */
        else if((Pulse_Time>=100) && (Pulse_Time<200)) /* 1.69ms(1ms-2ms): 1 bit */
        {
          Current_Bit = 1;
        }
		/* If the time is between 2ms to 4ms, we can judge it's the invalid data */
        else if((Pulse_Time>=200) && (Pulse_Time<400)) /* invalid(2ms-4ms) */
        {
          break;
        }
				LED1_ON;
        if (Leader_code_flag == 1) 
        {
          Frame_Data <<= 1;
          Frame_Data |= Current_Bit;
        }
      }
    }
	/* Indicating we transmit 1 frame of data */
    Frame_Flag = 1;
    EXTI_ClearITPendingBit(REMOTE_EXTI_LINE);
		LED1_OFF;
		Delay(8000); /* 80ms */
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_40973138/article/details/88360454