|
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(); //計算
}
}
}
|
-
-
SmartCar_CPU.pdf
2018-10-26 15:47 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
1.99 MB, 下載次數: 5, 下載積分: 黑幣 -5
智能小車原理圖
|