|
void StartModule() //啟動(dòng)測距信號(hào)
{
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;
}
這個(gè)nop_是什么意思??
51單片機(jī)超聲波智能小車程序:
- #include<AT89x51.H>
- #include <intrins.h>
- #define Sevro_moto_pwm P2_0 //接舵機(jī)信號(hào)端輸入PWM信號(hào)調(diào)節(jié)速度
- sbit led1 =P2^4;
- sbit led2 =P2^5;
- sbit led3 =P2^6;
- sbit led4 =P2^7;
- #define ECHO P1_1 //超聲波接口定義
- #define TRIG P1_2 //超聲波接口定義
- bit flag =0;
- #define moto_go {P3_0=1,P3_1=0,P3_2=1,P3_3=0;} //左邊兩個(gè)電機(jī)向前走
- #define moto_back {P3_0=0,P3_1=1,P3_2=0,P3_3=1;} //左邊兩個(gè)電機(jī)向后轉(zhuǎn)
- #define moto_Stop {P3_0=0,P3_1=0,P3_2=0,P3_3=0;} //左邊兩個(gè)電機(jī)停轉(zhuǎn)
- #define moto_R {P3_0=1,P3_1=0,P3_2=0,P3_3=1;} //右邊兩個(gè)電機(jī)向前走
- #define moto_L {P3_0=0,P3_1=1,P3_2=1,P3_3=0;} //右邊兩個(gè)電機(jī)向前走
- unsigned char const discode[] ={0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,0x01,0x09,0x11,0xC1,0x63,0x85,0x61,0x71,0x03,0x9F,0x25,0x0D
- }; //0,1,2,3,4....F,0,1,2,3的段碼
-
- unsigned char LedBuff[4]={ 0xFF, 0xFF, 0x03, 0x03 };
- unsigned char posit=0;
- unsigned char pwm_val_left = 0;//變量定義
- unsigned char push_val_left =14;//舵機(jī)歸中,產(chǎn)生約,1.5MS 信號(hào)
- unsigned long S=0;
- unsigned long S1=0;
- unsigned long S2=0;
- unsigned long S3=0;
- unsigned long S4=0;
- unsigned int time=0; //時(shí)間變量
- unsigned int timer=0; //延時(shí)基準(zhǔn)變量
- unsigned char timer1=0; //掃描時(shí)間變量
- /************************************************************************/
- void delay(unsigned int k) //延時(shí)函數(shù)
- {
- unsigned int x,y;
- for(x=0;x<k;x++)
- for(y=0;y<2000;y++);
- }
- /************************************************************************/
- void Display(void) //掃描數(shù)碼管
- {
- static unsigned char i = 0;
- P0 = 0xFF;
- switch(i)
- {
- case 0: led1 = 0; led4 = 1; i++; P0=LedBuff[0];break;
- case 1: led2 = 0; led1 = 1; i++; P0=LedBuff[1];break;
- case 2: led3 = 0; led2 = 1; i++; P0=LedBuff[2];break;
- case 3: led4 = 0; led3 = 1; i = 0; P0=LedBuff[3];break;
- default:break;
- }
- }
- /************************************************************************/
- void StartModule() //啟動(dòng)測距信號(hào)
- {
- 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) //計(jì)算距離
- {
- while(!ECHO); //當(dāng)RX為零時(shí)等待
- TR0=1; //開啟計(jì)數(shù)
- while(ECHO); //當(dāng)RX為1計(jì)數(shù)并等待
- TR0=0; //關(guān)閉計(jì)數(shù)
- time=TH0*256+TL0; //讀取脈寬長度
- TH0=0;
- TL0=0;
- S=(time*1.7)/100; //算出來是CM
- if((S>=700)||flag==1) //超出測量范圍顯示“F”
- {
- flag=0;
- S=0;
- LedBuff[3] = discode[15];
- LedBuff[2] = discode[15];
- LedBuff[1] = discode[15];
-
- }
- else
- {
- LedBuff[3] = discode[S%10];
- LedBuff[2] = discode[S/10%10];
- LedBuff[1] = discode[S/100%10];
- }
- }
- /************************************************************************/
- //前速前進(jìn)
- void run(void)
- {
- moto_go ; //左電機(jī)往前走
-
- }
- /************************************************************************/
- //前速后退
- void backrun(void)
- {
- moto_back ;
-
- }
- /************************************************************************/
- //左轉(zhuǎn)
- void leftrun(void)
- {
- moto_L ;
- }
- /************************************************************************/
- //右轉(zhuǎn)
- void rightrun(void)
- {
- moto_R ;
-
- }
- /************************************************************************/
- //STOP
- void stoprun(void)
- {
- moto_Stop ;
- }
- /************************************************************************/
- void COMM( void )
- {
-
-
- push_val_left=7; //舵機(jī)向左轉(zhuǎn)90度
- timer=0;
- while(timer<=4000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
- StartModule(); //啟動(dòng)超聲波測距
- Conut(); //計(jì)算距離
- S2=S;
- push_val_left=17; //舵機(jī)向右轉(zhuǎn)90度
- timer=0;
- while(timer<=4000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
- StartModule(); //啟動(dòng)超聲波測距
- Conut(); //計(jì)算距離
- S4=S;
-
- push_val_left=12; //舵機(jī)歸中
- timer=0;
- while(timer<=4000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
- StartModule(); //啟動(dòng)超聲波測距
- Conut(); //計(jì)算距離
- S1=S;
- if((S2>25)&&(S4>25)&&(S1>60)) //只要左右各有距離小于,20CM小車后退
- {
- goto AA;
- }
-
- if((S2<20)||(S4<20)) //只要左右各有距離小于,20CM小車后退
- {
- backrun(); //后退
- timer=0;
- while(timer<=2000);
- stoprun();
- timer=0;
- while(timer<=1000);
- if(S2>S4)
- {
- rightrun(); //車的左邊比車的右邊距離小 右轉(zhuǎn)
- timer=0;
- while(timer<=1000);
- }
- else
- {
- leftrun(); //車的左邊比車的右邊距離大 左轉(zhuǎn)
- timer=0;
- while(timer<=1000);
- }
- }
- else
- {
- if(S2>S4)
- {
- rightrun(); //車的左邊比車的右邊距離小 右轉(zhuǎn)
- timer=0;
- while(timer<=1600);
- }
- else
- {
- leftrun(); //車的左邊比車的右邊距離大 左轉(zhuǎn)
- timer=0;
- while(timer<=1600);
- }
-
- }
- AA: run();
- }
- /************************************************************************/
- /* PWM調(diào)制電機(jī)轉(zhuǎn)速 */
- /************************************************************************/
- /* 左電機(jī)調(diào)速 */
- /*調(diào)節(jié)push_val_left的值改變電機(jī)轉(zhuǎn)速,占空比 */
- void pwm_Servomoto(void)
- {
- pwm_val_left++;
- if(pwm_val_left<=push_val_left)
- Sevro_moto_pwm=1;
- else
- Sevro_moto_pwm=0;
- if(pwm_val_left>=200)
- pwm_val_left=0;
- }
- /***************************************************/
- ///*TIMER1中斷服務(wù)子函數(shù)產(chǎn)生PWM信號(hào)*/
- void time1()interrupt 3 using 2
- {
- TH1=(65536-100)/256; //0.1MS定時(shí)
- TL1=(65536-100)%256;
- timer++; //定時(shí)器0.1MS為準(zhǔn)。在這個(gè)基礎(chǔ)上延時(shí)
- pwm_Servomoto();
- timer1++; //2MS掃一次數(shù)碼管
- if(timer1>=20)
- {
- timer1=0;
- Display();
- }
-
- }
- /***************************************************/
- ///*TIMER0中斷服務(wù)子函數(shù)產(chǎn)生PWM信號(hào)*/
- void timer0()interrupt 1 using 0
- {
- flag=1;
- StartModule();
- }
- /***************************************************/
- void main(void)
- {
- TMOD=0X11;
- TH1=(65536-100)/256; //100US定時(shí)
- TL1=(65536-100)%256;
- TH0=0;
- TL0=0;
- TR1= 1;
- ET1= 1;
- ET0= 1;
- EA = 1;
- delay(100);
- push_val_left=14; //舵機(jī)歸中
- while(1) /*無限循環(huán)*/
- {
- if(timer>=1000) //100MS檢測啟動(dòng)檢測一次
- {
- timer=0;
- StartModule(); //啟動(dòng)檢測
- Conut(); //計(jì)算距離
- if(S<30) //距離小于20CM
- {
- StartModule(); //啟動(dòng)檢測
- Conut(); //計(jì)算距離
- if(S<30) //距離小于20CM
- {
- stoprun(); //小車停止
- COMM(); //方向函數(shù)
- }
- }
- else
- if(S>40) //距離大于,30CM往前走
- run();
- }
- }
- }
-
復(fù)制代碼
|
|