急求基于AT89C51单片机的循迹避障小车电路原理图和主程序(避障模块是超声波测距的),感激不尽啊。 20
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit P1_O=P1^0;
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
sbit P0_2=P0^1;
sbit P0_3=P0^2;
void delaym1(uint z)
{
uint i;
for(i=0;i<z;i++);
}
void main(void)
{
while(1);
{
TRIG=1;
delay(1);
TRIG=0;
while(ECHO==0);
while(ECHO==1) a++; //a每次加1,所时间约21us
delay(30);
a=((340*a*21)/1000)/2;
display();
scan();
z=a;
a=0;
delay(200);}
void zhiliudianji1()
{
uint i,j;
P0=0X00;
//走直线
for(i=0;i<200;i++)
{ for(i=0;i<500;i++)
{
P0_O=1;
P0_1=0;
delaym1(280);
P0_O=0;
P0_1=0;
P0_2=1;
P0_3=0;
delaym1(420);
P0_2=0;
P0_3=0;
}
P0=0X00;
delaym1(30000);
delaym1(30000);}
//右转弯
void zhiliudianji2()
{for(j=0;j<300;j++)
{ P0_O=1;
P0_1=0;
delaym1(300);
P0_O=0;
P0_1=0;
P0_2=1;
P0_3=0;
delaym1(160);
P0_2=0;
P0_3=0;
}
P0=0X00;
delaym1(30000);
delaym1(30000);
//左转弯
void zhiliudianji3()
{for(i=0;i<600;i++)
{
P0_O=1;
P0_1=0;
delaym1(155);
P0_O=0;
P0_1=0;
P0_2=1;
P0_3=0;
delaym1(650);
P0_2=0;
P0_3=0;
}
P0=0X00;
delaym1(30000);
delaym1(30000);}
P0=0x00;
while(1);
}
}
void main(void)
{
while(1)
{
TRIG=1;
delay(1);
TRIG=0;
while(ECHO==0);
while(ECHO==1) a++; //a每次加1,所时间约21us
delay(30);
a=((340*a*21)/1000)/2;
if(a==50)
esle
{ void zhiliudianji3();}
delay(200);
void zhiliudianji1();
z=a;
a=0;
delay(200);
}
2024-09-04 广告