標(biāo)題: 51單片機(jī)小車避障代碼 [打印本頁]

作者: 牛嘟嘟    時(shí)間: 2017-6-17 18:56
標(biāo)題: 51單片機(jī)小車避障代碼
51單片機(jī)小車避障代碼:
避障.docx (15.81 KB, 下載次數(shù): 13)

  1. #include<AT89x51.H>
  2. #include <intrins.h>
  3. #define Sevro_moto_pwm P2_7 //接舵機(jī)信號(hào)端輸入PWM信號(hào)調(diào)節(jié)速度
  4. #define ECHO P1_1 //超聲波接口定義
  5. #define TRIG P1_0 //超聲波接口定義
  6. #define Left_moto_go {P1_4=1,P1_5=0;} //左邊兩個(gè)電機(jī)向前走
  7. #define Left_moto_back {P1_4=0,P1_5=1;} //左邊兩個(gè)電機(jī)向后轉(zhuǎn)
  8. #define Left_moto_Stop {P1_4=0,P1_5=0;} //左邊兩個(gè)電機(jī)停轉(zhuǎn)
  9. #define Right_moto_go {P1_6=1,P1_7=0;} //右邊兩個(gè)電機(jī)向前走
  10. #define Right_moto_back {P1_6=0,P1_7=1;} //右邊兩個(gè)電機(jī)向后走
  11. #define Right_moto_Stop {P1_6=0,P1_7=0;} //右邊兩個(gè)電機(jī)停轉(zhuǎn)
  12. #define DJEAEBGAO {P1_0=1,P1_1=1;}
  13. #define DJEAEBDI {P1_0=0,P1_1=0;}

  14. unsigned char pwm_val_left = 0;//變量定義
  15. unsigned char push_val_left =15;//舵機(jī)歸中,產(chǎn)生約,1.5MS 信號(hào)
  16. unsigned long S=0;
  17. unsigned long S1=0;
  18. unsigned long S2=0;
  19. unsigned long S3=0;
  20. unsigned long S4=0;
  21. unsigned int time=0; //時(shí)間變量
  22. unsigned int timer=0; //延時(shí)基準(zhǔn)變量
  23. /************************************************************************/
  24. void delay(unsigned int k) //延時(shí)函數(shù)
  25. {
  26.         unsigned int x,y;
  27.         for(x=0;x<k;x++)
  28.         for(y=0;y<2000;y++);
  29. }
  30. void pwm_leftdianji()
  31. {
  32.         DJEAEBGAO;
  33.         delay(3);
  34.         DJEAEBDI;
  35.         delay(1);
  36. }


  37. void StartModule() //啟動(dòng)測(cè)距信號(hào)
  38. {
  39.         TRIG=1;
  40.         _nop_();
  41.         _nop_();
  42.         _nop_();
  43.         _nop_();
  44.         _nop_();
  45.         _nop_();
  46.         _nop_();
  47.         _nop_();
  48.         _nop_();
  49.         _nop_();
  50.         _nop_();
  51.         _nop_();
  52.         TRIG=0;
  53. }
  54. /***************************************************/
  55. void Conut(void) //計(jì)算距離
  56. {
  57.         while(!ECHO&&timer<=250); //當(dāng)RX為零時(shí)等待,等待25ms沒信號(hào)則跳出
  58.         if(timer<=250)
  59.         {
  60.                 TR0=1; //開啟計(jì)數(shù)
  61.                 while(ECHO); //當(dāng)RX為1計(jì)數(shù)并等待
  62.                 TR0=0; //關(guān)閉計(jì)數(shù)
  63.                 time=TH0*256+TL0; //讀取脈寬長(zhǎng)度
  64.                 TH0=0;
  65.                 TL0=0;
  66.                 S=(time*1.7)/100; //算出來是CM
  67.         }
  68.         else
  69.                 S=400;               
  70. }
  71. /************************************************************************/
  72. //前進(jìn)
  73. void run(void)
  74. {
  75.         pwm_leftdianji();
  76.         Left_moto_go ; //左電機(jī)往前走
  77.         Right_moto_go ; //右電機(jī)往前走

  78. }
  79. /************************************************************************/
  80. //后退
  81. void backrun(void)
  82. {
  83.         pwm_leftdianji();
  84.         Left_moto_back; //左電機(jī)往后
  85.         Right_moto_back; //右電機(jī)往后
  86. }
  87. /************************************************************************/
  88. //左轉(zhuǎn)y
  89. void leftrun(void)
  90. {
  91.         pwm_leftdianji();
  92.         Left_moto_Stop; //左電機(jī)停止
  93.         Right_moto_go ; //右電機(jī)往前走
  94. }
  95. /************************************************************************/
  96. //右轉(zhuǎn)
  97. void rightrun(void)
  98. {
  99.         pwm_leftdianji();
  100.         Right_moto_Stop ; //右電機(jī)停止
  101.         Left_moto_go ; //左電機(jī)往前       
  102. }
  103. /************************************************************************/
  104. //STOP
  105. void stoprun(void)
  106. {
  107.         pwm_leftdianji();
  108.         Left_moto_Stop ; //左電機(jī)停走
  109.         Right_moto_Stop ; //右電機(jī)停走
  110. }
  111. /************************************************************************/
  112. void COMM( void )  //
  113. {
  114.         push_val_left=5; //舵機(jī)向左轉(zhuǎn)45度
  115.         timer=0;
  116.         while(timer<=3000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
  117.         timer=0;
  118.         StartModule(); //啟動(dòng)超聲波測(cè)距
  119.         Conut(); //計(jì)算距離
  120.         S2=S;
  121.         push_val_left=19; //舵機(jī)向右轉(zhuǎn)45度
  122.         timer=0;
  123.         while(timer<=3000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
  124.         timer=0;
  125.         StartModule(); //啟動(dòng)超聲波測(cè)距
  126.         Conut(); //計(jì)算距離
  127.         S4=S;
  128.         push_val_left=12; //舵機(jī)歸中
  129.         timer=0;
  130.         while(timer<=3000); //延時(shí)400MS讓舵機(jī)轉(zhuǎn)到其位置
  131.         timer=0;
  132.         StartModule(); //啟動(dòng)超聲波測(cè)距
  133.         Conut(); //計(jì)算距離
  134.         S1=S;
  135.         if((S2<20)||(S4<20)) //只要左右各有距離小于20CM小車后退
  136.         {
  137.                 backrun(); //后退
  138.                 timer=0;
  139.                 while(timer<=4000);
  140.                 timer=0;
  141.         }
  142.         /*******************************************/
  143.         if(S2>S4)   //判斷方向
  144.         {
  145.                 rightrun(); //車的左邊比車的右邊距離小 右轉(zhuǎn)
  146.                 timer=0;
  147.                 while(timer<=4000);
  148.                 timer=0;
  149.         }
  150.         else
  151.         {
  152.             leftrun(); //車的左邊比車的右邊距離大 左轉(zhuǎn)
  153.             timer=0;
  154.             while(timer<=4000);
  155.                 timer=0;
  156.         }
  157. }
  158. /************************************************************************/
  159. /* PWM調(diào)制電機(jī)轉(zhuǎn)速 */
  160. /************************************************************************/
  161. /* 左電機(jī)調(diào)速 */
  162. /*調(diào)節(jié)push_val_left的值改變電機(jī)轉(zhuǎn)速,占空比 */
  163. void pwm_Servomoto(void)
  164. {
  165. if(pwm_val_left<=push_val_left)
  166.   Sevro_moto_pwm=1;
  167. else
  168.   Sevro_moto_pwm=0;
  169. if(pwm_val_left>=200)
  170.    pwm_val_left=0;
  171. }
  172. /***************************************************/
  173. ///*TIMER中斷服務(wù)子函數(shù)產(chǎn)生PWM信號(hào)*/
  174. void time1()interrupt 3 using 2
  175. {
  176.         TH1=(65536-100)/256; //100US定時(shí)
  177.         TL1=(65536-100)%256;
  178.         timer++; //定時(shí)器100US為準(zhǔn)。在這個(gè)基礎(chǔ)上延時(shí)
  179.         pwm_val_left++;
  180.         pwm_Servomoto();
  181. //timer1++; //2MS掃一次數(shù)碼管
  182. //if(timer1>=20)
  183. //{
  184. //timer1=0;
  185. //Display();
  186. //}
  187. }
  188. /***************************************************/
  189. ///*TIME中斷服務(wù)子函數(shù)產(chǎn)生PWM信號(hào)*/
  190. void time0()interrupt 1 using 0
  191. {

  192. } /***************************************************/
  193. void main(void)
  194. {
  195. TMOD=0X11;
  196. TH1=(65536-100)/256; //100US定時(shí)
  197. TL1=(65536-100)%256;
  198. TR1=1;
  199. ET1=1;
  200. ET0=1;
  201. EA =1;
  202. delay(100);
  203. push_val_left=12; //舵機(jī)歸中

  204. while(1) /*無限循環(huán)*/
  205. {
  206.         if(timer>=65) //80MS檢測(cè)啟動(dòng)檢測(cè)一次
  207.         {
  208.                 timer=0;
  209.                 StartModule(); //啟動(dòng)檢測(cè)
  210.                 Conut(); //計(jì)算距離             
  211.                 if(S<20) //距離小于20CM
  212.                 {
  213.                         stoprun(); //小車停止
  214.                         COMM(); //方向函數(shù)
  215.                 }                            
  216.                 else
  217.                         run();
  218.         }
  219. }
  220. }
復(fù)制代碼



作者: 13852833337    時(shí)間: 2017-6-20 06:41
謝謝分享,先下載試試




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1