標(biāo)題: 單片機(jī)紅外遙控求助各位大俠。。。ò遄邮荋L-1) [打印本頁]

作者: w1206479884    時間: 2012-4-12 19:00
標(biāo)題: 單片機(jī)紅外遙控求助各位大俠!。。ò遄邮荋L-1)

#include<reg52.h>
#include<stdio.h>
sbit dula=P2^6;
sbit wela=P2^7;
sbit fm=P2^3;

#define uchar unsigned char
#define uint unsigned int

sbit ir=P3^2;//紅外接口標(biāo)準(zhǔn)
uint lowtime,hightime;
uchar a[4];
void cmg()
{
 dula=1;
 P0=0x00;
 dula=0;
 wela=1;
 P0=0x00;
 wela=0;
 //jd=0;
}
void timer0_init()
{
 TMOD=0x01;//定時器0定時方式1
}
void hong_init()
{
 EA=1;//總中斷允許
 EX0=1;//外部中斷0允許
 IT0=1;//外部中斷0觸發(fā)方式選擇位,置1為下降沿有效
 ir=1;
}
bit my()//紅外讀32位的數(shù)據(jù)
{
 uchar i,j,temp;

 for(i=0;i<4;i++)
 {
  for(j=0;j<8;j++)
  {
   temp=temp>>1;//右移用于取數(shù)
   TH0=0;
   TL0=0;//定時器清零
   TR0=1;
   while(ir==0);//等待低電平
   TR0=0;
   lowtime=TH0*256+TL0;//保存低電平時間
   TH0=0;
   TL0=0;
   TR0=1;
   while(ir==1);
   TR0=0;
   hightime=TH0*256+TL0;
   if((lowtime<370)||(lowtime>640))
    return 0;
   if((hightime>370)&&(hightime<640))
    temp=temp&0x7f;
   if((hightime>1300)&&(hightime<1800))
    temp=temp|0x80;
  }
  a=temp;
 }
 if(a[2]=~a[3])
 return 1;
}
//lcd1602

void main()
{
 hong_init();
 timer0_init();
 cmg();
 while(1);

}
void int0() interrupt 0
{
 EX0=0;//關(guān)閉外部中斷
 TH0=0;
 TL0=0;//定時器清零以準(zhǔn)確計(jì)時
 TR0=1;//開啟定時器0
 while(ir==0);//4.5ms的引導(dǎo)碼等待,當(dāng)ir為1是為結(jié)束碼,跳出等待
 TR0=0;// 關(guān)定時器
 lowtime=TH0*256+TL0;//保存低電平時間
 TH0=0;
 TL0=0;//定時器再次清零
 TR0=1;//開定時器
 while(ir==1);//等待結(jié)束碼
 TR0=0;
 hightime=TH0*256+TL0;//保存高電平時間
 if((lowtime>3800)&&(lowtime<4600)&&(hightime>3800)&&(hightime<4600))
 {
   //my();
  if(my()==1)
      fm=0;

 }
 
}

 

用遙控器控制蜂鳴器發(fā)聲的功能無法實(shí)現(xiàn),謝謝各位大俠!






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1