#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;//紅外接口標準 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觸發方式選擇位,置1為下降沿有效 ir=1; } bit my()//紅外讀32位的數據 { uchar i,j,temp;
for(i=0;i<4;i++) { for(j=0;j<8;j++) { temp=temp>>1;//右移用于取數 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;//關閉外部中斷 TH0=0; TL0=0;//定時器清零以準確計時 TR0=1;//開啟定時器0 while(ir==0);//4.5ms的引導碼等待,當ir為1是為結束碼,跳出等待 TR0=0;// 關定時器 lowtime=TH0*256+TL0;//保存低電平時間 TH0=0; TL0=0;//定時器再次清零 TR0=1;//開定時器 while(ir==1);//等待結束碼 TR0=0; hightime=TH0*256+TL0;//保存高電平時間 if((lowtime>3800)&&(lowtime<4600)&&(hightime>3800)&&(hightime<4600)) { //my(); if(my()==1) fm=0;
} }
用遙控器控制蜂鳴器發聲的功能無法實現,謝謝各位大俠!! |