附录一程序代码红外遥控编码程序#include<>#defineucharunsignedchar#defineuintunsignedint#defineuintlongunsignedlongsbitp3_0=P3^0;sbitp2_1=P2^1;sbitp1_0=P1^0;sbitp1_1=P1^1;sbitp1_2=P1^2;sbitp1_3=P1^3;bitout;uintkeyvalue=0x00,flag_key=0,value1,value2,keycount=0,i,j,flag_set=0,flag_press=0;ucharcodekeycode[4]={0x7f,0xbf,0xdf,0xef};ucharcodeportvalue[12]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x00,0x0b};ucharcodewy[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};//**********红外发送部分**********//ucharuser1=0x00,user2=0x00;uintcount=0,endcount=0;uintirdata=0;voiddeltime(void);voidkey_scan(void);voidsendirdata(void);main(void){EA=1;TMOD=0x11;ET0=1;p3_0=1;P1=0xff;TH0=0xFF;TL0=0xE4;TR0=0;while(1){key_scan();if(flag_press==1) { flag_press=0;TR0=1;sendirdata(); } }}//**********按键扫描***********//voidkey_scan(void){for(i=0;i<=3;i++) { P1=keycode[i];if(p1_3==0) {keycount=i*3+0;flag_key=1;break;}if(p1_2==0) {keycount=i*3+1;flag_key=1;break;}if(p1_1==0) {keycount=i*3+2;flag_key=1;break;} } if(flag_key==1) {flag_key=0; value1=P1; deltime(); value2=P1; if(value1==value2) {keyvalue=portvalue[keycount];flag_set=1;flag_press=1;} while(flag_set) {value2=P1; if(value1!=value2) flag_set=0; } }}//**********延时**********//voiddeltime(void){uintk;for(k=0;k<=20;k++){}}//**********定时中断**********//voidtime0int(void)interrupt1{TH0=0xFF;TL0=0xE4;count++;}//**********发送数据**********//voidsendirdata(){uchars=0,datapd=0;endcount=320;p3_0=0;count=0;do{}while(count<endcount);endcount=180;count=0;p3_0=1;do{}while(count<endcount);for(s=0;s<=11;s++){endcount=20;count=0;p3_0=0;do{}while(count<endcount);endcount=20;count=0;p3_0=1;do{}while(count<endcount); }irdata=keyvalue;for(s=0;s<=7;s++){datapd=irdata&wy[s];if(datapd==0){endcount=20;count=0;}else {endcount=60;count=0;}p3_0=0;do{}while(count<endcount);endcount=20;count=0;p3_0=1;do{}while(count<endcount);}irdata=keyvalue;for(s=0;s<=7;s++){datapd=irdata&wy[s];if(datapd==0){endcount=60;count=0;} else {endcount=20;count=0;}p3_0=0;do{}while(count<endcount);endcount=2
红外遥控小车程序 来自淘豆网m.daumloan.com转载请标明出处.