|
100黑币
新人求助:这是一个超声波寻迹小车的程序,但现在除了一些问题,求各位大神能帮帮忙,将其修正。
需要达到的效果,四路小车能按照黑带自动寻迹,到达终点时能切换至超声波测距。
//hc-sr04 超声波测距模块 DEMO 程序
//晶振:11。0592
//接线:模块TRIG接 P3.7 ECH0 接P3.6
//数码管:共阳数码管P0接数据口,P2.2 P2.1 P2.0接选通数码管
/***********************************************************************************************************/
#include <AT89x51.H> //器件配置文件
#include <intrins.h>
# define Left_moto_go {P1_0=1,P1_1=0,P1_2=0,P1_3=1;}
# define Left_moto_back {P1_0=0,P1_1=1,P1_2=1,P1_3=0;}
# define Right_moto_go {P1_4=1,P1_5=0,P1_6=0,P1_7=1;}
# define Right_moto_back {P1_4=0,P1_5=1,P1_6=1,P1_7=0;}
# define Stop {P1_0 = 0,P1_1 = 0,P1_2 = 0,P1_3 = 0,P1_4 = 0,P1_5 = 0,P1_6 = 0,P1_7 = 0;}
# define S1 P2_4
# define S2 P2_5
# define S3 P2_6
# define S4 P2_7
#define RX P3_6
#define TX P3_7
unsigned int time=0;
unsigned int timer=0;
unsigned char posit=0;
unsigned long S=0;
bit flag =0;
unsigned char const discode[] ={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF,0xff/*-*/};
unsigned char const positon[3]={ 0xfe,0xfd,0xfb};
unsigned char disbuff[4] ={ 0,0,0,0,};
int n = 0 ;
void run(void);
void right2trun(void);
void right1trun(void);
void left2run(void);
void left1run(void);
void xunji(void);
/********************************************************/
void Display(void) //扫描数码管
{
if(posit==0)
{P0=(discode[disbuff[posit]])&0x7f;}
else
{P0=discode[disbuff[posit]];}
P2=positon[posit];
if(++posit>=3)
posit=0;
}
/********************************************************/
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(time*1.7)/100; //算出来是CM
if((S>=700)||flag==1) //超出测量范围显示“-”
{
flag=0;
disbuff[0]=0; //“-”
disbuff[1]=0; //“-”
disbuff[2]=0; //“-”
}
else
{
disbuff[0]=S%1000/100;
disbuff[1]=S%1000%100/10;
disbuff[2]=S%1000%10 %10;
}
}
/********************************************************/
void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围
{
flag=1;
//中断溢出标志
}
/********************************************************/
void zd3() interrupt 3 //T1中断用来扫描数码管和计800MS启动模块
{
TH1=0xf8;
TL1=0x30;
Display();
timer++;
if(timer>=400)
{
timer=0;
TX=1; //800MS 启动一次模块
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TX=0;
}
}
void run()
{
static int a = 2;
if(--a >= 1)
Stop;
if(a == 0)
{
Left_moto_go;
Right_moto_go;
a = 2;
}
}
void right2trun()
{
static int b2 = 3;
if(--b2 >= 1)
{
Stop;
}
if(b2 == 0)
{
Left_moto_go;
Right_moto_back;
b2 = 3;
}
}
void right1trun()
{
int r = 1;
while(r)
{
Left_moto_go;
Right_moto_back;
if(S3 == 0)
r = 0 ;
}
}
void left2run()
{
static int c2 =2;
if(--c2 >= 1)
{
Stop;
}
if(c2 == 0)
{
Left_moto_back;
Right_moto_go;
c2 = 2;
}
}
void left1run()
{
int l = 1;
while(l)
{
Left_moto_back;
Right_moto_go;
if(S2 == 0)
l = 0;
}
}
void xunji()
{
n=n+1;
if(n == 1)
{
n = 0 ;
if(S1 == 1 && S2 == 1 && S3 == 0 && S4 == 1)
{
right2trun();
}
if((S1 == 1 && S2 == 1 && S3 == 0 && S4 == 0) || (S1 == 1 && S2 == 1 && S3 == 1 && S4 == 0))
{
right1trun();
}
if(S1 == 1 && S2 == 0 && S3 == 1 && S4 == 1)
{
left2run();
}
if((S1 == 0 && S2 == 0 && S3 == 1 && S4 == 1)||(S1 == 0 && S2 == 1 && S3 == 1 && S4 == 1))
{
left1run();
}
if(S1 == 0&& S2 == 0 && S3 == 0 && S4 == 0)
{
Stop;
}
if(S1 == 1 && S2 == 1&& S3 == 1 && S4 == 1)
{
run();
}
}
else
{
Stop;
}
}
/*********************************************************/
void main( void )
{
TMOD=0x11; //设T0为方式1,GATE=1;
TH0=0;
TL0=0;
TH1=0xf8; //2MS定时
TL1=0x30;
ET0=1; //允许T0中断
ET1=1; //允许T1中断
TR1=1; //开启定时器
EA=1; //开启总中断
run();
while(1)
{
xunji();
if(S1==0&&S2==0&&S3==0&&S4==0)
{
while(!RX); //当RX为零时等待
TR0=1; //开启计数
while(RX); //当RX为1计数并等待
TR0=0; //关闭计数
Conut(); //计算
}
}
}
|
|