欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

專注電子技術學習與研究
當前位置:單片機教程網(wǎng) >> MCU設計實例 >> 瀏覽文章

8051單片機紅外線遙控實驗

作者:佚名   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2013年10月24日   【字體:


如果網(wǎng)頁顯示格式錯亂可從這里下載完整的源程序:http://www.raoushi.com/f/dpjjmhw.rar
//遙控器采用Ht6221芯片
//紅外線遙控      用外部中斷作為紅外線解碼輸入   紅外遙控器1號鍵為電風扇啟動鍵
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
code uchar seg[]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60};   //數(shù)碼管碼表
code uchar tab[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};     //數(shù)碼管位選
code uchar ss[]={0x10,0x03,0x01,0x06,0x09,0x1d,0x1f,0x0d,0x19,0x1b,0x11,0x15,0x17,0x12,0x16,0x4c,0x40,0x48,0x04,0x00,
                 0x02,0x05,0x54,0x4d,0x0a,0x1e,0x0e,0x1a,0x1c,0x14,0x0f,0x0c}; //紅外線鍵碼1--32
void delay(uint k)
{
 while(k--);
}
void show(uint k)     //顯示數(shù)值
{
  uchar i=0,j;
  uchar s[4];        //顯示最終位數(shù)
  do
  {
   s[i++]=k%10;            //數(shù)據(jù)分離
  }while(k/=10);
   for(j=0;j<i;j++)
   {
 P0=seg[s[j]];
 P2=~(1<<(3-j));
 delay(100);
 P0=0xff;
 P2=0xff;
   }
 
}
uchar buf[4];   //存放解碼結果
sbit out=P3^2;   //紅外線發(fā)射點
sbit fan=P1^0;    //電扇控制位
void time0_init()
{
   TMOD|=0x01;   //設置定時器0方式
   TH0=0;
   TL0=0;
   TR0=0;
   ET0=0;
}
uint low_test()    //測低電平時間
{
 uint t;
 TR0=1;   //啟動定時器
 while((out==0)&&(TH0&0x80)==0)   //低電平時間不超時最多9ms 
 {
  if(TH0&0x80)break;  // 超時結束
 }
 TR0=0;  //關閉定時器
 t=TH0;
 t<<=8;   
 t|=TL0;
 TH0=0;   //定時器清零
 TL0=0;
 return t; 
}
uint high_test()  //測高電平時間
{
   uint t;
 TR0=1;   //啟動定時器
 while((out==1)&&(TH0&0x80)==0)   //低電平時間不超時最多9ms 
 {
  if(TH0&0x80)break;  // 超時結束
 }
 TR0=0;  //關閉定時器
 t=TH0;
 t<<=8;   
 t|=TL0;
 TH0=0;
 TL0=0;
 return t; 
}
uchar get_byte()      //紅外線接受先接受低位數(shù)據(jù)后接受高位數(shù)據(jù)

    uint t;
 uchar i,dat;
 for(i=0;i<8;i++)
 {
    t=low_test();
    t=high_test();
    dat>>=1;
    if((t>1400)&&(t<1700))
    {
  dat|=0x80;
    }
    }
 return dat;
}
 
 
void decode()    //紅外線解碼
{
  uint t;
  if(out==0)  //表示接受到紅外線信號
  {
 t=low_test();
  if((t>8000)&&(t<8500))   //低電平時間在9ms左右
  {
     t=high_test();
     if((t>4000)&&(t<4500)) //高電平時間在4.5ms左右
     {
    buf[0]=get_byte();   // 獲取解碼值
    buf[1]=get_byte();
    buf[2]=get_byte();
    buf[3]=get_byte();
    if(buf[2]==~buf[3])    //鍵碼校驗
      ;
again:
          t=low_test();
       t=high_test();
      if((t-2500)<500)   //測停止位時間
      {
   goto again;
      }
     }
  }
    }
}
void int0() //外部中斷0設定
{
   EA=1;
   IT0=1;  //外部中斷下降沿觸發(fā)
   EX0=1;  //外部中斷0開啟
}
void  int0_exe() interrupt 0    //外部中斷函數(shù)
{
   decode();
}
uchar panduan()  //鍵碼按鍵數(shù)判斷
{
   uchar i;
   for(i=0;i<32;i++)
   {
 if(buf[2]==ss[i])break;
   }
   return i+1;
}
 
void main()
{
  uchar start;
  time0_init();
  int0();
  P1=0xff;
  while(1)
  {
    show(panduan());
 start=panduan();
 if(start==1)
 {
  fan=0;    //電扇啟動
 }
 else
 fan=1;    //電扇關閉
  }
}
 
關閉窗口

相關文章