资源描述:
《自己写的51单片机的红外线遥控接收程序(C语言)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、//51单片机做的红外遥控实验(C语言)#include#defineu8unsignedchar#defineu16unsignedint#defineID0x00//本遥控器的ID号sbitir=P3^3;codeu8seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0-9的段码codeu8s[]={1,0x40,0x48,0x04,0x02,0x05,0x54,0x0A,0x1E,0x0E};u8buf[4];bitir_f=0;u8nu;voiddelay(u16x){while
2、(x--);}voidshow(u16x){u8i=0,k=0;u8s[4];kk:s[i]=x%10;if((x/10)>=1){x=x/10;i++;gotokk;}k=i+1;for(i=0;i>i);delay(300);P0=0XFF;P2=0XFF;}}voidtimer0_init(){TH0=0;TL0=0;TMOD
3、=0x01;TR0=0;}u16low_test(){u16t;TR0=1;while((ir==0)&&((TH0&0X80)!=0X80));TR0=0;t=TH0;t<<=8;t
4、=
5、TL0;TH0=0;TL0=0;//t=(TH*256+TL0);//机器周期数returnt;}u16high_test(){u16t;TR0=1;while((ir==1)&&((TH0&0X80)!=0X80));TR0=0;t=TH0;t<<=8;t
6、=TL0;TH0=0;TL0=0;returnt;}/*u16time_test(bitx){}*/u8receive_8bit(){u8d,i;u16t;for(i=0;i<8;i++){t=low_test();t=high_test();d>>=1;if((t>=2750)&&(t<=3100)){d
7、=0x80
8、;}}returnd;}voidir_decode(){u16t;u8i;if(ir==0)//有遥控信号{t=low_test();//8295-9000us,倍频的是16590-18000if((t>=14500)&&(t<=18000))//检查引导码低电平时间{t=high_test();if((t>=8000)&&(t<=9000))//检查高电平{for(i=0;i<4;i++){buf[i]=receive_8bit();}if(buf[0]==(~buf[1]))//检查系统码是否正确{if(buf[0]==ID){if(buf[2]==(~buf[3]))
9、{//具体按键处理ir_f=1;//遥控有效}}}}}}}/*voidkey(){if(buf[2]==0x40){P1^=(1<<0);}if(buf[2]==0x48){P1^=(1<<1);}}*/voidir_execuse(){if(ir_f==1){switch(buf[2]){case0x40:P1^=(1<<0);break;case0x48:P1^=(1<<1);break;case0x04:P1^=(1<<2);break;case0x02:P1^=(1<<3);break;case0x05:P1^=(1<<4);break;case0x54:P1^=(
10、1<<5);break;case0x0A:P1^=(1<<6);break;case0x1E:P1^=(1<<7);break;}ir_f=0;}}voidshow_d(){u8j;for(j=0;j<10;j++){if(s[j]==buf[2]){nu=j;break;}}show(nu);}voidisr_init(){EA=1;EX1=1;//外部中断,一直看3.3有没有下降沿。或者3.3一直是低电平IT1=1;//外部中断1下降沿触发}voidex1_isr()interrupt2{EA=0;ir_decode();EA=1;}voidmain(){timer0_
11、init();isr_init();while(1){ir_execuse();show_d();//key();}}