找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

基于stm32f103c8t6藍牙遙控小車可測距避障程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:957064 發(fā)表于 2021-7-28 13:35 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
STM32單片機源程序如下:
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "key.h"
  4. #include "sys.h"
  5. #include "usart.h"
  6. #include "timer.h"
  7. #include "motor.h"
  8. #include "sg90.h"
  9. #include "HCSR.h"
  10. #include "oled.h"       
  11. #include "beep.h"
  12. #include "motorpwm.h"
  13. #include "TCRT.h"
  14. #define motor1_PWM     TIM1->CCR1               

  15. #define motor3_PWM     TIM1->CCR3               
  16.         #define motor2_PWM     TIM1->CCR2               
  17.        
  18. #define motor4_PWM     TIM1->CCR4       
  19. // motor11=1; PA7
  20. //                motor12=0; PA6
  21. //               
  22. //                motor21=1; PB7
  23. //                motor21=0; PB6


  24. extern u8 res;       
  25.        
  26. int main(void)
  27. {                                
  28.          int len;
  29.                  
  30.         delay_init();                     //延時函數(shù)初始化          
  31.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);          //設(shè)置NVIC中斷分組2:2位搶占優(yōu)先級,2位響應(yīng)優(yōu)先級
  32.         uart_init(9600);         //串口初始化為9600
  33.         My_USART2_Init();  
  34.           OLED_Init();
  35.          OLED_Clear();
  36.          motor_pwm_init();
  37.          Motor_Init();
  38.         TIM3_PWM_Init(199,7199);         //不分頻。T為20ms,這樣的配置就是為了讓輸出的PWM信號達到舵機要求的20ms周期。
  39.         SG90_pwm_init();
  40.           TCRT_config(); //循跡模塊初始化
  41.          CH_SR04_Init();
  42.          motor1_PWM=0;motor3_PWM =0;motor21=0; motor22=0;
  43. //         motor1_PWM=10;motor2_PWM=10;motor3_PWM =10;motor4_PWM =10;
  44.          beep_Init();
  45.            while(1)
  46.         {
  47.                  len =Senor_Using();
  48. //     printf("距離為:%dcm\n",length);   
  49.                  OLED_ShowCHinese(0,0,0);//前
  50.                 OLED_ShowCHinese(18,0,1);//方
  51.                 OLED_ShowCHinese(36,0,2);//距
  52.                 OLED_ShowCHinese(54,0,3);//離
  53.                 OLED_ShowCHinese(72,0,4);//為
  54.                  OLED_Float(3,3,len,1);   
  55.      delay_ms(20);                   
  56. //                  if(res=='1'){motor11=1; motor12=0;motor21=1; motor22=0;TIM_SetCompare2(TIM3,175);}//前進右轉(zhuǎn)90
  57. if(res=='1')//避障
  58. {
  59.        
  60. float  length_res[5];  //用來存放測距結(jié)果
  61.                  len =Senor_Using();
  62. //     printf("距離為:%dcm\n",length);   
  63.                  OLED_ShowCHinese(0,0,0);//前
  64.                 OLED_ShowCHinese(18,0,1);//方
  65.                 OLED_ShowCHinese(36,0,2);//距
  66.                 OLED_ShowCHinese(54,0,3);//離
  67.                 OLED_ShowCHinese(72,0,4);//為
  68.                  OLED_Float(3,3,len,1);   
  69.      delay_ms(20);  

  70.                 SG90_Front;    //舵機擺正
  71.                 delay_ms(100);
  72.     length_res[0] =Senor_Using();  //測前方距離放在數(shù)組里
  73.                 delay_ms(100);
  74.                        
  75.                
  76.                 if(length_res[0]>40.00)       //如果前方距離大于30cm  前進
  77.                         {
  78.                                 motor1_PWM=70;motor3_PWM =70;        motor21=0; motor22=0;  
  79.                           SG90_Front;               
  80. TIM_SetCompare2(TIM3,185);                                //方向回中  

  81.                                
  82.                                 }
  83.                                
  84.                 if(length_res[0]<40.00)     //如果前方距離小于30厘米  停車測左右距離
  85.                         {
  86.                         beep=1;
  87.                        
  88.                                  motor1_PWM=0;motor3_PWM =0;motor21=0; motor22=0;  
  89.                                
  90.                           SG90_Left_45;   //舵機左轉(zhuǎn)45度測距
  91.                           delay_ms(700);beep=0;               
  92.                           length_res[1] =Senor_Using();    //把測量結(jié)果放進數(shù)組
  93.         SG90_Right_45;     //舵機右轉(zhuǎn)45度測距
  94.                           delay_ms(700);       
  95.         length_res[4] =Senor_Using();     //把測量結(jié)果放進數(shù)組                               
  96.                                
  97.                           SG90_Front;          //舵機擺正
  98.                           delay_ms(100);
  99.                 if(length_res[1]>length_res[4])    //如果左邊的距離大于右邊的距離
  100.                                 {
  101.                                         do                        //舵機擺正
  102.                                         {
  103.                                         SG90_Front;
  104.                                         delay_ms(10);
  105.                                         length_res[0] =Senor_Using();        //重復(fù)測前方的距離同時左轉(zhuǎn)
  106.                                         delay_ms(10);                                               
  107.                                         TIM_SetCompare2(TIM3,195);
  108.                                         motor1_PWM=75;motor3_PWM =75;          motor21=0; motor22=0;                                                 
  109.                                         }
  110.                                         while(length_res[0]<40.00);                //一直轉(zhuǎn)到前方距離大于30cm               
  111.                                 }
  112.                    if(length_res[1]<length_res[4])    //如果右邊的距離大于左邊的距離
  113.                                 {
  114.                                         do
  115.                                         {
  116.                                         SG90_Front;
  117.                                         delay_ms(10);
  118.                                         length_res[0] =Senor_Using();  
  119.                                         delay_ms(10);                                               
  120.                                         TIM_SetCompare2(TIM3,175);
  121.                                         motor1_PWM=75;motor3_PWM =75;         motor21=0; motor22=0;                                                  
  122.                                         }
  123.                                         while(length_res[0]<40.00);                //一直轉(zhuǎn)到前方距離大于30cm
  124.                                 }
  125.                                
  126.                         }               
  127.        
  128.        

  129. }
  130.                         if(res=='2')
  131.         {
  132.                                 if(Senor_Using()<30)
  133.                                 {        beep=1;
  134.         delay_ms(500);
  135.         beep=0;motor1_PWM=0;motor3_PWM =0;motor21=0; motor22=0;delay_ms(300);}motor1_PWM=75;motor3_PWM =65;motor21=0; motor22=0;  TIM_SetCompare2(TIM3,180);}//前進右轉(zhuǎn)45
  136.                         if(res=='3')
  137. {
  138.         if(Senor_Using()<30)
  139.         {beep=1;
  140.         delay_ms(500);
  141.         beep=0;motor1_PWM=0;motor3_PWM =0;motor21=0; motor22=0;delay_ms(600);
  142.         }
  143. motor1_PWM=75;motor3_PWM =75;motor21=0; motor22=0;  TIM_SetCompare2(TIM3,185);}//直行
  144.                         if(res=='4')
  145. {
  146.         if(Senor_Using()<30)
  147.                 {beep=1;
  148.         delay_ms(500);
  149.         beep=0;motor1_PWM=0;motor3_PWM =0;motor21=0; motor22=0;delay_ms(300);}motor1_PWM=65;motor3_PWM =75;motor21=0; motor22=0;  TIM_SetCompare2(TIM3,190);}//前進左轉(zhuǎn)45
  150. //                        if(res=='5'){motor11=1; motor12=0;motor21=1; motor22=0;TIM_SetCompare2(TIM3,195);}//前進左轉(zhuǎn)90
  151.                         if(res=='0'){TIM_SetCompare2(TIM3,185);motor1_PWM=0;motor3_PWM =0;;motor21=0; motor22=0;}
  152.                         if(res=='6'){motor1_PWM=30;motor3_PWM =30;motor21=1; motor22=1;TIM_SetCompare2(TIM3,185);}//后退
  153.                         if(res=='7'){motor1_PWM=30;motor3_PWM =30;motor21=1; motor22=1;TIM_SetCompare2(TIM3,180);}//后退右轉(zhuǎn)45
  154.                         if(res=='8'){motor1_PWM=30;motor3_PWM =30;motor21=1; motor22=1;TIM_SetCompare2(TIM3,190);}//后退左轉(zhuǎn)45

  155.                         if(res=='5')
  156.         {  
  157.        
  158.                                  Read_TCRT_Date(); //讀循跡線值
  159.                 TIM_SetCompare2(TIM3,185);                        //回中
  160.    
  161.          if(L==1&&M==1&&R==1)//11
  162.           {
  163.              motor1_PWM=75;motor3_PWM =75;motor21=0; motor22=0;          //如果都沒有讀取到黑線,直走
  164.                 TIM_SetCompare2(TIM3,0);
  165.             delay_ms(10);
  166.           }
  167.          if(L==1&&M==0&&R==1)//101
  168.           {
  169.           motor1_PWM=75;motor3_PWM =75;motor21=0; motor22=0;       //如果中間一個讀取到黑線,直走
  170.                   TIM_SetCompare2(TIM3,185);                //回中
  171.             delay_ms(10);
  172.           }          
  173.          if(L==1&&M==1&&R==0)//110
  174.           {
  175.                         motor1_PWM=75;motor3_PWM =75;motor21=0; motor22=0;        //直走
  176.         TIM_SetCompare2(TIM3,179); //如果右邊讀取到黑線,右轉(zhuǎn)
  177.             delay_ms(10);
  178.           }

  179.           if(L==0&&M==1&&R==1)//011
  180.           {
  181.                         motor1_PWM=75;motor3_PWM =75;motor21=0; motor22=0;        //直走
  182.                  TIM_SetCompare2(TIM3,191); //如果左邊讀取到黑線,左轉(zhuǎn)
  183.              delay_ms(10);
  184.           }
  185.                 if(L==0&&M==0&&R==0)//000
  186.           {
  187.             motor1_PWM=75;motor3_PWM =75;motor21=0; motor22=0;
  188.                          TIM_SetCompare2(TIM3,185);
  189.                        
  190.                 }               
  191.         }
  192.         }
  193.                                        
  194.                                                
  195.                 //在周期20ms的PWM信號中,不同的脈寬對應(yīng)舵機不同的轉(zhuǎn)動角度,在0.5ms-2.5ms間有效                                                                                  
  196.                 //        TIM_SetCompare2(TIM4,175);//總共計數(shù)200,前面設(shè)置的模式是計數(shù)在比較值之后才是高電平
  197.                 //也就是高電平的時間是[(200-175)/200]*20ms=2.5ms,即轉(zhuǎn)過360度,180度舵機依次類推                               
  198.         }         
復(fù)制代碼

所有程序51hei附件下載:
stm32利用藍牙控制舵機1.3.7z (215.34 KB, 下載次數(shù): 31)

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

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

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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