真心求教啊,帮我看看程序啊,谢谢啊,不知道怎么调到最好的速度,
要不然真线速度太快会没有反应过来就冲出跑道的啊,
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char b1=0 ;
unsigned char b2=0 ;
unsigned char t=0;
sbit out1=P1^0;
sbit out2=P1^1;
sbit out3=P1^2;
sbit out4=P1^3;
sbit IN1=P1^4;
sbit IN2=P1^5;
sbit IN3=P1^6;
sbit IN4=P1^7;
sbit ENA=P0^0;
sbit ENB=P0^1;
void delay(uint z)//延时1ms
{
uchar i;
while(z--)
{for(i=0;i<121;i++);}
}
void init()
{ TMOD=0x01;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
EA=1;
ET0=1;
TR0=1;
}
void timer0() interrupt 1
{ t++;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
if(t<b1)
ENA=1;
else
ENA=0;
if(t<b2)
ENB=1;
else
ENB=0;
if(t>=100)
t=0;
}
void tz()
{ ENA=0;ENB=0;
b1=0;
b2=0;
}
void z0() //左转弯函数快
{ENA=0;ENB=1;
b1=0;
b2=75;
}
void z1() //左转弯函数慢
{ENA=0;ENB=1;
b1=0;
b2=40;
}
void y0() //右转弯函数快
{ENA=1;ENB=0;
b1=75;
b2=0;
}
void y1() //右转弯函数慢
{ENA=1;ENB=0;
b1=40;
b2=0;
}
void qj() //直线函数
{ENA=1;ENB=1;
b1=37;
b2=37;
}
void xj() //循迹
{ uchar flag;
while(1)
{
if((out1==0)&&(out2==0)&&(out3==0)&&(out4==0))
{flag=0;}
else if((out1==1)&&(out2==0)&&(out3==0)&&(out4==0))
{flag=1;}
else if((out1==0)&&(out2==1)&&(out3==0)&&(out4==0))
{flag=2;}
else if((out1==0)&&(out2==0)&&(out3==0)&&(out4==1))
{flag=3;}
else if((out1==0)&&(out2==0)&&(out3==1)&&(out4==0))
{flag=4;}
else if((out1==1)&&(out2==1)&&(out3==1)&&(out4==1))
{flag=5;}
switch(flag)
{
case 0:
qj();
break;
case 1:
delay(6);
z0();
break;
case 2:
delay(6);z1();
break;
case 3:
delay(6);
y0();
break;
case 4:
delay(6);y1();
break;
case 5:
tz();
break;
default:
break;
}
}
}
void main()
{ delay(1000);
init();
b1=37;
b2=37;
ENA=1;ENB=1;
IN1=1;
IN2=0;
IN3=1;
IN4=0;
xj();
}
此帖出自
小平头技术问答
一周热门 更多>