智能小车.docx
《智能小车.docx》由会员分享,可在线阅读,更多相关《智能小车.docx(15页珍藏版)》请在冰豆网上搜索。
智能小车
#include
#defineucharunsignedchar
#defineuintunsignedint
#defineLCMdataP0
#defineBusy0x80
sbitRS=P1^5;
sbitRW=P1^6;
sbitE=P1^7;
sbitIN1=P3^0;
sbitIN2=P3^1;
sbitIN3=P3^3;
sbitIN4=P3^4;
sbitalarm=P3^5;
sbitalarm_led=P3^6;
sbitbzq=P1^1;//前方
sbitbzy=P1^2;//右方
sbitmetal=P1^3;
sbitlight=P1^4;
sbitpark=P2^5;
intmetal_n,model,v,D,count,count1,count2,count11,count22,t_5s,xunguang_model;
uints,s1,s2,s3,t,tcount,z,wcount,s11,s12;
voiddelay(uintx)
{
uinti,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
ucharcodestring_1[]={"时间:
"};
ucharcodestring_2[]={"金属:
"};
voidDelay5Ms(void)//延时5ms
{
unsignedintTempCyc=5552;
while(TempCyc--);
}
voidRDbf(void)//读忙状态
{
while
(1)
{
RS=0;
RW=1;
E=0;
LCMdata=0xFF;
E=1;
if((LCMdata&Busy)==0)
break;
}
}
voidWr_Command(ucharwrcommand,busyc)//写指令
{
if(busyc)
RDbf();
RS=0;
RW=0;
E=1;
LCMdata=wrcommand;
E=0;
}
voidWr_Data(ucharwrdata)//写数据
{
RDbf();
RS=1;
RW=0;
E=1;
LCMdata=wrdata;
E=0;
}
voidDisplay_String(ucharline,uchar*string)//显示字符串
{
uinti;
ucharaddr;
if(line==1)
addr=0x80;
elseif(line==2)
addr=0x90;
elseif(line==3)
addr=0x88;
elseif(line==4)
addr=0x98;
Wr_Command(addr,1);
for(i=0;i<6;i++)
{
Wr_Data(*string++);
}
}
voiddisplay()
{
Display_String(1,string_1);
Wr_Data(0x30+t/10);
Wr_Data(0x30+t%10);
Display_String(2,string_2);
Wr_Data(0x30+metal_n);
Wr_Command(0x88,1);
Wr_Data(0x31);
Wr_Data(0x3a);
Wr_Data(0x30+s1/100);
Wr_Data(0x30+s1/10%10);
Wr_Data(0x30+s1%10);
Wr_Command(0x8c,1);
Wr_Data(0x32);
Wr_Data(0x3a);
Wr_Data(0x30+s2/100);
Wr_Data(0x30+s2/10%10);
Wr_Data(0x30+s2%10);
Wr_Command(0x98,1);
Wr_Data(0x33);
Wr_Data(0x3a);
Wr_Data(0x30+s3/100);
Wr_Data(0x30+s3/10%10);
Wr_Data(0x30+s3%10);
}
voidLcm_Init()
{
Delay5Ms();
Wr_Command(0x30,0);
Delay5Ms();
Wr_Command(0x30,0);//2次显示模式设置,不用判忙
Delay5Ms();
Wr_Command(0x0C,1);//开显示及光标设置
Delay5Ms();
Wr_Command(0x01,1);//显示清屏
Delay5Ms();
Delay5Ms();
Wr_Command(0x06,1);//显示光标移动设置
Delay5Ms();
}
voidforward(uintcount1,uintcount2)
{
count11=count1;
count22=count2;
D=1;
}
voidbackward(uintcount1,uintcount2)
{
count11=count1;
count22=count2;
D=2;
}
voidturn_right(uintcount1)
{
count11=count1;
D=3;
}
voidturn_left(uintcount1)
{
count11=count1;
D=4;
}
voidrotate_right(uintcount1,uintcount2)
{
count11=count1;
count22=count2;
D=5;
}
voidrotate_left(uintcount1,uintcount2)
{
count11=count1;
count22=count2;
D=6;
}
voidSTOP()
{
IN1=0;
IN2=0;
IN3=0;
IN4=0;
EA=0;
}
voidinit()
{
TMOD=0x11;
IT0=1;
EX0=1;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
t=0;
s=0;
}
voidrun()
{
switch(P2)
{
{
case0x83:
rotate_right(85,85);break;//最右
case0x43:
rotate_right(75,75);break;//次右
case0x07:
rotate_left(85,85);break;//最左
case0x0b:
rotate_left(75,75);break;//次左
case0x23:
turn_right(37);break;//中间之左
case0x13:
turn_left(37);break;//中间之左
default:
if(model==1)
forward(70,70);
elseif(model==2)
forward(65,90);
else
forward(70,70);
}
}
}
voidrun1()
{
switch(P2)
{
{
case0x83:
rotate_left(75,75);break;//最右
case0x43:
rotate_left(65,65);break;//次右
case0x07:
rotate_right(75,75);break;//最左
case0x0b:
rotate_right(65,65);break;//次左
case0x23:
turn_left(40);break;//中间之左
case0x13:
turn_right(40);break;//中间之左
default:
forward(30,33);
}
}
}
voidSTOP_5s()
{
t_5s=t;
ET0=0;
IN1=0;
IN2=0;
IN3=0;
IN4=0;
while(t<(t_5s+5))
{
alarm=0;
alarm_led=0;
delay(200);
alarm=1;
alarm_led=1;
delay(200);
}
ET0=1;
}
voidbizhang()
{
while(bzq)
{
forward(89,90);//要保证走的是直线
}
rotate_left(60,60);//保证左拐九十度
delay(200);
while(!
bzy)
forward(87,90);
delay(100);
rotate_right(60,60);
delay(180);//拐小没事,不能拐大
forward(87,90);
delay(400);//还需要调一下
}
voidxunguang()
{
while(light)
{
if(!
bzq)
xunguang_model=1;
if(!
xunguang_model)
rotate_right(30,30);
else
rotate_left(30,30);
}
forward(82,90);
delay(1300);
while(!
park)
run1();
backward(70,70);
delay(100);
STOP();
while
(1);
}
voidmain()
{
Lcm_Init();
display();
init();
model=1;
while(t<=5)//直线区的程序,通过测速与时间测量路程,路程到时进入转弯区
{
if(metal==0)
{
metal_n++;
s11=z*3.85;
while(!
metal)
{
alarm=0;
alarm_led=0;
run();
}
s12=z*3.85;
if(metal_n==1)
s1=(s11+s12)/2-5;
elseif(metal_n==2)
s2=(s11+s12)/2-5;
else
s3=(s11+s12)/2-5;
display();
}
alarm=1;
alarm_led=1;
run();
}
model=2;
while(metal)//弯道区的程序(左拐加循迹保证)
{
run();
}
STOP_5s();
metal_n++;
display();
bizhang();//壁障区
xunguang();//寻光入库区
}
voidtimer0()interrupt1
{
TH0=(65536-100)/256;
TL0=(65536-100)%256;
count++;
if(D==1)//前进
{
IN2=0;
IN4=0;
if(count==count11)
IN1=0;
if(count==count22)
IN3=0;
if(count==100)
{
count=0;
IN1=1;
IN3=1;
}
}
elseif(D==2)//后退
{
IN1=0;
IN3=0;
if(count==count11)
IN2=0;
if(count==count22)
IN4=0;
if(count==100)
{
count=0;
IN2=1;
IN4=1;
}
}
elseif(D==3)//右转
{
IN2=0;
IN3=0;
IN4=0;
if(count==count11)
IN1=0;
if(count==100)
{
count=0;
IN1=1;
}
}
elseif(D==4)//左转
{
IN1=0;
IN2=0;
IN4=0;
if(count==count11)
IN3=0;
if(count==100)
{
count=0;
IN3=1;
}
}
elseif(D==5)//急右转
{
IN2=0;
IN3=0;
if(count==count11)
IN1=0;
if(count==count22)
IN4=0;
if(count==100)
{
count=0;
IN1=1;
IN4=1;
}
}
elseif(D==6)//急左转
{
IN1=0;
IN4=0;
if(count==count11)
IN2=0;
if(count==count22)
IN3=0;
if(count==100)
{
count=0;
IN2=1;
IN3=1;
}
}
else
{
run();
}
}
voidexter0()interrupt3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
tcount++;
if(tcount==20)
{
tcount=0;
t++;
display();
}
}
voidcounter()interrupt0
{
EX0=0;
wcount++;
if(wcount==20)
{
wcount=0;
z++;
}
EX0=1;
}