|
超聲波智能小車c程序
單片機(jī)源程序如下:
- #include<AT89x51.H>
- #include <intrins.h>
- #define Sevro_moto_pwm P2_7 //接舵機(jī)信號端輸入PWM信號調(diào)節(jié)速度
- #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;} //左邊兩個電機(jī)停轉(zhuǎn)
- #define Right_moto_go {P1_4=1,P1_5=0,P1_6=1,P1_7=0;} //右邊兩個電機(jī)向前走
- #define Right_moto_back {P1_4=0,P1_5=1,P1_6=0,P1_7=1;} //右邊兩個電機(jī)向前走
- #define Right_moto_Stop {P1_4=0,P1_5=0,P1_6=0,P1_7=0;} //右邊兩個電機(jī)停轉(zhuǎn)
- 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,};
- unsigned char posit=0;
- unsigned char pwm_val_left = 0;//變量定義
- unsigned char push_val_left =14;//舵機(jī)歸中,產(chǎn)生約,1.5MS 信號
- unsigned long S=0;
- unsigned long S1=0;
- unsigned long S2=0;
- unsigned long S3=0;
- unsigned long S4=0;
- unsigned int time=0; //時間變量
- unsigned int timer=0; //延時基準(zhǔn)變量
- unsigned char timer1=0; //掃描時間變量
- /************************************************************************/
- void delay(unsigned int k) //延時函數(shù)
- {
- unsigned int x,y;
- for(x=0;x<k;x++)
- for(y=0;y<2000;y++);
- }
- /************************************************************************/
- void Display(void) //掃描數(shù)碼管
- {
- if(posit==0)
- {P0=(discode[disbuff[posit]])&0x7f;}//產(chǎn)生點(diǎn)
- else
- {P0=discode[disbuff[posit]];} if(posit==0)
- { P2_1=0;P2_2=1;P2_3=1;}
- if(posit==1)
- {P2_1=1;P2_2=0;P2_3=1;}
- if(posit==2)
- {P2_1=1;P2_2=1;P2_3=0;}
- if(++posit>=3)
- posit=0;
- }
- /************************************************************************/
- 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); //當(dāng)RX為零時等待
- TR0=1; //開啟計數(shù)
- while(ECHO); //當(dāng)RX為1計數(shù)并等待
- TR0=0; //關(guān)閉計數(shù)
- time=TH0*256+TL0; //讀取脈寬長度
- TH0=0;
- TL0=0;
- S=(time*1.7)/100; //算出來是CM
- disbuff[0]=S%1000/100; //更新顯示
- disbuff[1]=S%1000%100/10;
- disbuff[2]=S%1000%10 %10;
- }
- /************************************************************************/
- //前速前進(jìn)
- void run(void)
- {
- Left_moto_go ; //左電機(jī)往前走
- Right_moto_go ; //右電機(jī)往前走
- }
- /************************************************************************/
- //前速后退
- void backrun(void)
- {
- Left_moto_back ; //左電機(jī)往前走
- Right_moto_back ; //右電機(jī)往前走
- }
- /************************************************************************/
- //左轉(zhuǎn)
- void leftrun(void)
- {
- Left_moto_back ; //左電機(jī)往前走
- Right_moto_go ; //右電機(jī)往前走
- }
- /************************************************************************/
- //右轉(zhuǎn)
- void rightrun(void)
- {
- Left_moto_go ; //左電機(jī)往前走
- Right_moto_back ; //右電機(jī)往前走
- }
- /************************************************************************/
- //STOP
- void stoprun(void)
- {
- Left_moto_Stop ; //左電機(jī)停走
- Right_moto_Stop ; //右電機(jī)停走
- }
- /************************************************************************/
- void COMM( void )
- {
- push_val_left=5; //舵機(jī)向左轉(zhuǎn)90度
- timer=0;
- while(timer<=4000); //延時400MS讓舵機(jī)轉(zhuǎn)到其位置
- StartModule(); //啟動超聲波測距
- Conut(); //計算距離
- S2=S;
- push_val_left=23; //舵機(jī)向右轉(zhuǎn)90度
- timer=0;
- while(timer<=4000); //延時400MS讓舵機(jī)轉(zhuǎn)到其位置
- StartModule(); //啟動超聲波測距
- Conut(); //計算距離
- S4=S;
- push_val_left=14; //舵機(jī)歸中
- timer=0;
- while(timer<=4000); //延時400MS讓舵機(jī)轉(zhuǎn)到其位置 StartModule(); //啟動超聲波測距
- Conut(); //計算距離
- S1=S; if((S2<20)||(S4<20)) //只要左右各有距離小于20CM小車后退
- {
- backrun(); //后退
- timer=0;
- while(timer<=4000);
- }
- if(S2>S4)
- {
- rightrun(); //車的左邊比車的右邊距離小 右轉(zhuǎn)
- timer=0;
- while(timer<=4000);
- }
- else
- {
- leftrun(); //車的左邊比車的右邊距離大 左轉(zhuǎn)
- timer=0;
- while(timer<=4000);
- }
- } /************************************************************************/
- /* PWM調(diào)制電機(jī)轉(zhuǎn)速 */
- /************************************************************************/
- /* 左電機(jī)調(diào)速 */
- /*調(diào)節(jié)push_val_left的值改變電機(jī)轉(zhuǎn)速,占空比 */
- void pwm_Servomoto(void)
- {
- 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信號*/
- void time1()interrupt 3 using 2
- {
- TH1=(65536-100)/256; //100US定時
- TL1=(65536-100)%256;
- timer++; //定時器100US為準(zhǔn)。在這個基礎(chǔ)上延時
- pwm_val_left++;
- pwm_Servomoto(); timer1++; //2MS掃一次數(shù)碼管
- if(timer1>=20)
- {
- timer1=0;
- Display();
- }
- }
- /***************************************************/
- ///*TIMER0中斷服務(wù)子函數(shù)產(chǎn)生PWM信號*/
- void timer0()interrupt 1 using 0
- {
- } /***************************************************/
- void main(void)
- { TMOD=0X11;
- TH1=(65536-100)/256; //100US定時
- TL1=(65536-100)%256;
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
超聲波.zip
(1.86 KB, 下載次數(shù): 49)
2017-6-7 09:01 上傳
點(diǎn)擊文件名下載附件
c程序 下載積分: 黑幣 -5
|
|