您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 电气安装工程 > 红外遥控小车程序[1]
附录一程序代码红外遥控编码程序#includereg51.h#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(countendcount);endcount=180;count=0;p3_0=1;do{}while(countendcount);for(s=0;s=11;s++){endcount=20;count=0;p3_0=0;do{}while(countendcount);endcount=20;count=0;p3_0=1;do{}while(countendcount);}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(countendcount);endcount=20;count=0;p3_0=1;do{}while(countendcount);}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(countendcount);endcount=20;count=0;p3_0=1;do{}while(countendcount);}TR0=0;}红外遥控解码及驱动程序COUNTEQU30HFLAG_USER1EQU45HFLAG_USER2EQU46HSAVEDATAEQU47HCS1EQUP3.0CS2EQUP3.1RSEQUP3.3RWEQUP3.4EEQUP3.5PAGESEQU56HCOLUMNEQU57HNUMEQU58HORG0000HLJMPMAINORG0003HLJMPEXTER0INTORG000BHLJMPTIMER0INTORG1000HMAIN:MOVP1,#0C5HSETBP3.6SETBP3.7MOVTMOD,#01H;中断初始化MOVTH0,#0FFHMOVTL0,#9CHSETBEASETBIT0SETBET0SETBEX0MOVR0,52H;数据接收初始化MOV52H,#00HMOV53H,#00HMOV54H,#00HMOV55H,#00HMOV51H,#00HMOVCOUNT,#00HMOVR1,#08HMOVR2,#02HCLRPSW.5;数据接收标志CLRPSW.1;数据处理标志MOVFLAG_USER1,#00HLCALLCLEARMOVDPTR,#TAB0;显示初始化MOVPAGES,#1MOVCOLUMN,#0LCALLLCD_DISPMOVDPTR,#TAB2MOVPAGES,#1MOVCOLUMN,#16LCALLLCD_DISPMOVDPTR,#TAB1MOVPAGES,#1MOVCOLUMN,#32LCALLLCD_DISPMOVDPTR,#TAB3MOVPAGES,#1MOVCOLUMN,#48LCALLLCD_DISPMOVDPTR,#TAB4MOVPAGES,#1MOVCOLUMN,#64LCALLLCD_DISPMOVDPTR,#TAB5MOVPAGES,#1MOVCOLUMN,#80;显示红外遥控模式LCALLLCD_DISPMOVDPTR,#TAB6MOVPAGES,#3MOVCOLUMN,#0LCALLLCD_DISPMOVDPTR,#TAB7MOVPAGES,#3MOVCOLUMN,#16LCALLLCD_DISPMOVDPTR,#TAB8MOVPAGES,#3MOVCOLUMN,#32LCALLLCD_DISPMOVDPTR,#TAB10MOVPAGES,#3MOVCOLUMN,#64LCALLLCD_DISPMOVDPTR,#TAB9MOVPAGES,#3MOVCOLUMN,#48LCALLLCD_DISP;显示状态:初始LOOP:;是否进行数据处理JNBPSW.1,$LCALLDATACHULICLRPSW.1MOVA,SAVEDATALCALLDATASHOWENDLOOP:LJMPLOOPEXTER0INT:;外部中断SETBTR0MOV30H,#00HRETITIMER0INT:;定时中断,数据的验证和接收.MOVTH0,#0FFHMOVTL0,#9CHINC30HSETBP3.2MOVC,P3.2JBPSW.5,DATARECEIVEPDJNCENDTIMER0INTMOVA,30HCLRCSUBBA,#90JCENDTIMER0INTSETBPSW.5CLRTR0MOVR1,#08HMOV51H,#00HMOVR0,#52HMOVR2,#04H;共接受两组数据LJMPENDTIMER0INTDATARECEIVEPD:;数据接收判断JNCENDTIMER0INTCLRTR0MOVA,30HCLRTR0SUBBA,#10JCORECEIVESETBCMOVA,51HRRCAMOV51H,ALJMPWENDPDORECEIVE:CLRCMOVA,51HRRCAMOV51H,AWENDPD:DJNZR1,ENDTIMER0INT;重复两次接收,确保存储.MOVR1,#08HMOV@R0,51HMOV@R0,51HINCR0MOV51H,#00HDJNZR2,ENDTIMER0INTCLRPSW.5SETBPSW.1SETBP0.0ENDTIMER0INT:;定时中断结束RETIDATACHULI:;数据处理MOVA,52HCLRCSUBBA,FLAG_USER1JNZENDDATACHULIMOVA,54HANLA,55HJNZENDDATACHULIMOVA,54HMOVSAVEDATA,AMOV52H,#00HMOV53H,#00HMOV54H,#00HMOV55H,#00HENDDATACHULI:;处理完成,返回主程序RETDATASHOW:;判断接收信号,进行相应处理CJNEA,#00H,CON1LJMPAOTO;状态判断CON1:CJNEA,#02H,CON2MOVP1,#0FAHMOVDPTR,#TAB11MOVPAGES,#3LJMPENDDATASHOW;后转并显示CON2:CJNEA,#04H,CON3MOVP1,#0E5HMOVDPTR,#TAB12MOVPAGES,#3LJMPENDDATASHOW;左转并显示CON3:CJNEA,#05H,CON4MOVP1,#0C5HMOVDPTR,#TAB13MOVPAGES,#3LJMPENDDATASHOW;停转并显示CON4:CJNEA,#06H,CON5MOVP1,#0D5HMOVDPTR,#TAB14MOVPAGES,#3LJMPENDDATASHOW;右转并显示CON5:CJNEA,#08H,CON6MOVP1,#0F5HMOVDPTR,#TAB15MOVPAGES,#3LJMPENDDATASHOW;前转并显示CON6:JMPENDDATASHOW1ENDDATASHOW:MOVCOLUMN,#48LCALLLCD_DISPMOVDPTR,#TAB16MOVPAGES,#3MOVCOLUMN,#64LCALLLcD_DISPENDDATASHOW1:RETAOTO:MOVP1,#0C0H;寻迹壁障程序MOVDPTR,#TAB17MOVPAGES,#1MOVCOLUMN,#0LCALLLCD_DISPMOVDPTR,#TAB18MOVPAGES,#1MOVCOLUMN,#16LCALLLCD_DISPMOVDPTR,#TAB19MOVPAGES,#1MOVCOLUMN,#32LCALLLCD_DISPMOVDPTR,#TAB20MOVPAGES,#1MOVCOLUMN,#48LCALLLCD_DISP;显示寻迹避障MOVDPTR,#TAB10MOVPAGES,#3MOVCOLUMN,#64LCALLLCD_DISPMOVDPTR,#TAB9MOVPAGES,#3MOVCOLUMN,#48LCALLLCD_DISP;显示初始LCALLDLY;模式工作前等待3秒AOTO1:;自动寻迹避障程序MOVDPTR,#TAB15MOVPAGES,#3MOVCOLUMN,#48LCALLLCD_DISPMOVDPTR,#TAB16MOVPAGES,#3MOVCOLUMN,#64LCALLLCD_DISP;显示前转MOVP1,#0F5HJNBPSW.5,AOTO2;检测红外信号LCALLCLEARMOVP1,#0C5HLCALLDLY1LJM
本文标题:红外遥控小车程序[1]
链接地址:https://www.777doc.com/doc-4233158 .html