main.c
#include<reg52.h>
#include<funcation.h> //多功能头文件
#include<bizhang.h> //避障头文件
#define uint unsigned int
#define uchar unsigned char
uchar flag,tt=0,a;
void delay2();
int i=10;
void main()
{
init();
while(1)
{
if(flag==1)
{
ES=0;
flag=0;
switch(a)
{
case0x2f: //后退
back();
break;
case0x1f: //·前进
go();
break;
case0x3f: //左转
left();
break;
case0x4f: //右转
right();
break;
case0xa0: //避障模式
bizhang();
break;
case0xb0: //旋转模式
spin();
break;
case0xc0: //走8字模式
bazizou();
case0xd0: //跟随模式
gensui();
}
ES=1;
}
}
}
void ser() interrupt 4
{
RI=0;
a=SBUF;
flag=1;
}
funcation.h
#include<reg52.h>
sbit A0 = P1^0;
sbit A1 = P1^1;
sbit A2 = P1^2;
sbit A3 = P1^3;
#define uint unsigned int
#define uchar unsigned char
uchar spinstop;
uchar bazizoustop;
//uchar fangi;
//uchar qii;
void delay3()
{
uinti=20000;
while(--i);
}
void init() //初始化
{
TMOD=0x21;
TH1=0xfd;
TL1=0xfd;
TR1=1;
REN=1;
SM0=0;
SM1=1;
EA=1;
ES=1;
EX0=1;
}
void delay() //延时函数
{
uinti=2000;
while(--i);
}
void go()
{
A0 = 1;
A1 = 0;
A2 = 1;
A3 = 0;
delay();
P1=0;
}
void back()
{
A0 = 0;
A1 = 1;
A2 = 0;
A3 = 1;
delay();
P1=0;
}
void left()
{
A0 = 0;
A1 = 1;
A2 = 1;
A3 = 0;
delay();
P1=0;
}
void right()
{
A0 = 1;
A1 = 0;
A2 = 0;
A3 = 1;
delay();
P1=0;
}
void spin()
{
while(1){
right();
RI=0;
spinstop=SBUF;
if(spinstop==0xa0)
{
RI=0;
break;
}
}
}
void go3()
{
A0 = 1;
A1 = 0;
A2 = 1;
A3 = 0;
delay3();
P1=0;
}
void left3()
{
A0 = 0;
A1 = 1;
A2 = 1;
A3 = 0;
delay3();
P1=0;
}
void right3()
{
A0 = 1;
A1 = 0;
A2 = 0;
A3 = 1;
delay3();
P1=0;
}
void stop3()
{
P1=0;
delay3();
}
void bazizou()
{
while(1){
go3();go3();stop3();
left3();left3();left3();stop3();
go3();go3();go3();go3();stop3();
right3();right3();right3();stop3();
go3();go3();stop3();
right3();right3();stop3();
go3();go3();stop3();
right3();right3();right3();stop3();
go3();go3();go3();go3();stop3();
left3();left3();left3();stop3();
go3();go3();stop3();
left3();left3();left3();stop3();
RI=0;
bazizoustop=SBUF;
if(bazizoustop==0xd0)
{
RI=0;
break;
}
}
}
bizhang.h
#include<reg52.h>
#include <intrins.h>
sbit trig=P2^4;
sbit Echo=P2^5;
unsigned int duration,distance; //测距时间,距离
uchar bizhangstop; //避障模式停止标志
uchar gensuistop; //跟随模式停止标志
void delay2()
{
uinti=20000;
while(--i);
}
void trigger() //超声波启动脉冲
{
trig= 0;
trig= 1;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
trig=0;
}
void calc() //计算距离
{
duration=TH0*256+TL0;
duration*=12/11.0592;
distance=duration*0.017;
TH0=0;
TL0=0;
}
void loop() //启动脉冲后等待接受时间与计算距离
{
trigger();
while(!Echo);
TR0=1;
while(Echo);
TR0=0;
calc();
}
void setup()
{
TH0=0;
TL0=0;
}
void go2()
{
A0 = 1;
A1 = 0;
A2 = 1;
A3 = 0;
delay();
P1=0;
}
void back2()
{
A0 = 0;
A1 = 1;
A2 = 0;
A3 = 1;
delay2();
delay2();
delay2();
delay2();
P1=0;
}
void left2()
{
A0 = 0;
A1 = 1;
A2 = 1;
A3 = 0;
delay2();
P1=0;
}
/*void right2()
{
A0 = 1;
A1 = 0;
A2 = 0;
A3 = 1;
delay2();
P1=0;
}*/
void stop2()
{
P1=0;
delay2();
}
void bizhang()
{
setup();
while(1)
{
RI=0;
bizhangstop=SBUF;
if(bizhangstop==0xb0) //停止标志
{
RI=0;
break;
}
loop();
if(distance>=25) //当障碍物距离大于25cm,前进
go();
if(distance<25) //当障碍物距离小于25cm,迂回
{
stop2();back2();delay2();left2();
delay2();go2();delay2();
}
}
}
void gensui()
{
setup();
while(1)
{
RI=0;
gensuistop=SBUF;
if(gensuistop==0xc0)
{
RI=0;
break;
}
loop();
if(distance>=15&&distance<=30)
{go();}else{stop2();}
}
}
|