#include
unsigned char disbuff[4]={0,0,0,0};
unsigned int time=0;
unsigned long S=0;
char a=0;
bit flag=0;
unsigned char push_val_left1;
unsigned char push_val_right1;
sbit IN1=P2^3;
sbit IN2=P2^4;
sbit IN3=P2^6;
sbit IN4=P2^5;
sbit ENA=P1^0;//左边两个电机使能
sbit ENB=P1^1;//右边两个电机使能
sbit TX=P1^6; //Trig 控制端
sbit RX=P1^7; //Echo 接收端
sbit lse1=P1^5;//左边红外传感器的数据口
sbit lse2=P1^4;//右边红外传感器的数据口
unsigned char mot1=0;//定义左轮
unsigned char mot2=0;//定义右轮
int t = 0;
void run(void) //小车前进
{
mot1=5;
mot2=5;
IN1=1;
IN2=0;
IN3=1;
IN4=0;
}
void stop(void) //小车停止
{
mot1=0;
mot2=0;
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
void leftrun(void) //小车右转
{
mot1=5;
mot2=0;
IN1=1;
IN2=0;
IN3=0;
IN4=0;
}
void rightrun(void) //小车左转
{
mot1=0;
mot2=5;
IN1=0;
IN2=0;
IN3=1;
IN4=0;
}
void Delay(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;
}
}
void startmodule()
{
TX=1;
Delay(10);
TX=0;
}
void count()
{
time=TH1*256+TL1;
TH1=0;
TL1=0;
S=time*0.17+10;
}
void count1()
{
count1() ;
if((S>=5000)||flag==1)
{
a=0;
flag=0;
}
else
{
disbuff[0]=S%10;
disbuff[1]=S/10%10;
disbuff[2]=S/100%10;
disbuff[3]=S/1000;
}
if(S<=240)
{
a++;
if(a>=2)
{
a=0;
stop();
Delay(20);
B:rightrun();
Delay(10);
stop();
Delay(10);
StartModule();
while(RX==0);
TR1=1;
while(TX=0);
TR1=0;
count();
if(S>340)
{
rightrun();
Delay(10);
stop();
Delay(10);
run();
}
else
{
goto B;
}
}
else
{
run();
}
}
else
{
a=0;
run();
}
}
void PIT_Init()
{
EA=1; //打开总中断
TMOD |=0x01;
TL0=0xA4; //设置定时初值
TH0=0xFF; //设置定时初值
ET0=1; //打开定时器中断允许
TR0=1; //打开定时器
}
void timer()interrupt 1//interrrupt
{
TL0 = 0xA4; //设置定时初值
TH0 = 0xFF; //设置定时初值
if(tENA = 1;
}
else
{
ENA = 0;
}
if(tENB = 1;
}
else
{
ENB = 0;
}
t=(t+1)%100;
}
void xunji()
{
unsigned char flag; //用于循迹方向的判断
if((lse1==0)&&(lse2==1))
{
flag=0; //左边检测到黑线
}
else if((lse1==1)&&(lse2==0))
{
flag=1; //右边检测到黑线
}
else if((lse1==0)&&(lse2==0))
{
flag=3; //两边检测到黑线
}
else
{
flag=4;
}
switch(flag)
{
case 0:leftrun(); break;
case 1:rightrun();break;
case 3:stop();break;
case 4:run();break;
}
}void main()
{
PIT_Init();
mot1=13;
mot2=13;
IN1=1;
IN2=0;
IN3=1;
IN4=0;
while(1)
{
if(lse2 == 0) //检测到右边有障碍物时,向左移动
{
leftrun();
}
else if(lse1== 0) //检测到左边边有障碍物时,向右移动
{
rightrun();
}
else if(lse2 == 1 && lse1== 1) //检测到都没有障碍物时,向前移动
{
run();
}
else if(lse2 == 0 && lse1 == 0) //检测到都有障碍物时,向后移动
{
stop();
}
}
xunji();
count1();
}
显示count1和xunji不能调用