#include<STC_NEW_8051.H>
typedef unsigned char uchar;
typedef unsigned int uint;
sbit in1=P2^4; //L298控制1
sbit in2=P2^5; //L298控制2
sbit in3=P2^6; //L298控制3
sbit in4=P2^7; //L298控制4
sbit k1=P3^6; //按键1
sbit k2=P3^7; //按键2
sbit dsw4=P2^0; //拨码4
sbit dsw3=P2^1; //拨码3
sbit dsw2=P2^2; //拨码2
sbit dsw1=P2^3; //拨码1
sbit TX0=P3^4; //传感器0触发
sbit RX0=P3^2; //传感器0回响
sbit TX1=P3^5; //传感器1触发
sbit RX1=P3^3; //传感器1回响
uint time0=0,time1=0;
uchar buf,temp;
uchar motorl,motorr;
uchar key;
char offset,a;
bit flag,startflag;
/*void serialinit()
{
AUXR=0x11;
BRT=0xfd;
SCON=0x50;
PCON=0x00;
EA=1;
ES=1;
}*/
void pwminit() //PWM初始化
{
CCON=0;
CL=0;
CH=0;
CMOD=0x08; //PWM频率为
CCAPM0=CCAPM1=0x42;//设置为PWM输出模式
CR=1;
}
void timerinit() //定时器初始化
{
TMOD=0x99;
TCON=0x50;
TH0=0;
TL0=0;
TH1=0;
TL1=0;
}
//延时时间:10微秒
void Delay10us(void)
{
unsigned char i,j,k;
for(i=3;i>0;i--)
for(j=14;j>0;j--)
for(k=1;k>0;k--);
}
//延时时间:10000微秒
void Delay(void)
{
unsigned char i,j,k;
for(i=22;i>0;i--)
for(j=82;j>0;j--)
for(k=65;k>0;k--);
}
/*void senddata(uchar dat)
{
SBUF=dat;
while(!TI);
TI=0;
}*/
void read() //读取传感器
{
TR0=TR1=1;
TX0=TX1=1;
Delay10us();
TX0=TX1=0;
Delay();
time0=TH0*256+TL0;
time1=TH1*256+TL1;
TH0=TL0=TH1=TL1=0;
}
void speed(uchar motorl,uchar motorr) //调速控制
{
CCAP0H=CCAP0L=motorl-offset+a;
CCAP1H=CCAP1L=motorr+offset+a;
}
void forward()
{in1=in4=1;in2=in3=0;}
void stopl()
{in3=in4=0;}
void stopr()
{in1=in2=0;}
void readkey()
{
key=P2&0x0f;
key>>=1;
switch(key)
{
case 0x00: a=0x25; break;
case 0x01: a=0x20; break;
case 0x02: a=0x15; break;
case 0x03: a=0x10; break;
case 0x04: a=0x08; break;
case 0x05: a=0x05; break;
case 0x06: a=0x02; break;
case 0x07: a=0x00; break;
}
}
void main()
{
P3M1=0x00;P3M0=0x30; //传感器触发端配置为强推挽输出
TX0=TX1=0; //触发端复位
//serialinit();
timerinit(); //定时器初始化
pwminit(); //PWM初始化
readkey(); //全局速度衰减设置
offset=-0x02; //固有误差偏移修正
speed(0x60,0x60); //速度复位
forward(); //开始前进
while(dsw4)
{
read();
//senddata(0x50);
if(time0>1900 || time1>1900) //超出测距范围
{
if(time0>1900 && time1>1900) //两侧传感器都超出
{speed(0x50,0x50);forward();} //直线前进
else if(time0>1900) //左传感器超出
{stopl();speed(0xd0,0x75);forward();}//左直拐
else if(time1>1900) //右传感器超出
{stopr();speed(0x75,0xd0);forward();}//右直拐
}
else //测量范围内
{
forward(); //前进
if(time0>time1 && time0-time1>50)
{
if(time0-time1>800){stopl();speed(0xd0,0x75);forward();}
else if(time0-time1>600){speed(0x65,0x42);forward();}
else if(time0-time1>500){speed(0x63,0x45);forward();}
else if(time0-time1>400){speed(0x62,0x47);forward();}
else if(time0-time1>300){speed(0x61,0x49);forward();}
else if(time0-time1>200){speed(0x5e,0x4b);forward();}
else if(time0-time1>100){speed(0x5a,0x4e);forward();}
//else if(time0-time1> 50){speed(0x58,0x4f);forward();}
else {speed(0x52,0x50);forward();}
}
else if(time0<time1 && time1-time0>50)
{
if(time1-time0>800){stopr();speed(0x75,0xd0);forward();}
else if(time1-time0>600){speed(0x42,0x65);forward();}
else if(time1-time0>500){speed(0x45,0x63);forward();}
else if(time1-time0>400){speed(0x47,0x62);forward();}
else if(time1-time0>300){speed(0x49,0x61);forward();}
else if(time1-time0>200){speed(0x4b,0x5e);forward();}
else if(time1-time0>100){speed(0x4e,0x5a);forward();}
//else if(time1-time0> 50){speed(0x4f,0x58);forward();}
else {speed(0x50,0x52);forward();}
}
else
{speed(0x50,0x50);forward();}
}
}
}