zoukankan      html  css  js  c++  java
  • HAL超声波测距

     1 extern TIM_HandleTypeDef htim7;
     2 /*
     3 ³¬Éù²¨²â¾à
     4    Trig ¿ØÖÆ PA2 out
     5      Echo ½ÓÊÕ PA3 in   Echo Ϊ¸ßµçƽʱ£¬Æô¶¯¶¨Ê±Æ÷   Echo ΪµÍµçƽ£¬¹Ø±Õ¶¨Ê±Æ÷
     6 */
     7   double  get_distance(){
     8     //¶¨Ê±Æ÷¹é0 È·±£´Ó0¿ªÊ¼¼ÆÊý
     9     __HAL_TIM_SET_COUNTER(&htim7,0);
    10 
    11     __HAL_TIM_SET_AUTORELOAD(&htim7,50000-1);
    12         
    13     Led_off_all();
    14     //³¬Éù²¨´¥·¢ÐźÅ
    15     HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,1);
    16     HAL_Delay(100);//Trig¸ßµçƽʱ¼ä´óÓÚ10΢Ãë
    17     HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,0);//
    18     
    19     while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_3)==0);//Åжϳ¬Éù²¨ÊÇ·ñ
    20         //·¢³öÉù²¨£¬Æô¶¯¶¨Ê±Æ÷
    21         HAL_TIM_Base_Start_IT(&htim7);
    22      
    23     while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_3)==1);//Åжϳ¬Éù²¨ÊÇ·ñ
    24         //½ÓÊÕÉù²¨£¬¹Ø±Õ¶¨Ê±Æ÷
    25         HAL_TIM_Base_Stop(&htim7);
    26      
    27     //¼ÆËã³ö³¬Éù²¨µÄ´«Êäʱ¼ä£¬ÒÔ¼°²âµÃµÄ¾àÀë
    28     
    29         double  times;   //³¬Éù²¨µÄ´«ÊäµÄʱ¼ä
    30         double distance;//³¬Éù²¨²âµÃµÄ¾àÀë
    31       double count;   //¶¨Ê±Æ÷¼ÆÊýµÄʱ¼ä
    32         count  = __HAL_TIM_GET_COUNTER(&htim7);//¼ÆÊýµÄ´ÎÊý
    33         printf("
    计数器计数的次数:%lf
    ",count);
    34         
    35         times = count*100;//¼ÆÊýµÄʱ¼ä = ¼ÆÊýµÄ´ÎÊý * ÿ¼Ó1µÄʱ¼ä
    36         
    37         distance = times * 340 / 1000000.0 * 100 / 2.0;
    38         
    39     return distance;
    40 }

    void main(){
      printf("距离:%.2lf",get_distance());
    }

    运行的结果

    归去来兮
  • 相关阅读:
    Mongodb 查询时间类型
    Python更新所有pip package
    Python selenium on windows
    Redis cluster setup on windows
    CAP定理(theorem)
    error: Microsoft Visual C++ 14.0 is required when installing python package
    using RNGCryptoServiceProvider to generate random string
    <map>的使用
    二维vector的使用
    vector做形参时的三种传参方式
  • 原文地址:https://www.cnblogs.com/zoute/p/9486691.html
Copyright © 2011-2022 走看看