解壓C51FPS.rar
把C51FPS.LIB這個(gè)文件放到\Keil\C51\LIB下面,這個(gè)文件有浮點(diǎn)運(yùn)算
單片機(jī)源程序如下:
- /**********************ZYWIFI0939C-WIFI機(jī)器人實(shí)驗(yàn)板例程************************
- * 平臺(tái):ZYWIFI0939C-WIFI機(jī)器人 + Keil U4 + STC89C52
- * 名稱(chēng):ZY-1智能小車(chē)參考程序
- * 公司:湖南智宇科教設(shè)備有限公司
- * 編寫(xiě):智宇公司研發(fā)一部
- * 日期:2015-1-15
- * 交流:智能車(chē)QQ:261339276
- * 晶振:11.0592MHZ
- * 說(shuō)明:免費(fèi)開(kāi)源,不提供源代碼分析
- * 硬件設(shè)置:要有自己動(dòng)手能力,才能完成實(shí)驗(yàn)
- * 使用說(shuō)明:根據(jù)下面IO口自己用杜邦線連接各種模塊,可以自己修改各種模塊IO口
- * 視頻教程:本小車(chē)配套學(xué)習(xí)C語(yǔ)言詳細(xì)視頻教程,資料統(tǒng)一網(wǎng)盤(pán)下載
- 重點(diǎn)提示:本程序只做參考,不提供技術(shù)支持,請(qǐng)自己研究吸收。
- ******************************************************************/
- #include <at89x51.h>
- #include <intrins.h>
- #define TX P2_1
- #define RX P2_0
- #define Forward_L_DATA 180//當(dāng)前進(jìn)不能走直線的時(shí)候,請(qǐng)調(diào)節(jié)這兩個(gè)參數(shù),理想的時(shí)候是100,100,最大256,最小0。0的時(shí)候最慢,256的時(shí)候最快
- #define Forward_R_DATA 180 //例如小車(chē)前進(jìn)的時(shí)候有點(diǎn)向左拐,說(shuō)明右邊馬達(dá)轉(zhuǎn)速過(guò)快,那可以取一個(gè)值大一點(diǎn),另外一個(gè)值小一點(diǎn),例如 200 190
- //直流電機(jī)因?yàn)橹圃焐系恼`差,同一個(gè)脈寬下也不一定速度一致的,需要自己手動(dòng)調(diào)節(jié)
- /*****按照原圖接線定義******/
- sbit L293D_IN1=P1^2;
- sbit L293D_IN2=P1^3;
- sbit L293D_IN3=P1^4;
- sbit L293D_IN4=P1^5;
- sbit L293D_EN1=P1^6;
- sbit L293D_EN2=P1^7;
- sbit BUZZ=P2^3;
- void Delay400Ms(void);//延時(shí)400毫秒函數(shù)
- unsigned char disbuff[4]={0,0,0,0};//用于分別存放距離的值0.1mm、mm、cm和m的值
- void Count(void);//距離計(jì)算函數(shù)
-
- unsigned int time=0;//用于存放定時(shí)器時(shí)間值
- unsigned long S=0;//用于存放距離的值
- bit flag =0; //量程溢出標(biāo)志位
- bit turn_right_flag;
- //**********************************************************
- //函數(shù)名稱(chēng):Delay1ms(unsigned int i)
- //函數(shù)功能:延時(shí)i*1ms的子程序(對(duì)應(yīng)于22.1184Mhz晶振)
- //形式參數(shù):unsigned int i
- //行參說(shuō)明:無(wú)
- //返回參數(shù):無(wú)
- //使用說(shuō)明:i為要延時(shí)的時(shí)間長(zhǎng)度,單位是MS,最大可以延時(shí)65536 ms
- //**********************************************************
- void Delay1ms(unsigned int i)
- {
- unsigned char j,k;
- do{
- j = 10;
- do{
- k = 50;
- do{
- _nop_();
- }while(--k);
- }while(--j);
- }while(--i);
- }
- //**********************************************************
- //函數(shù)名稱(chēng):Delay10us(unsigned char i)
- //函數(shù)功能:延時(shí)i*10us的子程序(對(duì)應(yīng)于22.1184Mhz晶振)
- //形式參數(shù):無(wú)
- //行參說(shuō)明:無(wú)
- //返回參數(shù):無(wú)
- //使用說(shuō)明:i為要延時(shí)的時(shí)間長(zhǎng)度,單位是US,最大可以延時(shí)250 ms
- //**********************************************************
- void Delay10us(unsigned char i)
- {
- unsigned char j;
- do{
- j = 10;
- do{
- _nop_();
- }while(--j);
- }while(--i);
- }
- //=========================================================================================================================
- void Forward()// 前進(jìn)
- {
- L293D_IN1=1;
- L293D_IN2=0;
- L293D_IN3=1;
- L293D_IN4=0;
- // PWM_Set(255-Speed_Right,255-Speed_Left);
- }
- void backrun()// 后退
- {
- L293D_IN1=0;
- L293D_IN2=1;
- L293D_IN3=0;
- L293D_IN4=1;
- // PWM_Set(255-Speed_Right,255-Speed_Left);
- }
- void Stop(void) //剎車(chē)
- {
- L293D_IN1=0;
- L293D_IN2=0;
- L293D_IN3=0;
- L293D_IN4=0;
- // PWM_Set(0,0);
- }
- void Turn_Retreat() //左
- {
- L293D_IN1=0;
- L293D_IN2=0;
- L293D_IN3=0;
- L293D_IN4=1;
- // PWM_Set(255-Speed_Right,255-Speed_Left);
- }
- void Turn_left() //右
- {
- L293D_IN1=1;
- L293D_IN2=0;
- L293D_IN3=0;
- L293D_IN4=0;
- // PWM_Set(255-Speed_Right,255-Speed_Left);
- }
- //=========================================================================================================================
- /********距離計(jì)算程序***************/
- void Conut(void)
- {
- time=TH1*256+TL1;
- TH1=0;
- TL1=0;
-
- //此時(shí)time的時(shí)間單位決定于晶振的速度,外接晶振為11.0592MHZ時(shí),
- //time的值為0.54us*time,單位為微秒
- //那么1us聲波能走多遠(yuǎn)的距離呢?1s=1000ms=1000000us
- // 340/1000000=0.00034米
- //0.00034米/1000=0.34毫米 也就是1us能走0.34毫米
- //但是,我們現(xiàn)在計(jì)算的是從超聲波發(fā)射到反射接收的雙路程,
- //所以我們將計(jì)算的結(jié)果除以2才是實(shí)際的路程
- S=time*2;//先算出一共的時(shí)間是多少微秒。
- S=S*0.17;//此時(shí)計(jì)算到的結(jié)果為毫米,并且是精確到毫米的后兩位了,有兩個(gè)小數(shù)點(diǎn)
- if(S<=400) //
- {
- if(turn_right_flag!=1)
- {
- Stop();
- Delay1ms(5);//發(fā)現(xiàn)小車(chē)自動(dòng)復(fù)位的時(shí)候,可以稍微延長(zhǎng)一點(diǎn)這個(gè)延時(shí),減少電機(jī)反向電壓對(duì)電路板的沖擊。
- }
- turn_right_flag=1;
-
- P2_3=0;
- Delay1ms(50);
-
- P2_3=1;
- backrun();
- Delay1ms(300); // 關(guān)鍵點(diǎn) 延時(shí)5MS
- Turn_left();
- Delay1ms(400); //左轉(zhuǎn)800MS
- }
- else
- {
- turn_right_flag=0;
- //Forward(Forward_R_DATA,Forward_L_DATA);
- Forward();
- }
- //=======================================
- if((S>=5000)||flag==1) //超出測(cè)量范圍
- {
- flag=0;
- //DisplayListChar(0, 1, table1);
- }
- else
- {
- disbuff[0]=S%10;
- disbuff[1]=S/10%10;
- disbuff[2]=S/100%10;
- disbuff[3]=S/1000;
- }
- }
- /********************************************************/
- void zd0() interrupt 3 //T0中斷用來(lái)計(jì)數(shù)器溢出,超過(guò)測(cè)距范圍
- {
- flag=1; //中斷溢出標(biāo)志
- RX=0;
- }
- /********超聲波高電平脈沖寬度計(jì)算程序***************/
- void Timer_Count(void)
- {
- TR1=1; //開(kāi)啟計(jì)數(shù)
- while(RX); //當(dāng)RX為1計(jì)數(shù)并等待
- TR1=0; //關(guān)閉計(jì)數(shù)
- Conut(); //計(jì)算
- }
- /********************************************************/
- void StartModule() //啟動(dòng)模塊
- {
- TX=1; //啟動(dòng)一次模塊
- Delay10us(2);
- TX=0;
- }
- /********************************************************/
- /*************主程序********************/
- void main(void)
- {
- unsigned char i;
- unsigned int a;
- Delay1ms(5);//延時(shí)片刻
- TMOD=TMOD|0x10;//設(shè)T0為方式1,GATE=1;
- EA=1;
- TH1=0;
- TL1=0;
- ET1=1; //允許T0中斷
- //開(kāi)啟總中斷
- turn_right_flag=0;
- //=================================
- B: for(i=0;i<50;i++) //判斷K3是否按下
- {
- Delay1ms(1); //1ms內(nèi)判斷50次,如果其中有一次被判斷到K3沒(méi)按下,便重新檢測(cè)
- if(P3_2!=0 )//當(dāng)S1按下時(shí),啟動(dòng)小車(chē)
- goto B; //跳轉(zhuǎn)到標(biāo)號(hào)B,重新檢測(cè)
- }
- //蜂鳴器響一聲
- BUZZ=0; //50次檢測(cè)K3確認(rèn)是按下之后,蜂鳴器發(fā)出“滴”聲響,然后啟動(dòng)小車(chē)。
- Delay1ms(50);
- BUZZ=1;//響50ms后關(guān)閉蜂鳴器
- //=======================================================================================================================
- while(1)
- {
- RX=1;
- StartModule();
- for(a=951;a>0;a--)
- {
-
- if(RX==1)
- {
- Timer_Count();
- }
- }
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
51黑論壇_9、ZYWIFI0939C超聲波避障程序(不帶1602顯示).rar
(54.07 KB, 下載次數(shù): 29)
2018-7-10 09:20 上傳
點(diǎn)擊文件名下載附件
超聲波避障
|