找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1559|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

數(shù)碼管遙控解碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:146918 發(fā)表于 2016-11-8 22:02 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#include<reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long


#define                Nop()                {_nop_(); _nop_(); _nop_(); _nop_(); _nop_();}



volatile ulong IRcode=0x00000000;  //32位的鍵代碼
volatile ulong Irdcode=0x00000000;
volatile uint customcode=0x0000;      //16位用戶碼
volatile uint time_us=0x0000;      //兩個(gè)下降沿之間的時(shí)間

volatile uchar timeH,timeL;  //保存TH1 TL的值

uchar Lcustomcode;      //低8用戶碼
uchar Hcustomcode;     //高8
uchar datacode;    //8位鍵數(shù)據(jù)碼
uchar mycode;
uchar Rdatacode;      //8位鍵數(shù)據(jù)反碼
uchar uc1ms;
uchar uc10ms;
uchar uc3ms;


uchar  ucDispTime;
uchar  ucDispOrder;
uchar  ucDispCon;
uchar ucSpeakerTime;
unsigned char code LedData[16] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
unsigned char code LedCon[2] = {0x8f,0x4f};
unsigned char ucDispData[2];        // 存放顯示數(shù)據(jù)
sbit led1 = P2^7;
sbit led2 = P2^6;
sbit power=P1^4;  //電源開(kāi)關(guān)
sbit BEEP= P3^6;
bit NewIRcode=0;  //指示當(dāng)處理完了32位碼后,就有了新的遙控碼
bit DataRight=0;   //為1時(shí)讀取數(shù)據(jù)正確
bit bSampleOk;
bit bSampling;
bit b10msInt;
bit  b1msInt;
bit bKeySound;
bit b1msMain;
bit IR_E;           //表示有新的遙控鍵控下就更新掃描數(shù)據(jù)
bit b3msint;

void SendDataToDispDevice();
void Ir_process();
void display();
void beeping();

/****** 晶振為12 MHZ*******/
/**定時(shí)器1,12 MHZ最大定時(shí)為65.53ms***/
//------------------------------------------------------------------------------
void init()
{
  IP=0x09;   //定時(shí)器1,外部中斷0優(yōu)先級(jí)高
  TMOD=0x11; //定時(shí)器0,工作方式1 ;  定時(shí)器1,工作方式1
  TCON=0x01; //外中斷0下降沿觸發(fā),(包括TR1=0,TR0=0)
  TH0=0xff;  //初始化定時(shí)器0,定時(shí)0.2ms
  TL0=0x47;
  TH1=0x00;  //初始化定時(shí)器1
  TL1=0x00;

  EA=1;    //開(kāi)全中斷
  ET0=1;   //開(kāi)放T0中斷
  ET1=1;      //開(kāi)放T1中斷
  EX0=1;   //開(kāi)放INT0
  TR1=0;
  TR0=1;

}
//--------------------------------------
void TimeProg(void)
{      
        b1msMain = b1msInt;
           b1msMain=0;       
        b10msInt = 0;


        if(b1msInt == 1)
        {  
         b1msMain=1;
           if(++uc10ms == 10)       
                {
                        uc10ms = 0;
                        b10msInt = 1;
                        if(bKeySound==1)
                  {        beeping();
                        bKeySound=0;
                        }
                }       
       
        }
}//void TimeProg(void)
//-------------------------------------interrupt0-------------------------------------
void IR_ISR() interrupt 0 using 1 //遙控器中斷處理函數(shù)
{
  static uchar cn;

  TR1=0;
  timeH=TH1;
  timeL=TL1;
  TH1=0;
  TL1=0;
  TR1=1;                     //開(kāi)定時(shí)器中斷1

  time_us=(unsigned int)timeH;
  time_us=time_us<<8;
  time_us=time_us|timeL;

  if(time_us>12200&&time_us<13000) {cn=1;IRcode=0;}  //遇到引導(dǎo)碼,就把cn清0,IRcode清0
        //引導(dǎo)碼的時(shí)間長(zhǎng)度為9ms+4.5ms
  if(cn<34)
      {
          if(time_us>950&&time_us<1120)                   //0
           {
             IRcode=IRcode|0x00000000;
             if(cn<33) IRcode=IRcode>>1;
           }

      else if(time_us>1920&&time_us<2120)        //1t > 1950 && t < 2150
          {
             IRcode=IRcode|0x80000000;
             if(cn<33) IRcode=IRcode>>1;
           }


  //else if(time_us>10000&&time_us<11000) {Irdcode=IRcode;cn=34; }   //遇到重復(fù)碼
                                               //cn用于記錄接收到的數(shù)據(jù)位
    }
        cn++;
  if(cn==34)
   { NewIRcode=1;
     TR1=0;  
       
     Irdcode= IRcode;  cn=0;  
        }  //讀完32位碼,則有新碼產(chǎn)生

}

