单片机——红外遥控器——代码

#include 
   
   
    
    
#include 
    
    
     
     
#include "./delay/delay.h"
#include "./lcd1602/lcd1602.h"

unsigned int irtime = 0;
bit startflag = 0;
unsigned char irdata[33];
unsigned char ircood[4];
bit irok = 0;
sbit IR = P3^2;

void timer0()
{
      EA = 1;
      TMOD |= 0x02;    
      TH0 = 0;
      TL0 = 0;
      ET0 = 1;
      TR0 = 1;
}

void int0()
{
      EA = 1;
      IT0 = 1;
      EX0 = 1;
}

void int0_isr() interrupt 0
{
      static unsigned char bitnum = 0;
      if(1 == startflag)
      {
          if((irtime > 40) && (irtime < 60))
         {
              bitnum = 0;
          }
        irdata[bitnum] = irtime;
        bitnum++;
        irtime = 0;
        if(33 == bitnum)
        {
            irok = 1;
            bitnum = 0;
            startflag = 0;
        }
     }
    else
    {
        startflag = 1;
        irtime = 0;
    }
}
void timer0_isr() interrupt 1
{
    irtime++;
}
unsigned char irprocess()
{
      unsigned char i,j,k;
      unsigned char temp;
      k = 1;

      for(j = 0; j < 4; j++)
      {
            for(i = 0; i < 8; i++)
            {
                temp >>= 1;
               if(irdata[k] > 6)
               {
                     temp += 0x80;
                }
               k++;
             }
            ircood[j] = temp;
    }   
    if((ircood[0] + ircood[1] == 0xff)  && (ircood[2] + ircood[3] == 0xff))
    {
        switch(ircood[2])
        {
                      case 0x16: return 0;
                      case 0x0c: return 1;
                      case 0x18: return 2;
                      case 0x5e: return 3;
                      case 0x08: return 4;
                      case 0x1c: return 5;
                      case 0x5a: return 6;
                      case 0x42: return 7;
                      case 0x52: return 8;
                      case 0x4a: return 9;
                      case 0x45: return 10;
                      case 0x46: return 11;
                      case 0x47: return 12;
                      case 0x44: return 13;
                      case 0x40: return 14;
                      case 0x43: return 15;
                      case 0x07: return 16;
                      case 0x15: return 17;
                      case 0x09: return 18;
                      case 0x19: return 19;
                      case 0x0d: return 20;
                        default: break;
         }
    }
    return 0xff;

}
void display()
{
      unsigned char high,low;
      unsigned char i;
      for(i = 0; i < 4; i++)
      {
              high = ircood[i]/16;
              low = ircood[i]%16;
              if(high > 9)
             {
                 high += 0x37;
              }
            else
            {
                 high += 0x30;
             }
            if(low > 9)
            {
                 low += 0x37;
             }
            else
            {
                  low += 0x30;
             } 
            lcd_print(1, 2*i+1, high);
            lcd_print(1, 2*i + 2, low);
     }
}
void irdisplay(unsigned char num)
{
        unsigned char high,low;
        high = num/10 +0x30;
        low = num%10 + 0x30;

        lcd_print(2, 1, high);
        lcd_print(2, 2, low);
}

void main()
{
      unsigned char keynum;
      lcd_init();
      timer0();
      int0();
      while(1)
      {
          if(1 == irok)
          {
               irok = 0;
              keynum = irprocess();
              display();
              if(keynum != 0xff)
              {
                     irdisplay(keynum);
               }
           }
       }
}
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值