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

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 16713|回復(fù): 19
打印 上一主題 下一主題
收起左側(cè)

循跡+pwm調(diào)速的小車(chē)源程序

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:242950 發(fā)表于 2017-10-27 22:28 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
小車(chē)的單片機(jī)源程序如下:
  1. #include<reg52.h>
  2. ////////////電機(jī)轉(zhuǎn)動(dòng)
  3. sbit P30=P2^0;
  4. sbit P31=P2^1;
  5. sbit P32=P2^2;
  6. sbit P33=P2^3;
  7. /////////pwm調(diào)試使能端
  8. sbit ENA=P0^0;
  9. sbit ENB=P0^1;
  10. ////////////四路循跡
  11. sbit P10=P1^7;
  12. sbit P11=P1^6;
  13. sbit P12=P1^5;
  14. sbit P13=P1^4;
  15. ////////////////
  16. #define Right_moto_pwm P0^0 //接驅(qū)動(dòng)模塊ENA使能端輸入PWM信號(hào)調(diào)節(jié)速度

  17. void delay(unsigned int t); //函數(shù)聲明

  18. #define Left_moto_pwm P0^1 //接驅(qū)動(dòng)模塊ENB使能端輸入PWM信號(hào)調(diào)節(jié)速度

  19. void Init_Timer0(void);//定時(shí)器初始化
  20.   
  21. ///////////////定義電機(jī)轉(zhuǎn)動(dòng)方向                                                                                                         
  22. #define Left_moto_back {P30=1,P31=0;} //左電機(jī)后退

  23. #define Left_moto_go {P30=0,P31=1;} //左電機(jī)前進(jìn)

  24. #define Left_moto_stop {P30=1,P31=1;} //左電機(jī)停轉(zhuǎn)

  25. #define Right_moto_back {P32=1,P33=0;} //右電機(jī)后退

  26. #define Right_moto_go {P32=0,P33=1;} //右電機(jī)前轉(zhuǎn)

  27. #define Right_moto_stop {P32=1,P33=1;} //右電機(jī)停轉(zhuǎn)

  28. //////////////////////////////

  29. #define uchar unsigned char

  30. #define uint unsigned int

  31. /////////////////////////////
  32. uchar pwm_val_left =0;

  33. uchar push_val_left =0; //左電機(jī)占空比N/10

  34. uchar pwm_val_right =0;

  35. uchar push_val_right=0; //右電機(jī)占空比N/10

  36. bit Right_moto_stp=1;

  37. bit Left_moto_stp =1;
  38. uint num,i,d,j=0;

  39. /****************************************************************

  40. ********/

  41. void run(void) //前進(jìn)函數(shù)

  42. {

  43. push_val_left =17; //PWM 調(diào)節(jié)參數(shù)1-20 1為最慢20是最快 改這個(gè)值可以改變其速度

  44. push_val_right =17; //PWM 調(diào)節(jié)參數(shù)1-20 1為最慢20是最快 改這個(gè)值可以改變其速度



  45. Left_moto_go ; //左電機(jī)前進(jìn)

  46. Right_moto_go ; //右電機(jī)前進(jìn)


  47. }

  48. void run1(void)//前進(jìn)函數(shù)1

  49. {

  50. push_val_left =4.8; //PWM 調(diào)節(jié)參數(shù)1-20 1為最慢20是最快 改這個(gè)值可以改變其速度



  51. push_val_right =4.8; //PWM 調(diào)節(jié)參數(shù)1-20 1為最慢20是最快 改這個(gè)值可以改變其速度



  52. Left_moto_go ; //左電機(jī)前進(jìn)

  53. Right_moto_go ; //右電機(jī)前進(jìn)


  54. }
  55. void run2(void)//前進(jìn)函數(shù)1

  56. {

  57. push_val_left =21; //PWM 調(diào)節(jié)參數(shù)1-20 1為最慢20是最快 改這個(gè)值可以改變其速度



  58. push_val_right =21; //PWM 調(diào)節(jié)參數(shù)1-20 1為最慢20是最快 改這個(gè)值可以改變其速度



  59. Left_moto_go ; //左電機(jī)前進(jìn)

  60. Right_moto_go ; //右電機(jī)前進(jìn)


  61. }

  62. /****************************************************************

  63. ********/

  64. void left(void) //左轉(zhuǎn)函數(shù)

  65. {

  66. push_val_left =19;

  67. push_val_right =19;

  68. Right_moto_go; //右電機(jī)繼續(xù)

  69. Left_moto_stop; //左電機(jī)停走

  70. }

  71. void left1(void) //左轉(zhuǎn)函數(shù)        1

  72. {

  73. push_val_left =20;

  74. push_val_right =20;

  75. Right_moto_go; //右電機(jī)繼續(xù)

  76. Left_moto_stop; //左電機(jī)停走

  77. }


  78. /***************************************  *********************************/

  79. void right(void) //右轉(zhuǎn)函數(shù)

  80. {

  81. push_val_left =19;
  82.                                  
  83. push_val_right =19;

  84. Right_moto_stop; //右電機(jī)停走

  85. Left_moto_go; //左電機(jī)繼續(xù)

  86. }

  87. void right1(void) //右轉(zhuǎn)函數(shù)1

  88. {

  89. push_val_left =20;

  90. push_val_right =20;

  91. Right_moto_stop; //右電機(jī)停走

  92. Left_moto_go; //左電機(jī)繼續(xù)

  93. }

  94. ///////////////////////////////////////////////////////////////停止

  95. void stop(void)

  96. {

  97. Right_moto_stop; //右電機(jī)停走

  98. Left_moto_stop; //左電機(jī)停走



  99. //run();
  100. //
  101. //Delayms(100);

  102. }

  103. ///////////////////////////////////////////

  104. void zzhijiao()
  105. {

  106.    push_val_left =19;
  107.    
  108.    push_val_right =19;       
  109.    
  110.    Left_moto_go ; //左電機(jī)前進(jìn)
  111.    
  112.    Right_moto_back ;
  113.                                                  }
  114. ///////////////////////////////////////
  115. void yzhijiao()
  116. {       
  117.   push_val_left =19;

  118.   push_val_right =19;
  119.   
  120.   Left_moto_back ; //左電機(jī)前進(jìn)
  121.       
  122.   Right_moto_go ;}
  123. /*************************PWM調(diào) 制 電 機(jī) 轉(zhuǎn) 速

  124. ********************************/

  125. void pwm_out_left_moto(void) //左電機(jī)調(diào)速,調(diào)節(jié)push_val_left的值改變電機(jī)轉(zhuǎn)速,占空比



  126. {

  127. if(Left_moto_stp)

  128. {
  129. {if(pwm_val_left<=push_val_left)

  130. { ENB=1;}

  131. else

  132.   {ENB=0;}
  133.   }
  134. {if(pwm_val_left>=20)

  135.   {pwm_val_left=0;}
  136.   }
  137. }

  138. else

  139. {ENB=0;}

  140. }


  141. void pwm_out_right_moto(void) //右電機(jī)調(diào)速,調(diào)節(jié)push_val_left的值改變電機(jī)轉(zhuǎn)速,占空比



  142. {

  143. if(Right_moto_stp)

  144. {

  145. if(pwm_val_right<=push_val_right)

  146. {ENA=1;}

  147. else

  148. {ENA=0;}

  149. if(pwm_val_right>=20)

  150. {pwm_val_right=0;}

  151. }

  152. else

  153. {ENA=0;}
  154.        
  155. }

  156. /***************************************************/

  157. void xunji()

  158. {        
  159.            uchar a=0,b=0;
  160.                                                                                                                                                                                                                

  161.       if(   P10==1&&P11==1&&P12==1&&P13==1)
  162.                 {            
  163.                         i++;
  164.                                             }
  165.       if( P10==0&&P11==0&&P12==0&&P13==0)
  166.         {
  167.                  run();
  168.                                  }   
  169.       if( P10==0&&P11==0&&P12==1&&P13==0||P10==0&&P11==0&&P12==0&&P13==1)
  170.         {          
  171.                   right();
  172.                                    }
  173.       if(          P10==0&&P11==1&&P12==0&&P13==0||P10==1&&P11==0&&P12==0&&P13==0)
  174.                  {  
  175.                       left();
  176.                                     }          
  177.           if(   P10==1&&P11==1&&P12==1&&P13==1&&i>=500)   //600
  178.                  {
  179.                         j++;
  180.                                             }

  181.    while(j==2)

  182.     {           
  183.         if( P10==0&&P11==0&&P12==0&&P13==0)
  184.            {
  185.                  run1();
  186.                                  }   
  187.                                                                
  188.             if(        P10==0&&P11==0&&P12==1&&P13==0||P10==0&&P11==0&&P12==0&&P13==1)
  189.                         {       
  190.                           right();
  191.                                     }
  192.             if( P10==1&&P11==1&&P12==0&&P13==0||P10==1&&P11==1&&P12==1&&P13==0)
  193.                          {
  194.                           
  195.                          yzhijiao();
  196.                          a++;
  197.                                       }
  198.                  if( P10==0&&P11==0&&P12==1&&P13==1||P10==0&&P11==1&&P12==1&&P13==1)
  199.                          {
  200.                           zzhijiao();
  201.               d++;
  202.                
  203.                          }
  204.          if(   P10==0&&P11==1&&P12==0&&P13==0||P10==1&&P11==0&&P12==0&&P13==0)
  205.                                          {        left();}
  206.       
  207.       

  208.                    if(a+d>=2400)
  209.             {
  210.                  if( P10==0&&P11==0&&P12==0&&P13==0)
  211.           {
  212.                  run2();
  213.                                  }   
  214.         if( P10==0&&P11==0&&P12==1&&P13==0||P10==0&&P11==0&&P12==0&&P13==1)
  215.           {          
  216.                   right1();
  217.                                    }
  218.         if(          P10==0&&P11==1&&P12==0&&P13==0||P10==1&&P11==0&&P12==0&&P13==0)
  219.                 {  
  220.                       left1();
  221.                                     }          
  222.            if(   P10==1&&P11==1&&P12==1&&P13==1)   //600
  223.                      {
  224.                          stop();
  225.                                             
  226.                                         }
  227.                                                 }
  228.                                          }
  229.          
  230.                           
  231.                                      }
  232.                                           



  233. /***********TIMER0中斷服務(wù)子函數(shù)產(chǎn)生PWM信號(hào)**********/

  234. void Init_Timer0()interrupt 1 using 2

  235. {


  236. TH0=0XFC; //2Ms定時(shí)

  237. TL0=0X30;

  238.                
  239. ……………………

  240. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
循跡小車(chē).rar (24.16 KB, 下載次數(shù): 489)


評(píng)分

參與人數(shù) 4黑幣 +65 收起 理由
為之則易 + 8 淡定
147wzl + 2 共享資料的黑幣獎(jiǎng)勵(lì)!
kikiikllll + 5 很給力!
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏18 分享淘帖 頂2 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:244394 發(fā)表于 2017-10-30 22:36 | 只看該作者
收藏額   謝謝樓主
回復(fù)

使用道具 舉報(bào)

板凳
ID:275384 發(fā)表于 2018-1-16 22:25 | 只看該作者
完全解決燃眉之急
回復(fù)

使用道具 舉報(bào)

地板
ID:338048 發(fā)表于 2018-5-25 14:28 | 只看該作者
代碼非常清楚,解決了我對(duì)于pwm調(diào)速的疑問(wèn)
回復(fù)

使用道具 舉報(bào)

5#
ID:404723 發(fā)表于 2018-11-6 20:07 | 只看該作者
十分感謝 , 寫(xiě)的非常清楚明了   謝謝謝謝
回復(fù)

使用道具 舉報(bào)

6#
ID:412832 發(fā)表于 2018-12-1 14:48 | 只看該作者
剛好需要 謝謝樓主
回復(fù)

使用道具 舉報(bào)

7#
ID:412832 發(fā)表于 2018-12-1 16:29 | 只看該作者
val是什么意思呢
回復(fù)

使用道具 舉報(bào)

8#
ID:421579 發(fā)表于 2018-12-4 10:44 | 只看該作者
謝謝源代碼,,。
回復(fù)

使用道具 舉報(bào)

9#
ID:458115 發(fā)表于 2018-12-29 15:46 | 只看該作者
厲害了
回復(fù)

使用道具 舉報(bào)

10#
ID:478732 發(fā)表于 2019-2-21 11:22 | 只看該作者
代碼寫(xiě)的很可以,謝謝大佬相助
回復(fù)

使用道具 舉報(bào)

11#
ID:509968 發(fā)表于 2019-4-11 20:37 | 只看該作者
真好啊
回復(fù)

使用道具 舉報(bào)

12#
ID:510468 發(fā)表于 2019-4-12 14:06 | 只看該作者
非常好
回復(fù)

使用道具 舉報(bào)

13#
ID:489425 發(fā)表于 2019-4-12 15:50 | 只看該作者
請(qǐng)問(wèn)具體的單片機(jī)型號(hào)是什么
回復(fù)

使用道具 舉報(bào)

14#
ID:324545 發(fā)表于 2019-4-22 14:43 | 只看該作者
不錯(cuò)啊啊啊
回復(fù)

使用道具 舉報(bào)

15#
ID:538577 發(fā)表于 2019-5-15 17:18 | 只看該作者
厲害
回復(fù)

使用道具 舉報(bào)

16#
ID:546593 發(fā)表于 2019-5-24 20:34 | 只看該作者
真不錯(cuò)
回復(fù)

使用道具 舉報(bào)

17#
ID:550631 發(fā)表于 2019-6-1 10:13 | 只看該作者
謝謝樓主,非常有用
回復(fù)

使用道具 舉報(bào)

18#
ID:487298 發(fā)表于 2019-11-3 21:02 | 只看該作者
謝謝,幫到我不少!
回復(fù)

使用道具 舉報(bào)

19#
ID:242950 發(fā)表于 2020-3-1 12:56 | 只看該作者
嫌的無(wú)聊,曬曬
回復(fù)

使用道具 舉報(bào)

20#
ID:28260 發(fā)表于 2021-7-24 08:08 | 只看該作者

謝謝樓主,非常有用,學(xué)習(xí)了!
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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