//--------------------------------------timer_ISR------------------------------
void Timer0_ISR() interrupt 1 using 2  //定時(shí)器0中斷函數(shù)
{
  TR0=0;
  TH0=0xff;  //初始化定時(shí)器0,定時(shí)0.2ms  晶振為11.0592 MHZ
  TL0=0x47;
  TR0=1;
  if(++uc1ms == 5)
        {
                uc1ms = 0;
        b1msInt=1;
                 if(++uc3ms==8)
                  {
                   uc3ms=0;
                   b3msint=1;
                   SendDataToDispDevice();     //n* ms送一次顯示
                   }
                               
               
        }
}//void Timer0IntProg() interrupt 1 using 1

void Timer1_ISR() interrupt 3 using 3  //定時(shí)器1中斷函數(shù)
{
  TR1=0;
  TH1=0x00;  //初始化定時(shí)器1
  TL1=0x00;
  TR1=1;

}
//--------------------SendDataToDispDevice----------
void SendDataToDispDevice()
{
unsigned char n;
    //watchdog();
        if(++ucDispOrder >= 2)  ucDispOrder = 0; // 下一顯示巡回
       
        // 下面為發(fā)送控制數(shù)據(jù)          位控

         if(ucDispOrder==0)
              {led1=0;
           led2=1;
               Nop();
            Nop();
                  
                   }
         if(b3msint==1)
       {if(ucDispOrder==1)
               {led2=0;
            led1=1;
                    Nop();
            Nop();
                    }
                }
        // 下面為發(fā)送顯示數(shù)據(jù)
      n = LedData[ucDispData[ucDispOrder]];
      P0=n;


       
} //void SendDataToDispDevice()

//------------------------------------main()----------------------------------------------
void main()
{

    init();
  beeping();
while(1)
  {
     TimeProg();
     Ir_process();
     display();

  }

}

void Ir_process()

{

  if(NewIRcode==1)    //如果有新的遙控碼就讀
  {
    NewIRcode=0; //讀完之后清零,表示新碼已讀
    customcode=(Irdcode>>16);    //取紅外碼中的按碼鍵
                    //取低8位用戶碼
      Lcustomcode=customcode>>8;//取低8位按碼鍵
    datacode=(unsigned char)(customcode&0x00ff);  //取高8位按碼鍵


       Rdatacode=Lcustomcode;   //取低8位按碼鍵的反碼

      if(~Rdatacode!=datacode)
          {  DataRight=0;
             Irdcode=0;
             datacode=Rdatacode=0;
          } //校驗(yàn)用戶碼,反碼

     else
         { DataRight=1;
           IR_E=1;
           mycode=datacode;}
     if(DataRight==1)  { bKeySound = 1;DataRight=0;  }

     }



}

void display()
  {
  /*  unsigned char a[2];
        a[0] = mycode & 0x0f;
        mycode = mycode >> 4;
        a[1] = mycode & 0x0f;
        ET0 = 0;
        ucDispData[0] = a[0];
        ucDispData[1] = a[1];
        ET0 = 1;*/
        if(IR_E==1)
        {
        ET0 = 0;
        ucDispData[0] = mycode & 0x0f;
        mycode = mycode >> 4;
        ucDispData[1] = mycode & 0x0f;
    IR_E=0;
        ET0 = 1;
        }
}

/**********************************************************/
void delay(unsigned char x)    //x*0.14MS
{
unsigned char a;
  while(x--)
{
  for (a = 0; a<13; a++) {;}
}
}


/**********************************************************/
void beeping()
{
  unsigned char i;

  for (i=0;i<100;i++)
   {
     delay(4);
     BEEP=!BEEP;                 //BEEP取反
   }
  BEEP=1;
                    //關(guān)閉蜂鳴器
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:138282 發(fā)表于 2016-11-8 22:08 | 只看該作者
幫頂,有電路圖嗎?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表