小車測速通電后速度直接變成255CM/S,過了蠻久才會變化,而且靜止狀態的情況下速度不歸零。
單片機源程序如下:
- #include<AT89x51.H>
- #include <intrins.h>
- #include "LCD1602display.h"
- #include<HJ-4WD_PWM.H> //包含HL-1藍牙智能小車驅動IO口定義等函數
- /* 右電機測速線接 P3.2 左電機測速線接 P3.3*/
- #define Left_1_led P3_7 // 左循跡傳感器 由于測速程序 必須使用外部中斷 所以之前的循跡傳感器IO由P3.3改為P3.7
- #define Right_1_led P3_6 //右循跡傳感器 由于測速程序 必須使用外部中斷 所以之前的循跡傳感器IO由P3.2改為P3.6
-
- unsigned char disbuff[10]={0};//用于分別存放速度的值的值
- unsigned char time=0; //顯示緩存
- unsigned char i =0; //定義掃描數碼管字數
- unsigned int count1=0; //計右電機碼盤脈沖值
- unsigned int count2=0; //計左電機碼盤脈沖值
- bit count3=0;
- bit count5=0;
- unsigned int count4=0;
- unsigned int V1=0; //定義右電機其速度
- unsigned int V2=0; //定義左電機其速度
- unsigned int V3=0;
- void Delay400Ms(void);//延時400毫秒函數
- unsigned char code Range[] ="==Range Finder==";//LCD1602顯示格式
- unsigned char code ASCII[15] ="0123456789CM S";
- unsigned char code ASCII1[11] ="0123456789S";
-
- unsigned char code ASCIIR[13] = "RMspeed:";
- unsigned char code ASCIIL[13] = "T:";
-
- unsigned char code ASCIIL1[13] = "L:";
- unsigned char code ASCII2[12] ="0123456789CM";
- unsigned char code table[]="Distance:001.0cm";
- unsigned char code table1[]="!!! Out of range";
- void Count(void);//距離計算函數
- void Display_LCD();
- unsigned long S=0;//用于存放距離的值
- bit flag =0; //量程溢出標志位
- bit turn_right_flag;
-
- /************************************************************************/
- ///*TIMER0中斷服務子函數產生2MS定時掃描數碼管與產生0。5S*/
- void timer0()interrupt 1 using 2
- {
- TH0=(65536-1850)/256; //2MS定時
- TL0=(65536-1850)%256;
- time++;
- // Display_SMG();
- if(time>=250) //250次即是,0。5S
- {
- time=0;
- //V1=count1*2; //計數公式:輪子直徑*3.14/20格碼盤=6.5Cm*3.14/20約=1cm 即一個脈沖走1CM距離
- //圓周長公式 C = 2πr 所以將圓嚴格20等分 自然 一格就是約 1CM左右
- // 0.5S 到了 計算在0.5S時間內的 脈沖數 因為1個脈沖基本是1CM 根據 S=VT公式 算出 V = S/T
- // 即 ((count1*1CM))/0.5S= (count1*2)CM/S
- //count1=0; //清計數 0.5S 時間內 測一次速度 然后清0 重新 計數
- V1=V3/V2;
-
- disbuff[0]=V1/100; //右電機速度值百位
- disbuff[1]=V1%100/10; //右電機速度值十位
- disbuff[2]=V1%100%10; //右電機速度值個位
-
-
-
- if(count3==1)
- {
- V2=count2/2; //計數公式:輪子直徑*3.14/20格碼盤=6.5Cm*3.14/20約=1cm 即一個脈沖走1CM距離
- //圓周長公式 C = 2πr 所以將圓嚴格20等分 自然 一格就是約 1CM左右
- // 0.5S 到了 計算在0.5S時間內的 脈沖數 因為1個脈沖基本是1CM 根據 S=VT公式 算出 V = S/T
- // 即 ((count1*1CM))/0.5S= (count1*2)CM/S
- count2++; //清計數 0.5S 時間內 測一次速度 然后清0 重新 計數
-
- disbuff[3]=V2/100; //左電機速度值百位
- disbuff[4]=V2%100/10; //左電機速度值十位
- disbuff[5]=V2%100%10; //左電機速度值個位
- count3=0;
- }
-
- if(count5==1)
- {
- V3=count4/3; //計數公式:輪子直徑*3.14/20格碼盤=6.5Cm*3.14/20約=1cm 即一個脈沖走1CM距離
- //圓周長公式 C = 2πr 所以將圓嚴格20等分 自然 一格就是約 1CM左右
- // 0.5S 到了 計算在0.5S時間內的 脈沖數 因為1個脈沖基本是1CM 根據 S=VT公式 算出 V = S/T
- // 即 ((count1*1CM))/0.5S= (count1*2)CM/S
- count4++; //清計數 0.5S 時間內 測一次速度 然后清0 重新 計數
-
- disbuff[6]=V3/1000; //左電機速度值百位
- disbuff[7]=V3%1000/100; //左電機速度值十位
- disbuff[8]=V3%100/10; //左電機速度值個位
- disbuff[9]=V3%100%10;
- count5=0;
- }
-
- }
- }
- void Display_LCD()
- {
- // unsigned char code ASCII[15] ="0123456789CM S";
- // unsigned char code ASCIIR[13] = "RMspeed:";
- // unsigned char code ASCIIL[13] = "Time:";
- // unsigned char code table[]="Distance:001.0cm";
- // unsigned char code table1[]="!!! Out of range";
- // unsigned char code ASCIIL1[13] = "L:";
- // unsigned char code ASCII2[12] ="0123456789CM";
- DisplayOneChar(0, 1, ASCIIR[0]); //
- DisplayOneChar(1, 1, ASCIIR[1]); //
- DisplayOneChar(2, 1, ASCIIR[2]); //
- DisplayOneChar(3, 1, ASCIIR[3]); //
- DisplayOneChar(4, 1, ASCIIR[4]); //
- DisplayOneChar(5, 1, ASCIIR[5]); //
- DisplayOneChar(6, 1, ASCIIR[6]); //
- DisplayOneChar(7, 1, ASCIIR[7]); //
- DisplayOneChar(8, 1, ASCII[disbuff[0]]); //右電機速度值百位
- DisplayOneChar(9, 1, ASCII[disbuff[1]]); //右電機速度值十位
- DisplayOneChar(10, 1, ASCII[disbuff[2]]); //右電機速度值個位
- DisplayOneChar(11, 1, ASCII[10]); //
- DisplayOneChar(12, 1, ASCII[11]); //
- DisplayOneChar(13, 1, 0x2f); //根據1602的字符表找到 /
- DisplayOneChar(14, 1, ASCII[13]); //
- DisplayOneChar(0, 0, ASCIIL[0]); //
- DisplayOneChar(1, 0, ASCIIL[1]); //
- DisplayOneChar(2, 0, ASCII1[disbuff[3]]); //左電機速度值百位
- DisplayOneChar(3, 0, ASCII1[disbuff[4]]); //左電機速度值十位
- DisplayOneChar(4, 0, ASCII1[disbuff[5]]); //左電機速度值個位
- DisplayOneChar(5, 0, ASCII1[10]); //
-
- DisplayOneChar(7, 0, ASCIIL1[0]); //
- DisplayOneChar(8, 0, ASCIIL1[1]);
-
- DisplayOneChar(9, 0, ASCII2[disbuff[6]]);
- DisplayOneChar(10, 0, ASCII2[disbuff[7]]);
- DisplayOneChar(11, 0, ASCII2[disbuff[8]]);
- DisplayOneChar(12, 0, ASCII2[disbuff[9]]);
- DisplayOneChar(13, 0, ASCII2[10]); //
- DisplayOneChar(14, 0, ASCII2[11]);
-
- }
- /***************************************************/
- //外部0中斷用于計算左輪的脈沖
- void intersvr1(void) interrupt 0 using 1
- {
- count1++;
- count4++;
- count3=1;
- count5=1;
-
- }
- void intersvr2(void) interrupt 2 using 2
- {
- count2++;
-
- }
- /************************************************************************/
- /*********************************************************************/
- /*--主函數--*/
- void main(void)
- {
- //cmg88();//關數碼管
- Delay1ms(400); //啟動等待,等LCM講入工作狀態
- LCMInit(); //LCM初始化
- Delay1ms(5);//延時片刻
- TMOD=0X11;
- TH0=(65536-2000)/256; //2MS定時
- TL0=(65536-2000)%256;
- TR0= 1;
- ET0= 1;
- TH1= 0XFc; //1ms定時
- TL1= 0X18;
- TR1= 1;
- ET1= 1;
- EX0=1; //開啟外部中斷0
- IT0=1; //下降沿有效
- IE0=0;
- EX1=1; //開啟外部中斷1
- IT1=1; //下降沿有效
- IE1=0;
- EA = 1;
- // run();
-
- while(1)
- {
- Display_LCD();
- //有信號為0 沒有信號為1
-
- if(Left_1_led==0&&Right_1_led==0)
- run(); //調用前進函數
- else
- {
- if(Left_1_led==1&&Right_1_led==0) //左邊檢測到黑線
- {
- leftrun(); //調用小車左轉 函數
- }
-
- if(Right_1_led==1&&Left_1_led==0) //右邊檢測到黑線
- {
- rightrun(); //調用小車右轉 函數
- }
- if(Right_1_led==1&&Left_1_led==1) //懸空
- {
- stop(); //調用小車停止 函數
- }
- }
-
- }
- }
-
復制代碼
所有資料51hei提供下載:
13. ZYWIFI0939-3循跡 測速程序(PWM加強版 1602顯示速度).rar
(50.33 KB, 下載次數: 9)
2019-11-5 17:21 上傳
點擊文件名下載附件
|