《尋跡小車程序》由會(huì)員分享,可在線閱讀,更多相關(guān)《尋跡小車程序(14頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、精選優(yōu)質(zhì)文檔-----傾情為你奉上
尋跡小車程序
下面是我最開始做尋跡小車的時(shí)候?qū)懙膶ほE小車代碼,代碼非常簡(jiǎn)單里面的注釋比較少,如果看不懂的話可以留言。
#include
#define uchar unsigned char
#define uint unsigned int
uchar a1,a2;
uint PWM1,PWM2;
//電機(jī)驅(qū)動(dòng)
sbit ENA=P2^5;
sbit ENB=P2^4;
sbit z1=P2^1;
sbit z2=P2^0;
sbit y1=P2^3;
sbit y2=P2^2;
//尋跡
sbit x1=
2、P1^0;
sbit x2=P1^1;
sbit x3=P1^2;
sbit x4=P1^3;
/*
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
*/
void zhixing() //直行
{
z1=1;
z2=0;
PWM2=20; //控制左電機(jī)
y1=1;
y2=0;
PWM1=18; //控制右電機(jī)
}
void zuotiao_s() //向左小調(diào)
{
z1=1;
z2=
3、0;
PWM2=22; //25
y1=1;
y2=0;
PWM1=20;
}
void youtiao_s() //向右小調(diào)
{
z1=1;
z2=0;
PWM2=24; //28
y1=1;
y2=0;
PWM1=18;//20
}
void zuotiao_b() //向左大調(diào)
{
z1=0;
z2=1;
// PWM2=0;
PWM2=18;
y1=1;
y2=0;
// PWM1=70;
PWM1=35;
}
void youtiao_b() //向右大調(diào)
{
z1=1;
4、 z2=0;
// PWM2=70;
PWM2=35;
y1=0;
y2=1;
// PWM1=0;
PWM1=15;
}
void zuozhuan() //向左轉(zhuǎn)
{
z1=0;
z2=1;
PWM2=50;
PWM2=25;
y1=1;
y2=0;
PWM1=50;
PWM1=30;
}
void youzhuan() //向右轉(zhuǎn)
{
z1=1;
z2=0;
PWM2=50;
PWM2=25;
y1=0;
y2=1;
PWM1=50;
PWM1=25;
}
/*
void
5、stop() //停止
{
z1=0;
z2=0;
y1=0;
y2=0;
}
*/
void time0init()
{
TH0=(65536-100)/256;
TL0=(65536-100)%256;
EA=1;
TR0=1;
ET0=1;
}
void time0 () interrupt 1
{
TH0=(65536-100)/256;
TL0=(65536-100)%256;
a1++;
a2++;
if(a1>100)
a1=0;
if(a2>100)
a2=0;
if(a
6、1PWM1)
ENA=0;
if(a2>PWM2)
ENB=0;
}
void main()
{
time0init();
while(1)
{
if(x1==0&&x2==1&&x3==1&&x4==0)
{
zhixing();
}
else if(x1==0&&x2==0&&x3==1&&x4==0)
{
youtiao_s();
}
else if(x1==0&&x2==1&
7、&x3==0&&x4==0)
{
zuotiao_s();
}
else if(x1==0&&x2==0&&x3==0&&x4==1)
{
youtiao_b();
// delay(6);
}
else if(x1==0&&x2==0&&x3==1&&x4==1)
{
youtiao_b();
// delay(6);
}
else if(x1==1&&x2==0&&x3==0&&x4==0)
{
zuotiao_b();
8、 // delay(6);
}
else if(x1==1&&x2==1&&x3==0&&x4==0)
{
zuotiao_b();
}
else if(x1==1&&x2==0&&x3==1&&x4==0)
{
while(!x4)
{
zuozhuan();
}
}
else if(x1==0&&x2==1&&x3==1&&x4==1)
{
while(!x1)
{
youzhuan();
}
9、
}
else if(x1==0&&x2==1&&x3==0&&x4==1)
{
while(!x1)
{
youzhuan();
}
}
else if(x1==1&&x2==1&&x3==1&&x4==0)
{
while(!x4)
{
zuozhuan();
}
}
else if(x1==1&&x2==1&&x3==1&&x4==1)
{
zhixing();
}
else if(x1==1&&x2==0&&x3==0&&x4==1)
{
zuotiao_s();
}
else
{
zhixing();
}
}
}
專心---專注---專業(yè)