欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 1575|回復(fù): 0
收起左側(cè)

單片機(jī)小車測(cè)速程序問題,靜止?fàn)顟B(tài)的情況下速度不歸零

[復(fù)制鏈接]
ID:616526 發(fā)表于 2019-11-5 17:22 | 顯示全部樓層 |閱讀模式
小車測(cè)速通電后速度直接變成255CM/S,過了蠻久才會(huì)變化,而且靜止?fàn)顟B(tài)的情況下速度不歸零。

單片機(jī)源程序如下:
  1. #include<AT89x51.H>
  2. #include <intrins.h>
  3. #include "LCD1602display.h"
  4.         #include<HJ-4WD_PWM.H>                  //包含HL-1藍(lán)牙智能小車驅(qū)動(dòng)IO口定義等函數(shù)
  5. /*   右電機(jī)測(cè)速線接 P3.2    左電機(jī)測(cè)速線接 P3.3*/
  6.   #define Left_1_led        P3_7         // 左循跡傳感器       由于測(cè)速程序  必須使用外部中斷  所以之前的循跡傳感器IO由P3.3改為P3.7
  7.         #define Right_1_led       P3_6         //右循跡傳感器                    由于測(cè)速程序  必須使用外部中斷  所以之前的循跡傳感器IO由P3.2改為P3.6
  8.             

  9.   unsigned char disbuff[10]={0};//用于分別存放速度的值的值       
  10.         unsigned char  time=0;                                                                                          //顯示緩存
  11.   unsigned char  i =0;                  //定義掃描數(shù)碼管字?jǐn)?shù)
  12.         unsigned int  count1=0;            //計(jì)右電機(jī)碼盤脈沖值
  13.         unsigned int  count2=0;            //計(jì)左電機(jī)碼盤脈沖值
  14.         bit count3=0;       
  15.         bit count5=0;
  16.         unsigned int  count4=0;
  17.         unsigned int   V1=0;                   //定義右電機(jī)其速度
  18.         unsigned int   V2=0;                   //定義左電機(jī)其速度
  19.   unsigned int   V3=0;
  20.         void Delay400Ms(void);//延時(shí)400毫秒函數(shù)

  21. unsigned char code Range[] ="==Range Finder==";//LCD1602顯示格式
  22. unsigned char code ASCII[15] ="0123456789CM S";
  23. unsigned char code ASCII1[11] ="0123456789S";
  24.        
  25. unsigned char code ASCIIR[13] = "RMspeed:";
  26. unsigned char code ASCIIL[13] = "T:";
  27.        
  28. unsigned char code ASCIIL1[13] = "L:";
  29. unsigned char code ASCII2[12] ="0123456789CM";

  30. unsigned char code table[]="Distance:001.0cm";
  31. unsigned char code table1[]="!!! Out of range";

  32. void Count(void);//距離計(jì)算函數(shù)
  33. void  Display_LCD();                          

  34. unsigned long S=0;//用于存放距離的值
  35. bit  flag =0; //量程溢出標(biāo)志位
  36. bit  turn_right_flag;
  37.    
  38. /************************************************************************/
  39. ///*TIMER0中斷服務(wù)子函數(shù)產(chǎn)生2MS定時(shí)掃描數(shù)碼管與產(chǎn)生0。5S*/
  40.         void timer0()interrupt 1   using 2
  41. {
  42.       TH0=(65536-1850)/256;                //2MS定時(shí)
  43.       TL0=(65536-1850)%256;

  44.          time++;
  45. //         Display_SMG();
  46.          if(time>=250)        //250次即是,0。5S
  47.          {
  48.           time=0;
  49.           //V1=count1*2;         //計(jì)數(shù)公式:輪子直徑*3.14/20格碼盤=6.5Cm*3.14/20約=1cm  即一個(gè)脈沖走1CM距離
  50.                          //圓周長(zhǎng)公式     C = 2πr          所以將圓嚴(yán)格20等分  自然 一格就是約 1CM左右
  51.                          // 0.5S  到了   計(jì)算在0.5S時(shí)間內(nèi)的         脈沖數(shù)   因?yàn)?個(gè)脈沖基本是1CM  根據(jù) S=VT公式  算出  V = S/T
  52.                                          // 即  ((count1*1CM))/0.5S= (count1*2)CM/S          
  53.           //count1=0;                 //清計(jì)數(shù)    0.5S  時(shí)間內(nèi)  測(cè)一次速度     然后清0  重新 計(jì)數(shù)
  54.                 V1=V3/V2;

  55.                  
  56.           disbuff[0]=V1/100;                 //右電機(jī)速度值百位
  57.           disbuff[1]=V1%100/10;                //右電機(jī)速度值十位
  58.     disbuff[2]=V1%100%10;                //右電機(jī)速度值個(gè)位

  59.                  
  60.                  
  61.                 if(count3==1)
  62.                 {
  63.           V2=count2/2;         //計(jì)數(shù)公式:輪子直徑*3.14/20格碼盤=6.5Cm*3.14/20約=1cm  即一個(gè)脈沖走1CM距離
  64.                          //圓周長(zhǎng)公式     C = 2πr          所以將圓嚴(yán)格20等分  自然 一格就是約 1CM左右
  65.                          // 0.5S  到了   計(jì)算在0.5S時(shí)間內(nèi)的         脈沖數(shù)   因?yàn)?個(gè)脈沖基本是1CM  根據(jù) S=VT公式  算出  V = S/T
  66.                                                  // 即  ((count1*1CM))/0.5S= (count1*2)CM/S
  67.           count2++;                   //清計(jì)數(shù)    0.5S  時(shí)間內(nèi)  測(cè)一次速度     然后清0  重新 計(jì)數(shù)
  68.                                                                                                                                                                                                                                                                                                             
  69.           disbuff[3]=V2/100;                 //左電機(jī)速度值百位
  70.           disbuff[4]=V2%100/10;                 //左電機(jī)速度值十位
  71.     disbuff[5]=V2%100%10;                //左電機(jī)速度值個(gè)位
  72.           count3=0;
  73.                 }
  74.                
  75.                 if(count5==1)
  76.                 {
  77.           V3=count4/3;         //計(jì)數(shù)公式:輪子直徑*3.14/20格碼盤=6.5Cm*3.14/20約=1cm  即一個(gè)脈沖走1CM距離
  78.                        //圓周長(zhǎng)公式     C = 2πr          所以將圓嚴(yán)格20等分  自然 一格就是約 1CM左右
  79.                        // 0.5S  到了   計(jì)算在0.5S時(shí)間內(nèi)的         脈沖數(shù)   因?yàn)?個(gè)脈沖基本是1CM  根據(jù) S=VT公式  算出  V = S/T
  80.                                                // 即  ((count1*1CM))/0.5S= (count1*2)CM/S
  81.           count4++;                 //清計(jì)數(shù)    0.5S  時(shí)間內(nèi)  測(cè)一次速度     然后清0  重新 計(jì)數(shù)
  82.                                                                                                                                                                                                                                                                                                             
  83.           disbuff[6]=V3/1000;                 //左電機(jī)速度值百位
  84.           disbuff[7]=V3%1000/100;                 //左電機(jī)速度值十位
  85.     disbuff[8]=V3%100/10;                //左電機(jī)速度值個(gè)位
  86.           disbuff[9]=V3%100%10;
  87.           count5=0;
  88.                 }
  89.                
  90.          }
  91. }
  92. void  Display_LCD()
  93. {
  94. //          unsigned char code ASCII[15] ="0123456789CM S";
  95. //    unsigned char code ASCIIR[13] = "RMspeed:";
  96. //    unsigned char code ASCIIL[13] = "Time:";
  97. //          unsigned char code table[]="Distance:001.0cm";
  98. //    unsigned char code table1[]="!!! Out of range";
  99. //    unsigned char code ASCIIL1[13] = "L:";
  100. //    unsigned char code ASCII2[12] ="0123456789CM";
  101.     DisplayOneChar(0, 1, ASCIIR[0]);          //
  102.           DisplayOneChar(1, 1, ASCIIR[1]);          //
  103.           DisplayOneChar(2, 1, ASCIIR[2]);          //
  104.           DisplayOneChar(3, 1, ASCIIR[3]);          //
  105.           DisplayOneChar(4, 1, ASCIIR[4]);          //
  106.           DisplayOneChar(5, 1, ASCIIR[5]);          //
  107.           DisplayOneChar(6, 1, ASCIIR[6]);          //
  108.           DisplayOneChar(7, 1, ASCIIR[7]);          //

  109.           DisplayOneChar(8, 1, ASCII[disbuff[0]]);          //右電機(jī)速度值百位
  110.           DisplayOneChar(9, 1, ASCII[disbuff[1]]);          //右電機(jī)速度值十位
  111.           DisplayOneChar(10, 1, ASCII[disbuff[2]]);           //右電機(jī)速度值個(gè)位
  112.           DisplayOneChar(11, 1, ASCII[10]);           //
  113.           DisplayOneChar(12, 1, ASCII[11]);           //
  114.           DisplayOneChar(13, 1, 0x2f);          //根據(jù)1602的字符表找到 /
  115.           DisplayOneChar(14, 1, ASCII[13]);           //

  116.           DisplayOneChar(0, 0, ASCIIL[0]);          //
  117.           DisplayOneChar(1, 0, ASCIIL[1]);          //

  118.           DisplayOneChar(2, 0, ASCII1[disbuff[3]]);           //左電機(jī)速度值百位
  119.           DisplayOneChar(3, 0, ASCII1[disbuff[4]]);           //左電機(jī)速度值十位
  120.           DisplayOneChar(4, 0, ASCII1[disbuff[5]]);           //左電機(jī)速度值個(gè)位
  121.     DisplayOneChar(5, 0, ASCII1[10]);           //
  122.                
  123.                 DisplayOneChar(7, 0, ASCIIL1[0]);          //
  124.           DisplayOneChar(8, 0, ASCIIL1[1]);
  125.                
  126.                 DisplayOneChar(9, 0, ASCII2[disbuff[6]]);          
  127.           DisplayOneChar(10, 0, ASCII2[disbuff[7]]);         
  128.           DisplayOneChar(11, 0, ASCII2[disbuff[8]]);          
  129.                 DisplayOneChar(12, 0, ASCII2[disbuff[9]]);          
  130.     DisplayOneChar(13, 0, ASCII2[10]);           //
  131.     DisplayOneChar(14, 0, ASCII2[11]);

  132.           

  133. }
  134. /***************************************************/
  135. //外部0中斷用于計(jì)算左輪的脈沖
  136.    void intersvr1(void) interrupt 0 using 1

  137. {
  138.     count1++;
  139.           count4++;
  140.           count3=1;
  141.           count5=1;
  142.        
  143. }
  144.    void intersvr2(void) interrupt 2 using 2

  145. {
  146.     count2++;
  147.   
  148. }
  149. /************************************************************************/


  150. /*********************************************************************/                 
  151. /*--主函數(shù)--*/
  152.         void main(void)
  153. {
  154.            //cmg88();//關(guān)數(shù)碼管
  155.         Delay1ms(400); //啟動(dòng)等待,等LCM講入工作狀態(tài)
  156.         LCMInit(); //LCM初始化
  157.         Delay1ms(5);//延時(shí)片刻

  158.     TMOD=0X11;
  159.     TH0=(65536-2000)/256;                //2MS定時(shí)
  160.     TL0=(65536-2000)%256;
  161.         TR0= 1;
  162.         ET0= 1;

  163.         TH1= 0XFc;                  //1ms定時(shí)
  164.     TL1= 0X18;
  165.     TR1= 1;
  166.     ET1= 1;

  167.         EX0=1;           //開啟外部中斷0
  168.     IT0=1;           //下降沿有效
  169.         IE0=0;

  170.         EX1=1;           //開啟外部中斷1
  171.     IT1=1;           //下降沿有效
  172.         IE1=0;
  173.         EA = 1;         
  174. //   run();
  175.        
  176.         while(1)
  177.         {
  178.            Display_LCD();                
  179.                          //有信號(hào)為0  沒有信號(hào)為1
  180.          
  181.               if(Left_1_led==0&&Right_1_led==0)

  182.                           run();   //調(diào)用前進(jìn)函數(shù)

  183.                           else
  184.                          {                          
  185.                                                if(Left_1_led==1&&Right_1_led==0)            //左邊檢測(cè)到黑線
  186.                                   {
  187.                                            leftrun();                  //調(diào)用小車左轉(zhuǎn)  函數(shù)

  188.                              }
  189.                           
  190.                                                              if(Right_1_led==1&&Left_1_led==0)                //右邊檢測(cè)到黑線
  191.                                   {          
  192.                                       rightrun();                   //調(diào)用小車右轉(zhuǎn)        函數(shù)

  193.                                   }
  194.                                                       if(Right_1_led==1&&Left_1_led==1)                //懸空
  195.                                   {          
  196.                                       stop();                   //調(diào)用小車停止        函數(shù)

  197.                                   }

  198.                         }

  199.                                          
  200.          }

  201. }
  202.        
復(fù)制代碼

所有資料51hei提供下載:
13. ZYWIFI0939-3循跡 測(cè)速程序(PWM加強(qiáng)版 1602顯示速度).rar (50.33 KB, 下載次數(shù): 9)

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表