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

QQ登錄

只需一步,快速開始

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

紅外遙控解碼數(shù)碼管顯示實(shí)驗(yàn)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:146918 發(fā)表于 2016-11-8 22:04 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
      #include <reg51.h>
      #define c(x) (x*110592/120000)
      sbit Ir_Pin=P3^2;
          sbit beep=P3^6;
      unsigned char code Led_Tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
              0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf};                        //共陽極數(shù)碼顯示碼0-F.
      unsigned char code Led_Sel[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
      unsigned char Led_Buf[7]; //顯示緩沖區(qū)
      char Led_Index;                   //位選         
      unsigned char Ir_Buf[4]; //用于保存解碼結(jié)果
                 void delay_50ms(unsigned int t)
{
unsigned int j;   
for(;t>0;t--)
  for(j=6245;j>0;j--)
{;}
}

      //==============================================================
      //數(shù)碼管掃描
      timer0() interrupt 1 using 1
      {
      TL0=65536-1000;
      TH0=(65536-1000)/256; //定時(shí)器0設(shè)定約1000us中斷一次,用于數(shù)碼管掃描
      P0=0xff;
      P2=Led_Sel[Led_Index];                            //位選
      P0=Led_Tab[Led_Buf[Led_Index]];                    //段選
      if(++Led_Index>7) Led_Index=0;                    //八個(gè)掃描完了,到第一個(gè)數(shù)碼管

      }
      //==============================================================
      unsigned int Ir_Get_Low()
      {
      TL1=0;
      TH1=0;
      TR1=1;
      while(!Ir_Pin && (TH1&0x80)==0);               
      TR1=0;           
      return TH1*256+TL1;
      }
      //=============================================================
      unsigned int Ir_Get_High()
      {
      TL1=0;
      TH1=0;
      TR1=1;
      while(Ir_Pin && (TH1&0x80)==0);
      TR1=0;
      return TH1*256+TL1;
      }
      //==============================================================
      main()
      {
      unsigned int temp;
      char i,j;
      Led_Index=0;
      TMOD=0x11;
      TL0=65536-1000;
      TH0=(65536-1000)/256; //定時(shí)器0設(shè)定約1000us中斷一次,用于數(shù)碼管掃描
      EA=1;
      ET0=1;
      TR0=1;

      //Led_Buf[0]=0;
      //Led_Buf[1]=0;
      //Led_Buf[2]=0;
      //Led_Buf[3]=0; //顯示區(qū)設(shè)成0
         // Led_Buf[4]=0;
      //Led_Buf[5]=0;
      //Led_Buf[6]=0;
      //Led_Buf[7]=0; //顯示區(qū)設(shè)成0
      do{
      restart:
      while(Ir_Pin);
      temp=Ir_Get_Low();
      if(temp<c(8500) || temp>c(9500)) continue;//引導(dǎo)脈沖低電平9000
      temp=Ir_Get_High();
      if(temp<c(4000) || temp>c(5000)) continue;//引導(dǎo)脈沖高電平4500
      for(i=0;i<4;i++) //4個(gè)字節(jié)
      for(j=0;j<8;j++) //每個(gè)字節(jié)8位
      {
      temp=Ir_Get_Low();
      if(temp<c(200) || temp>c(800)) goto restart;
      temp=Ir_Get_High();
      if(temp<c(200) || temp>c(2000)) goto restart;
      Ir_Buf[i]>>=1;
      if(temp>c(1120)) Ir_Buf[i]|=0x80;
      }
          Led_Buf[0]=(Ir_Buf[0]/16)&0xf;//顯示結(jié)果
      Led_Buf[1]=Ir_Buf[0]&0xf;           //顯示結(jié)果
      Led_Buf[2]=(Ir_Buf[1]/16)&0xf; //顯示結(jié)果
      Led_Buf[3]=Ir_Buf[1]&0xf;                 //顯示結(jié)果
      Led_Buf[4]=(Ir_Buf[2]/16)&0xf; //顯示結(jié)果
          Led_Buf[5]=Ir_Buf[2]&0xf;                 //顯示結(jié)果
      Led_Buf[6]=(Ir_Buf[3]/16)&0xf; //顯示結(jié)果
      Led_Buf[7]=Ir_Buf[3]&0xf;                 //顯示結(jié)果
         beep=0;
         delay_50ms(2);
           beep=1;
      }
          while(1);

      }

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

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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