当前位置:文档之家› 51单片机根据黑线循迹小车程序

51单片机根据黑线循迹小车程序

#include
sbit P0_0=P0^0;//舵机
sbit P3_0=P3^0;//P3_0到P3_4传感器
sbit P3_1=P3^1;
sbit P3_2=P3^2;
sbit P3_3=P3^3;
sbit P3_4=P3^4;
sbit P1_0=P1^0;//p1_0到p1_3电机
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
unsigned int count=0, duo=0,diu;//diu??????
void delay200ms() //@11.0592MHz,
{
unsigned char i, j, k;

i = 2;
j = 103;
k = 147;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void delay25ms() //@11.0592MHz
{
unsigned char i, j;

i = 81;
j = 172;
do
{
while (--j);
} while (--i);
}

void timer()
{
TMOD=0x11;
TH1=(65536-9216)/256; //11.0592Mhz??10ms
TL1=(65536-9216)%256;
TH0=(65536-92)/256;
TL0=(65536-92)%256; //0.1ms
EA=1;
ET0=1;
ET1=1;
TR1=1;
TR0=1;
}
void wheel(int m)
{
if(count<=m){P1_0=1;P1_1=0;P1_2=1;P1_3=0;}
else {P1_0=1;P1_1=1;P1_2=1;P1_3=1;}
}
void duoji(char jd)
{
if(duo<=jd) P0_0=1;
else P0_0=0;

}
void main()
{
unsigned char delay_c,flag=0; //flag 停车标志,为2是停车
for(delay_c=0;delay_c<=15;delay_c++) //??3s??
{delay200ms(); }
timer();
while(1)
{
if(P3_0==0 && P3_1==0 && P3_2==0 && P3_3==0 && P3_4==0) //?????(??)
{
duoji(12);
delay25ms(); //?????,????????
if(P3_0==0 && P3_1==0 && P3_2==0 && P3_3==0 && P3_4==0)
flag++;
if(flag>=2)
while(1)
{ duoji(12);
P1_0=1;P1_1=1;P1_2=1;P1_3=1; //??
}

}
else if(P3_2==0 || (P3_2==0 &&P3_1==0 &&P3_3==0) || (P3_1==0 && P3_3==0 )||(P3_0==0 && P3_1==0)||(P3_3==0 && P3_4==0)||(P3_0==0&&P3_2==0)||(P3_0==0 && P3_3==0)||(P3_0==0 && P3_4==0)||(P3_0==0 && P3_1==0 && P3_3==0)||(P3_1==0 && P3_3==0 && P3_4==0)) //???
{
duoji(12);
wheel(4);//4

}

else if(P3_0==0 && P3_1==1&& P3_2==1 && P3_3==1 && P3_4==1)
{
duoji(11);diu=1;wheel(3);
}
else if(P3_0==1 && P3_1==0 && P3_2==1&& P3_3==1 && P3_4==1)
{
duoji(12);diu=3;wheel(4);
}
else if(P3_1==0&& P3_2==0 && P3_0==1 && P3_3==1 && P3_4==1)
{
duoji(12);wheel(4);
}

else if(P3_4==0 && P3_0==1 && P3_1==1 && P3_2==1 && P3_3==1)
{
duoji(14);diu=2;wheel(2);
}
else if( P3_3==0 &&P3_0==1 && P3_1==1 && P3_2==1 &&P3_4==1)
{
duoji(13);diu=4;wheel(4);
}
else if(P3_2==0 && P3_3==0 && P3_0==1 && P3_1==1 && P3_4==1)
{
duoji(12);wheel(4);
}
else if(diu==1 && P3_0==1 && P3_1==1 && P3_2==1 && P3_3==1 && P3_4==1 )//?????
{
duoji(10);wheel(2);
}
else if(diu==2 && P3_0==1 && P3_1==1 && P3_2==1 && P3_3==1 && P3_4==1 )//???
{
duoji(14);wheel(2);
}
else if( diu==3 && P3_0==1 && P3_1==1 && P3_2==1 && P3_3==1 && P3_4==1 )
{
duoji(12);wheel(4);
}
else if(diu==4 && P3_0==1 && P3_1==1 && P3_2==1 && P3_3==1 && P3_4==1 )
{
duoji(12);wheel(4);
}



}
}
void timer0(v

oid) interrupt 1 // 1 ding shi qi 0??????
{
TH0=(65536-92)/256; //0.1ms
TL0=(65536-92)%256;
duo++;if(duo==200)duo=0;
}
void timer1(void) interrupt 3 // 3 ding shi qi 1??????
{
TH1=(65536-9216)/256; //10ms
TL1=(65536-9216)%256;
count++;if(count==5)count=0;
}

相关主题
文本预览
相关文档 最新文档