|
代碼如上,我是小白~
請問下各位大佬 , 為什么進(jìn)入不了while循環(huán)呢 輪子一直轉(zhuǎn)但是超聲波模塊不能正常運(yùn)轉(zhuǎn) 幫忙改一下代碼并給我一下理由和建議 謝謝啦
- #include <AT89x51.H>
- #include <intrins.h>
- #define ECHO P2_4 //超聲波接口定義
- #define TRIG P2_5 //超聲波接口定義
- #define Left_moto_go {P1_0=1,P1_1=0,P1_2=1,P1_3=0;} //左邊兩個電機(jī)向前走
- #define Left_moto_back {P1_0=0,P1_1=1,P1_2=0,P1_3=1;} //左邊兩個電機(jī)后轉(zhuǎn)
- #define Left_moto_Stop {P1_0=0,P1_1=0,P1_2=0,P1_3=0;} //左邊停止
- #define Right_moto_go {P1_4=1,P1_5=0,P1_6=1,P1_7=0;} //右邊的兩個前進(jìn)
- #define Right_moto_back {P1_4=0,P1_5=1,P1_6=0,P1_7=1;} //右邊的后轉(zhuǎn)
- #define Right_moto_Stop {P1_4=0,P1_5=0,P1_6=0,P1_7=0;} //右邊停止
- typedef unsigned int u16;
- typedef unsigned char u8;
- unsigned long S=0;
- unsigned int time=0; //時間變量
- unsigned int timer=0; //延時基準(zhǔn)變量
- unsigned char timer1=0; //掃描時間變?
- bit flag =0;
- /**********************測距**************************************************/
- void zd0() interrupt 1 //T0中斷
- {
- flag=1; //中斷溢出標(biāo)志
- }
- void delay(unsigned int k) //延時函數(shù)
- {
- unsigned int x,y;
- for(x=0;x<k;x++)
- for(y=0;y<2000;y++);
- }
- /************************************************************************/
- //前進(jìn)
- void run(void)
- {
- Left_moto_go ; //左
- Right_moto_go ; //右
- }
- /************************************************************************/
- //后退
- void backrun(void)
- {
- Left_moto_back ; //左
- Right_moto_back ; //右
- }
- /************************************************************************/
- //左轉(zhuǎn)
- void leftrun(void)
- {
- Left_moto_back ;
- Right_moto_go ;
- }
- /************************************************************************/
- //右轉(zhuǎn)
- void rightrun(void)
- {
- Left_moto_go ;
- Right_moto_back ;
- }
- /************************************************************************/
- //停止
- void stoprun(void)
- {
- Left_moto_Stop ;
- Right_moto_Stop ;
- }
- /************************************************************************/
- void StartModule() //啟動模塊
- {
- TRIG=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- TRIG=0;
- }
- /***************************************************/
- void Conut(void) //啟動測距
- {
- while(!ECHO);
- TR0=1;
- while(ECHO);
- TR0=0;
- time=TH0*256+TL0;
- TH0=0;
- TL0=0;
- S=(time*1.7)/100;
- }
- /************************************************************************/
- void time1()interrupt 3 using 2
- {
- TH1=(65536-100)/256; //100US??
- TL1=(65536-100)%256;
- timer++; //???100US???????????
- }
- void main(void)
- { TMOD=0x21;
- SCON=0x50;
- TH1=0xFD;
- TL1=0xFD;
- TH0=0;
- TL0=0;
- TR0=1;
- ET0=1;
- TR1=1;
- TI=1;
-
- EA=1;
-
- run();
- while(1)
- {
- if(timer>=1000) //100MS檢查一次
- {
- timer=0;
- StartModule(); //啟動檢測
- Conut(); //計算距離
- if(S<50)
- {
- stoprun(); //小車停止
- delay(500);
- backrun();
- delay(500);
- leftrun();
- if(S<30)
- {
- stoprun();
- delay(500);
- rightrun();
- delay(500);
- run();
- }
- else
- run();
- timer=0;
- StartModule(); //啟動測超聲模塊
- Conut();
- //計算
- }
- else
- run();
- }
- }
- }
復(fù)制代碼
|
|