⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 remote_ir.c

📁 一个使用8051单片机接受遥控器编码的例子
💻 C
字号:

#include <reg52.h>
#include <intrins.h>

#define KEY_NO_KEY        0xff
#define IR_CUSTOMER_CODE_HIGH  0x01
#define IR_CUSTOMER_CODE_LOW  0xfe

extern BYTE idata g_SystemTick;        // system tick, unit is 100 ms       

extern BOOL PIN_IR;

extern void Validate_Continuous_Key(BYTE key);
extern void Save_RxKey(BYTE key);

static BYTE idata timer_keep;

static BYTE state = 0;  
static BYTE count = 0;

#ifdef CLOCK_16_9          // 16.9 MHz ---------------------------------------------------------
  #define ERROR    0.15    // 15%
  #define _1ms    1411
  #define _250us    352
  
  #define _4_5ms_LOW    168   //((4.5 * _1ms * (1-ERROR)) / 32)
  #define _4_5ms_HIGH    201   //((4.5 * _1ms * (1+ERROR)) / 32)
  
  #define _2_25ms_LOW    69     //((2.25 * _1ms * (1-2*ERROR)) / 32)  
  #define _2_25ms_HIGH  128     //((2.25 * _1ms * (1+2*ERROR)) / 32)
  
  #define _1_68ms_LOW    51     //((1.68 * _1ms * (1-2*ERROR)) / 32)  
  #define _1_68ms_HIGH  96     //((1.68 * _1ms * (1+2*ERROR)) / 32)

#else                // 12.288 MHz ---------------------------------------------------------
  #define ERROR    0.15    // 15%
  #define _1ms    1024
  #define _250us    256
  
  #define _4_5ms_LOW    136   //((4.5 * _1ms * (1-ERROR)) / 32)
  #define _4_5ms_HIGH    146   //((4.5 * _1ms * (1+ERROR)) / 32)
  
  #define _2_25ms_LOW    50     //((2.25 * _1ms * (1-2*ERROR)) / 32)  
  #define _2_25ms_HIGH  93     //((2.25 * _1ms * (1+2*ERROR)) / 32)
  
  #define _1_68ms_LOW    37     //((1.68 * _1ms * (1-2*ERROR)) / 32)  
  #define _1_68ms_HIGH  70     //((1.68 * _1ms * (1+2*ERROR)) / 32)
#endif

//---------------------------------------------------------------------------
// Timer 1 Int
//---------------------------------------------------------------------------
static void Timer1_Int() interrupt 3{
  
  if ((!PIN_IR) && (count < 40)){
    count ++;
  }
  else {
    TH1 = 0; TL1 = 0;
    ET1 = 0;    // disable timer 1 int
    TMOD &= 0x0f; TMOD |= B1001_0000;   // Timer 1, Mode 1, Enable Gate control through INT1

    if (count > 24){    // 6ms
      state = 1; 
     }
    else {
      state = 0;
     }
  }

  TH1 = (-_250us) >> 8; TL1 = (-_250us) & 0xff;
}

//---------------------------------------------------------------------------
// Name: Check_IRKey
// Description: This function Read the switch status byte from 16311
// Arguments: 
// Return value: the Switch status byte
//---------------------------------------------------------------------------
static void IRKey_Int() interrupt 2{  // INT1
static BYTE m_IRCodeKeep = KEY_NO_KEY;
BYTE aIRCodes[4];
BYTE bByte;
static BYTE con_cnt;
BYTE temp;

  for (temp =0; temp < 30; temp ++);    // remove the noise
  if (PIN_IR){state =0; return;}

  if (state == 0)
  {            // state 0
    count = 0;
    TH1 = (-_250us) >> 8; TL1 = (-_250us) & 0xff;
    TMOD &= 0x0f; TMOD |= B0001_0000;   // Timer 1, Mode 1, Disable Gate control through INT1
    TR1 = 1; ET1 = 1;          // Enable ET1
  }
  else if (state == 1)
  {  // 
    temp = (TH1 * 0x100 + TL1) >> 5;
    TH1 = 0; TL1 = 0;  // reset the timer 1

    if ((temp > _4_5ms_LOW) && (temp < _4_5ms_HIGH))
    {      // 4.5ms
      state = 2;
      return;
    } else if ((temp > _2_25ms_LOW) && (temp < _2_25ms_HIGH))
    {  // 2.25ms

      if ((g_SystemTick - timer_keep) >= 3) {state = 0; return;}    // If the gap >= 300ms, invalid key
      timer_keep = g_SystemTick;
      if (con_cnt < 5)
      {
        con_cnt ++; 
      }
      else
      { 
        Validate_Continuous_Key(m_IRCodeKeep) ;     // return the previous input key
      }
      state = 0;
      return;
    }      
  }
  else if ((state >= 2) && (state <= 33))
  {  
    if ((state == 2) || (state == 10) || (state == 18) || (state == 26)) bByte = 0;
    bByte >>= 1; // shift right one bit
    temp = (TH1 * 0x100 + TL1) >> 5;
    TH1 = 0; TL1 = 0;  // reset the timer 1
    if ((temp > _1_68ms_LOW) && (temp < _1_68ms_HIGH))
    {    // 1
      bByte |= 0x80;
    }

    // --------------------------------- //
    if (state == 9)
    {
      aIRCodes[0] = bByte; 
    }
    else if (state == 17)
    {
      aIRCodes[1] = bByte;
    }
    else if (state == 25)
    {
      aIRCodes[2] = bByte;
    }
    else if (state == 33)
    {
      aIRCodes[3] = bByte;
      if ((aIRCodes[0] != IR_CUSTOMER_CODE_HIGH) || (aIRCodes[1] != IR_CUSTOMER_CODE_LOW)) 
      {
        state = 0;
        return;     // system code error
      }
      // key remain pressed, only leader code sent
      if ((aIRCodes[0]==0) && (aIRCodes[1]==0) && (aIRCodes[2]==0) && (aIRCodes[3]==0))
      { 
        Validate_Continuous_Key(m_IRCodeKeep) ;     // return the previous input key
        state = 0;
        return;
      }
      else 
      {
        // each bit of aIRCodes[2] will be invert of aIRCodes[3]
        if (aIRCodes[2] == ~aIRCodes[3]) 
        {
          m_IRCodeKeep = aIRCodes[2];
          Save_RxKey (m_IRCodeKeep); 
          con_cnt = 0; 
          timer_keep = g_SystemTick;
          state = 0;
          return;
        }
        else 
        {
          state = 0;
          return;
        }
      }
    }
    state ++; 
  }
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -