#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;
} }
用遥控器控制蜂鸣器发声的功能无法实现,谢谢各位大侠!! |