您好,欢迎访问三七文档
寻迹小车程序下面是我最开始做寻迹小车的时候写的寻迹小车代码,代码非常简单里面的注释比较少,如果看不懂的话可以留言。#includereg52.h#defineucharunsignedchar#defineuintunsignedintuchara1,a2;uintPWM1,PWM2;//电机驱动sbitENA=P2^5;sbitENB=P2^4;sbitz1=P2^1;sbitz2=P2^0;sbity1=P2^3;sbity2=P2^2;//寻迹sbitx1=P1^0;sbitx2=P1^1;sbitx3=P1^2;sbitx4=P1^3;/*voiddelay(ucharz){ucharx,y;for(x=z;x0;x--)for(y=110;y0;y--);}*/voidzhixing()//直行{z1=1;z2=0;PWM2=20;//控制左电机y1=1;y2=0;PWM1=18;//控制右电机}voidzuotiao_s()//向左小调{z1=1;z2=0;PWM2=22;//25y1=1;y2=0;PWM1=20;}voidyoutiao_s()//向右小调{z1=1;z2=0;PWM2=24;//28y1=1;y2=0;PWM1=18;//20}voidzuotiao_b()//向左大调{z1=0;z2=1;//PWM2=0;PWM2=18;y1=1;y2=0;//PWM1=70;PWM1=35;}voidyoutiao_b()//向右大调{z1=1;z2=0;//PWM2=70;PWM2=35;y1=0;y2=1;//PWM1=0;PWM1=15;}voidzuozhuan()//向左转{z1=0;z2=1;PWM2=50;PWM2=25;y1=1;y2=0;PWM1=50;PWM1=30;}voidyouzhuan()//向右转{z1=1;z2=0;PWM2=50;PWM2=25;y1=0;y2=1;PWM1=50;PWM1=25;}/*voidstop()//停止{z1=0;z2=0;y1=0;y2=0;}*/voidtime0init(){TH0=(65536-100)/256;TL0=(65536-100)%256;EA=1;TR0=1;ET0=1;}voidtime0()interrupt1{TH0=(65536-100)/256;TL0=(65536-100)%256;a1++;a2++;if(a1100)a1=0;if(a2100)a2=0;if(a1PWM1)ENA=1;if(a2PWM2)ENB=1;if(a1PWM1)ENA=0;if(a2PWM2)ENB=0;}voidmain(){time0init();while(1){if(x1==0&&x2==1&&x3==1&&x4==0){zhixing();}elseif(x1==0&&x2==0&&x3==1&&x4==0){youtiao_s();}elseif(x1==0&&x2==1&&x3==0&&x4==0){zuotiao_s();}elseif(x1==0&&x2==0&&x3==0&&x4==1){youtiao_b();//delay(6);}elseif(x1==0&&x2==0&&x3==1&&x4==1){youtiao_b();//delay(6);}elseif(x1==1&&x2==0&&x3==0&&x4==0){zuotiao_b();//delay(6);}elseif(x1==1&&x2==1&&x3==0&&x4==0){zuotiao_b();}elseif(x1==1&&x2==0&&x3==1&&x4==0){while(!x4){zuozhuan();}}elseif(x1==0&&x2==1&&x3==1&&x4==1){while(!x1){youzhuan();}}elseif(x1==0&&x2==1&&x3==0&&x4==1){while(!x1){youzhuan();}}elseif(x1==1&&x2==1&&x3==1&&x4==0){while(!x4){zuozhuan();}}elseif(x1==1&&x2==1&&x3==1&&x4==1){zhixing();}elseif(x1==1&&x2==0&&x3==0&&x4==1){zuotiao_s();}else{zhixing();}}}
本文标题:寻迹小车程序
链接地址:https://www.777doc.com/doc-4683790 .html