/*从小车后面观察,从左向右的红外检测分别为DD1,DD2,DD3,DD4
DD1---P0^3
DD2---P0^2
DD3---P0^1
DD4---P0^0
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
extern void Motor_Left(bit ReverOrCoro, uchar DutyCycle); //左边电机控制函数
extern void Motor_Right(bit ReverOrCoro, uchar DutyCycle); //右边电机控制函数
void Timer0Config(); // 配置定时器0
void zhizou();
void dazhuan1();
void dazhuan2();
void weitiao1();
void weitiao2();
sbit ENA = P1^5; //定义电机使能端
sbit ENB = P1^0;
uchar a;
uchar Infrared; //检测P0低四位的值
void main()
{
Timer0Config();
ENA = 1; ENB = 1;
Infrared = P0 & 0x0F; //取出P0口低四位
while(1)
{
switch(Infrared)
{
case 0x00:
zhizou(); break; //未检测到黑线,直行
case 0x08:
dazhuan1(); break; //左边第一个检测到黑线,左大转
case 0x04:
weitiao1(); break; //左边第二个检测到黑线,左微调
case 0x02:
weitiao2(); break; //右边第一个检测到黑线,右微调
case 0x01:
dazhuan2(); break; //右边第二个检测到黑线,右大转
case 0x0C:
zhizou(); break; //左边两个同时检测到黑线,直行
case 0x0E:
zhizou(); break; //左边三个同时检测到黑线,直行
case 0x03:
zhizou(); break; //右边两个同时检测到黑线,直行
case 0x07:
zhizou(); break; //右边三个同时检测到黑线,直行
case 0x0f:
zhizou(); break; //全部检测到,直行
case 0x06:
zhizou(); break; //中间两个检测到,直行
case 0x09:
zhizou(); break; //旁边两个检测到,直行
case 0x0A:
weitiao1(); break; //左一右一检测到,左微调
case 0x05:
weitiao2(); break; //左二右二检测到,右微调
case 0x0b:
zhizou(); break; //左二未检测到,直行
case 0x0d:
zhizou(); break; //右一未检测到,直行
default: break;
}
}
}
void Timer0Config()
{
TMOD &= 0xF0;
TMOD |= 0x01;
TH0 = 0xFF;
TL0 = 0x7E;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void zhizou()
{
Motor_Left(1, 100), Motor_Right(1, 100);
Infrared = P0 & 0x0F;
}
void dazhuan1()
{
a=1;
while(a)
{
Motor_Left(0, 80); Motor_Right(1, 80);
Infrared = P0 & 0x0F;
if(Infrared==0x04)
{
Motor_Left(1, 100); Motor_Right(0, 100); break;
}
else if(Infrared==0x02)
{
Motor_Left(1, 100); Motor_Right(0, 100); break;
}
}
Infrared = P0 & 0x0F;
}
void dazhuan2()
{
a=1;
while(a)
{
Motor_Left(1, 80); Motor_Right(0, 80);
Infrared = P0 & 0x0F;
if(Infrared==0x02)
{
Motor_Left(0, 100); Motor_Right(1, 100); break;
}
else if(Infrared==0x04)
{
Motor_Left(0, 100); Motor_Right(1, 100); break;
}
}
Infrared = P0 & 0x0F;
}
void weitiao1()
{
a=1;
while(a)
{
Motor_Left(0, 60); Motor_Right(1, 100);
Infrared = P0 & 0x0F;
switch(Infrared)
{
case 0x04:
a=1; break;
default:
a=0; break;
}
}
Infrared = P0 & 0x0F;
}
void weitiao2()
{
a=1;
while(a)
{
Motor_Left(1, 100); Motor_Right(0, 60);
Infrared = P0 & 0x0F;
switch(Infrared)
{
case 0x02:
a=1; break;
default:
a=0; break;
}
}
Infrared = P0 & 0x0F;
}