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

QQ登錄

只需一步,快速開始

帖子
查看: 3456|回復(fù): 2
收起左側(cè)

PWM調(diào)速+超聲波控制車距的小車stm32源碼

[復(fù)制鏈接]
ID:321034 發(fā)表于 2018-7-24 16:28 | 顯示全部樓層 |閱讀模式
STM32f103EZ  系列  
超聲波 通過定時(shí)器來測(cè)量,PWM調(diào)速 控制車速   小車可以實(shí)現(xiàn)設(shè)定距離之間的 運(yùn)動(dòng)  ,實(shí)現(xiàn)跟隨運(yùn)動(dòng)
具體的 源程序 如下 :
  1. #include "stm32f10x.h"
  2. #include "bsp_usart1.h"
  3. #include "bsp_length.h"
  4. #include "bsp_tick.h"
  5. # include "motor.h"
  6. # include "pwm_motor.h"

  7. #define CLI()      __set_PRIMASK(1)                /* 關(guān)閉總中斷 */  
  8. #define SEI()      __set_PRIMASK(0)                /*開總中斷*/

  9. void Delay(unsigned short int time);  //粗略的延時(shí)函數(shù)
  10. float ChangeDistance(unsigned int cout1);  //時(shí)間轉(zhuǎn)為距離函數(shù)

  11. //ECHO PA4
  12. //TRIG PA5
  13. int main(void)
  14. {
  15.           unsigned int count=0; //  計(jì)數(shù)
  16.           float distance=0;     //  距離
  17.          
  18.                 /* USART1 config 115200 8-N-1 */
  19.                 USART1_Config();
  20.     Distance_Config();  //測(cè)距模塊對(duì)應(yīng)的引腳初始化
  21.     CLI() ;//關(guān)閉總中斷
  22.           SEI(); //開總中斷
  23.           Tim3_Config();  //定時(shí)器的初始化
  24.           Motor_Config();
  25.     TIM_Mode_Config();   //PWM 調(diào)速
  26.          
  27.           GPIO_ResetBits(GPIOA,GPIO_Pin_5);  //先拉低電平
  28.         
  29.           while(1)
  30.                 {
  31.                           printf(" 超聲波初始化完成�。� \r \n");
  32.         printf(" 測(cè)距開始 \r \n");
  33.                           GPIO_SetBits(GPIOA,GPIO_Pin_5);  //拉高電平
  34.                           Delay(30);  //延時(shí)20個(gè)微秒
  35.                         //  Delay(20);
  36.                         //  Delay(20);
  37.                          GPIO_ResetBits(GPIOA,GPIO_Pin_5); //拉低電平

  38.        TIM3->CNT=0;  //TIM3的計(jì)數(shù)器清0

  39.        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 0); //等待ECHO的高電平
  40.                

  41.        TIM_Cmd(TIM3,ENABLE);  //運(yùn)行TIM3進(jìn)行計(jì)時(shí)
  42.       
  43.                         while((GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 1) && (TIM3->CNT < TIM3->ARR-10))
  44.                         {
  45.                         
  46.                           TIM_Cmd(TIM3,DISABLE);  
  47.                        
  48.         count++;
  49.                         }
  50.         printf("count=%d",count);                        
  51.                           distance=ChangeDistance(count);
  52.                           printf(" 當(dāng)前距離為:%f\n",distance);
  53.                           Delay(20000);
  54.                                 Delay(20000);         
  55.                         
  56.                                         if(distance>10)
  57.                                                  Go_forward();
  58.                                         else
  59.                                                 Go_back();
  60.                                         while(1);
  61.                                 
  62.                         
  63.      }
  64. }

  65. void Delay(unsigned short int time)  //粗略的延時(shí)函數(shù)
  66. {
  67.     unsigned char i=0;
  68.           while(time--)
  69.                 {
  70.         i=10;
  71.                           while(i--);
  72.     }
  73. }

  74. float ChangeDistance(unsigned int cout1)
  75. {
  76.     float distance=0;
  77.           printf("cou1=%d\n",cout1);
  78.           distance=cout1/58.0;
  79.         
  80.           return distance;
  81. }
  82. /*********************************************END OF FILE**********************/
復(fù)制代碼

全部資料51hei下載地址:
超聲波+PWM調(diào)速 小車.rar (318.71 KB, 下載次數(shù): 45)


評(píng)分

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

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:383531 發(fā)表于 2018-8-6 17:50 | 顯示全部樓層
你好,能否詳細(xì)說一下超聲波測(cè)距的具體作用   
回復(fù)

使用道具 舉報(bào)

ID:267330 發(fā)表于 2018-10-15 10:32 | 顯示全部樓層
你好,能否詳細(xì)說一下超聲波測(cè)距的具體作用   
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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