專(zhuān)注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

8051單片機(jī)紅外線遙控實(shí)驗(yàn)

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


如果網(wǎng)頁(yè)顯示格式錯(cuò)亂可從這里下載完整的源程序:http://www.torrancerestoration.com/f/dpjjmhw.rar
//遙控器采用Ht6221芯片
//紅外線遙控      用外部中斷作為紅外線解碼輸入   紅外遙控器1號(hào)鍵為電風(fēng)扇啟動(dòng)鍵
#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];   //存放解碼結(jié)果
sbit out=P3^2;   //紅外線發(fā)射點(diǎn)
sbit fan=P1^0;    //電扇控制位
void time0_init()
{
   TMOD|=0x01;   //設(shè)置定時(shí)器0方式
   TH0=0;
   TL0=0;
   TR0=0;
   ET0=0;
}
uint low_test()    //測(cè)低電平時(shí)間
{
 uint t;
 TR0=1;   //啟動(dòng)定時(shí)器
 while((out==0)&&(TH0&0x80)==0)   //低電平時(shí)間不超時(shí)最多9ms 
 {
  if(TH0&0x80)break;  // 超時(shí)結(jié)束
 }
 TR0=0;  //關(guān)閉定時(shí)器
 t=TH0;
 t<<=8;   
 t|=TL0;
 TH0=0;   //定時(shí)器清零
 TL0=0;
 return t; 
}
uint high_test()  //測(cè)高電平時(shí)間
{
   uint t;
 TR0=1;   //啟動(dòng)定時(shí)器
 while((out==1)&&(TH0&0x80)==0)   //低電平時(shí)間不超時(shí)最多9ms 
 {
  if(TH0&0x80)break;  // 超時(shí)結(jié)束
 }
 TR0=0;  //關(guān)閉定時(shí)器
 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)  //表示接受到紅外線信號(hào)
  {
 t=low_test();
  if((t>8000)&&(t<8500))   //低電平時(shí)間在9ms左右
  {
     t=high_test();
     if((t>4000)&&(t<4500)) //高電平時(shí)間在4.5ms左右
     {
    buf[0]=get_byte();   // 獲取解碼值
    buf[1]=get_byte();
    buf[2]=get_byte();
    buf[3]=get_byte();
    if(buf[2]==~buf[3])    //鍵碼校驗(yàn)
      ;
again:
          t=low_test();
       t=high_test();
      if((t-2500)<500)   //測(cè)停止位時(shí)間
      {
   goto again;
      }
     }
  }
    }
}
void int0() //外部中斷0設(shè)定
{
   EA=1;
   IT0=1;  //外部中斷下降沿觸發(fā)
   EX0=1;  //外部中斷0開(kāi)啟
}
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;    //電扇啟動(dòng)
 }
 else
 fan=1;    //電扇關(guān)閉
  }
}
 
關(guān)閉窗口

相關(guān)文章