返回列表 发新帖我要提问本帖赏金: 10.00元(功能说明)

[STM32G4] SR04超声波模块测距

[复制链接]
2960|16
 楼主| gaoyang9992006 发表于 2022-2-11 15:38 | 显示全部楼层 |阅读模式
本帖最后由 gaoyang9992006 于 2022-2-11 15:44 编辑

#申请原创# @21小跑堂

为了提高测量精度采用平均值滤波经过多次测量提高测量精度。我使用STM32 NUCLEO-G431RB开发板做的,这里使用cubeMX选择该开发板配置

5133462060bf72984a.png

为了省事,我通过串口打印测量结果,板载默认的LPUART1
7993862060cbcb13d5.png
系统时钟配置为16MHz内部RC振荡器即可
2057162060d24623f6.png
根据时钟设置,定时器使用TIM2,分频为15+1,即可实现计数一次为1us,方便计算。
最大计数可以根据你的需要选择,我这里设置的是50ms的技术周期。这足够测量15米以内的距离了。
4994062060cf7920e1.png

为了使用串口printf,我直接复制cubeG4开发包里的例子中的内容
  1. #ifdef __GNUC__
  2. /* With GCC, small printf (option LD Linker->Libraries->Small printf
  3.    set to 'Yes') calls __io_putchar() */
  4. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  5. #else
  6. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  7. #endif /* __GNUC__ */
  1. PUTCHAR_PROTOTYPE
  2. {
  3.   /* Place your implementation of fputc here */
  4.   /* e.g. write a character to the LPUART1 and Loop until the end of transmission */
  5.   HAL_UART_Transmit(&hlpuart1, (uint8_t *)&ch, 1, 0xFFFF);

  6.   return ch;
  7. }
以实现串口printf函数的映射,同时在工程配置中启用MicroLIB即可。
头文件当然要引用一下stdio.h
为了方便代码阅读,对TRIG触发引脚的高低电平设置了如下宏定义。
  1. #include "stdio.h"
  2. #define TRIG_HIGH   HAL_GPIO_WritePin(TRIG_GPIO_Port,TRIG_Pin,GPIO_PIN_SET)
  3. #define TRIG_LOW    HAL_GPIO_WritePin(TRIG_GPIO_Port,TRIG_Pin,GPIO_PIN_RESET)
并声明了几个变量用于存储临时数据
  1. float t1,t2,temp[5],average,distance;
  2. int i=0;
每个周期的测量会产生两个计数器的值t1,t2,t2-t1时间差就是模块反馈回来的高电平时长,存储到temp数组内,
测完几组数据后计算出时间差的平均值,然后计算长度。循环临时变量为i.
  1.   for(i=0;i<5;)
  2.                 {
  3.                         htim2.Instance->CNT = 0;
  4.                         TRIG_LOW;
  5.                         TRIG_HIGH;
  6.                         HAL_Delay(1);
  7.                         TRIG_LOW;
  8.                         
  9.                         while(HAL_GPIO_ReadPin(ECHO_GPIO_Port,ECHO_Pin)==GPIO_PIN_RESET)
  10.                                 t1=htim2.Instance->CNT;
  11.                         while(HAL_GPIO_ReadPin(ECHO_GPIO_Port,ECHO_Pin)==GPIO_PIN_SET)
  12.                                 t2=htim2.Instance->CNT;               
  13.                         htim2.Instance->CNT = 0;
  14.                         if(t2>=t1)
  15.                                 {
  16.                                         temp[i]=t2-t1;
  17.                                         i++;
  18.                                 }
  19.                         HAL_Delay(50);
  20.                 }
  21.                
  22.                 average=0;
  23.                
  24.                 for(i=0;i<5;i++)
  25.                         average=average+temp[i];
  26.                         average=average/5.0;
  27.                
  28.                 distance = average*17/1000;
  29.                 printf("Distance=%.1f cm\n",distance);               
44546620610d8559de.png
经过测试,不采用平均值,也是非常准的
唯一的缺点是声波集束度不够好,呈现扇面状,模块传感器与被测物体之间要没有其他障碍物影响,如果没有这些干扰,精度还是非常高的。单次测量即输出数据的完整代码。
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * [url=home.php?mod=space&uid=288409]@file[/url]           : main.c
  5.   * [url=home.php?mod=space&uid=247401]@brief[/url]          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>© Copyright (c) 2022 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"

  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. #include "stdio.h"
  25. #define TRIG_HIGH   HAL_GPIO_WritePin(TRIG_GPIO_Port,TRIG_Pin,GPIO_PIN_SET)
  26. #define TRIG_LOW    HAL_GPIO_WritePin(TRIG_GPIO_Port,TRIG_Pin,GPIO_PIN_RESET)

  27. /* USER CODE END Includes */

  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30. float t1,t2,temp,distance;
  31. int i=0;
  32. /* USER CODE END PTD */

  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35. /* USER CODE END PD */

  36. /* Private macro -------------------------------------------------------------*/
  37. /* USER CODE BEGIN PM */

  38. /* USER CODE END PM */

  39. /* Private variables ---------------------------------------------------------*/
  40. UART_HandleTypeDef hlpuart1;

  41. TIM_HandleTypeDef htim2;

  42. /* USER CODE BEGIN PV */

  43. /* USER CODE END PV */

  44. /* Private function prototypes -----------------------------------------------*/
  45. void SystemClock_Config(void);
  46. static void MX_GPIO_Init(void);
  47. static void MX_LPUART1_UART_Init(void);
  48. static void MX_TIM2_Init(void);
  49. /* USER CODE BEGIN PFP */
  50. #ifdef __GNUC__
  51. /* With GCC, small printf (option LD Linker->Libraries->Small printf
  52.    set to 'Yes') calls __io_putchar() */
  53. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  54. #else
  55. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  56. #endif /* __GNUC__ */
  57. /* USER CODE END PFP */

  58. /* Private user code ---------------------------------------------------------*/
  59. /* USER CODE BEGIN 0 */

  60. /* USER CODE END 0 */

  61. /**
  62.   * [url=home.php?mod=space&uid=247401]@brief[/url]  The application entry point.
  63.   * @retval int
  64.   */
  65. int main(void)
  66. {
  67.   /* USER CODE BEGIN 1 */

  68.   /* USER CODE END 1 */

  69.   /* MCU Configuration--------------------------------------------------------*/

  70.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  71.   HAL_Init();

  72.   /* USER CODE BEGIN Init */

  73.   /* USER CODE END Init */

  74.   /* Configure the system clock */
  75.   SystemClock_Config();

  76.   /* USER CODE BEGIN SysInit */

  77.   /* USER CODE END SysInit */

  78.   /* Initialize all configured peripherals */
  79.   MX_GPIO_Init();
  80.   MX_LPUART1_UART_Init();
  81.   MX_TIM2_Init();
  82.   /* USER CODE BEGIN 2 */
  83.           if (HAL_UART_Transmit(&hlpuart1, (uint8_t *)"Hello\n", 6, 3000) != HAL_OK)
  84.   {
  85.     Error_Handler();
  86.   }
  87.         
  88.         HAL_TIM_Base_Start(&htim2);

  89.   /* USER CODE END 2 */

  90.   /* Infinite loop */
  91.   /* USER CODE BEGIN WHILE */
  92.   while (1)
  93.   {


  94.                         htim2.Instance->CNT = 0;
  95.                         TRIG_LOW;
  96.                         TRIG_HIGH;
  97.                         HAL_Delay(1);
  98.                         TRIG_LOW;
  99.                         
  100.                         while(HAL_GPIO_ReadPin(ECHO_GPIO_Port,ECHO_Pin)==GPIO_PIN_RESET)
  101.                                 t1=htim2.Instance->CNT;
  102.                         while(HAL_GPIO_ReadPin(ECHO_GPIO_Port,ECHO_Pin)==GPIO_PIN_SET)
  103.                                 t2=htim2.Instance->CNT;               
  104.                         htim2.Instance->CNT = 0;
  105.                         if(t2>=t1)
  106.                                 {
  107.                                         temp=t2-t1;
  108.                                 }
  109.                         HAL_Delay(100);
  110.                
  111.                 distance = temp*17/1000;
  112.                 printf("Distance=%.1f cm\n",distance);               
  113.                
  114.                 HAL_Delay(2000);
  115.     /* USER CODE END WHILE */

  116.     /* USER CODE BEGIN 3 */
  117.   }
  118.   /* USER CODE END 3 */
  119. }

  120. /**
  121.   * @brief System Clock Configuration
  122.   * @retval None
  123.   */
  124. void SystemClock_Config(void)
  125. {
  126.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  127.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  128.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  129.   /** Configure the main internal regulator output voltage
  130.   */
  131.   HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  132.   /** Initializes the RCC Oscillators according to the specified parameters
  133.   * in the RCC_OscInitTypeDef structure.
  134.   */
  135.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  136.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  137.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  138.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  139.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  140.   {
  141.     Error_Handler();
  142.   }
  143.   /** Initializes the CPU, AHB and APB buses clocks
  144.   */
  145.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  146.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  147.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  148.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  149.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  150.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  151.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  152.   {
  153.     Error_Handler();
  154.   }
  155.   /** Initializes the peripherals clocks
  156.   */
  157.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
  158.   PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  159.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  160.   {
  161.     Error_Handler();
  162.   }
  163. }

  164. /**
  165.   * @brief LPUART1 Initialization Function
  166.   * @param None
  167.   * @retval None
  168.   */
  169. static void MX_LPUART1_UART_Init(void)
  170. {

  171.   /* USER CODE BEGIN LPUART1_Init 0 */

  172.   /* USER CODE END LPUART1_Init 0 */

  173.   /* USER CODE BEGIN LPUART1_Init 1 */

  174.   /* USER CODE END LPUART1_Init 1 */
  175.   hlpuart1.Instance = LPUART1;
  176.   hlpuart1.Init.BaudRate = 115200;
  177.   hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
  178.   hlpuart1.Init.StopBits = UART_STOPBITS_1;
  179.   hlpuart1.Init.Parity = UART_PARITY_NONE;
  180.   hlpuart1.Init.Mode = UART_MODE_TX_RX;
  181.   hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  182.   hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  183.   hlpuart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  184.   hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  185.   if (HAL_UART_Init(&hlpuart1) != HAL_OK)
  186.   {
  187.     Error_Handler();
  188.   }
  189.   if (HAL_UARTEx_SetTxFifoThreshold(&hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  190.   {
  191.     Error_Handler();
  192.   }
  193.   if (HAL_UARTEx_SetRxFifoThreshold(&hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  194.   {
  195.     Error_Handler();
  196.   }
  197.   if (HAL_UARTEx_DisableFifoMode(&hlpuart1) != HAL_OK)
  198.   {
  199.     Error_Handler();
  200.   }
  201.   /* USER CODE BEGIN LPUART1_Init 2 */

  202.   /* USER CODE END LPUART1_Init 2 */

  203. }

  204. /**
  205.   * @brief TIM2 Initialization Function
  206.   * @param None
  207.   * @retval None
  208.   */
  209. static void MX_TIM2_Init(void)
  210. {

  211.   /* USER CODE BEGIN TIM2_Init 0 */

  212.   /* USER CODE END TIM2_Init 0 */

  213.   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  214.   TIM_MasterConfigTypeDef sMasterConfig = {0};

  215.   /* USER CODE BEGIN TIM2_Init 1 */

  216.   /* USER CODE END TIM2_Init 1 */
  217.   htim2.Instance = TIM2;
  218.   htim2.Init.Prescaler = 15;
  219.   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  220.   htim2.Init.Period = 49999;
  221.   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  222.   htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  223.   if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  224.   {
  225.     Error_Handler();
  226.   }
  227.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  228.   if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  229.   {
  230.     Error_Handler();
  231.   }
  232.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  233.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  234.   if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  235.   {
  236.     Error_Handler();
  237.   }
  238.   /* USER CODE BEGIN TIM2_Init 2 */

  239.   /* USER CODE END TIM2_Init 2 */

  240. }

  241. /**
  242.   * @brief GPIO Initialization Function
  243.   * @param None
  244.   * @retval None
  245.   */
  246. static void MX_GPIO_Init(void)
  247. {
  248.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  249.   /* GPIO Ports Clock Enable */
  250.   __HAL_RCC_GPIOC_CLK_ENABLE();
  251.   __HAL_RCC_GPIOF_CLK_ENABLE();
  252.   __HAL_RCC_GPIOA_CLK_ENABLE();
  253.   __HAL_RCC_GPIOB_CLK_ENABLE();

  254.   /*Configure GPIO pin Output Level */
  255.   HAL_GPIO_WritePin(TRIG_GPIO_Port, TRIG_Pin, GPIO_PIN_RESET);

  256.   /*Configure GPIO pin Output Level */
  257.   HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);

  258.   /*Configure GPIO pin : B1_Pin */
  259.   GPIO_InitStruct.Pin = B1_Pin;
  260.   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  261.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  262.   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

  263.   /*Configure GPIO pin : TRIG_Pin */
  264.   GPIO_InitStruct.Pin = TRIG_Pin;
  265.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  266.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  267.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  268.   HAL_GPIO_Init(TRIG_GPIO_Port, &GPIO_InitStruct);

  269.   /*Configure GPIO pin : ECHO_Pin */
  270.   GPIO_InitStruct.Pin = ECHO_Pin;
  271.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  272.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  273.   HAL_GPIO_Init(ECHO_GPIO_Port, &GPIO_InitStruct);

  274.   /*Configure GPIO pin : LD2_Pin */
  275.   GPIO_InitStruct.Pin = LD2_Pin;
  276.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  277.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  278.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  279.   HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);

  280. }

  281. /* USER CODE BEGIN 4 */
  282. /**
  283.   * @brief  Retargets the C library printf function to the USART.
  284.   * @param  None
  285.   * @retval None
  286.   */
  287. PUTCHAR_PROTOTYPE
  288. {
  289.   /* Place your implementation of fputc here */
  290.   /* e.g. write a character to the LPUART1 and Loop until the end of transmission */
  291.   HAL_UART_Transmit(&hlpuart1, (uint8_t *)&ch, 1, 0xFFFF);

  292.   return ch;
  293. }
  294. /* USER CODE END 4 */

  295. /**
  296.   * @brief  This function is executed in case of error occurrence.
  297.   * @retval None
  298.   */
  299. void Error_Handler(void)
  300. {
  301.   /* USER CODE BEGIN Error_Handler_Debug */
  302.   /* User can add his own implementation to report the HAL error return state */
  303.   __disable_irq();
  304.   while (1)
  305.   {
  306.   }
  307.   /* USER CODE END Error_Handler_Debug */
  308. }

  309. #ifdef  USE_FULL_ASSERT
  310. /**
  311.   * @brief  Reports the name of the source file and the source line number
  312.   *         where the assert_param error has occurred.
  313.   * @param  file: pointer to the source file name
  314.   * @param  line: assert_param error line source number
  315.   * @retval None
  316.   */
  317. void assert_failed(uint8_t *file, uint32_t line)
  318. {
  319.   /* USER CODE BEGIN 6 */
  320.   /* User can add his own implementation to report the file name and line number,
  321.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  322.   /* USER CODE END 6 */
  323. }
  324. #endif /* USE_FULL_ASSERT */

  325. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

139620613c3afeb8.png   

打赏榜单

21小跑堂 打赏了 10.00 元 2022-02-11
理由:恭喜通过原创文章审核!请多多加油哦!

koala889 发表于 2022-2-14 09:52 | 显示全部楼层
感觉你们cube都用的好熟练啊
yljon 发表于 2022-2-14 10:56 | 显示全部楼层
不错啊
内政奇才 发表于 2022-2-14 11:00 来自手机 | 显示全部楼层
大神又创新作,新作
 楼主| gaoyang9992006 发表于 2022-2-14 20:52 | 显示全部楼层
koala889 发表于 2022-2-14 09:52
感觉你们cube都用的好熟练啊

我也是从标准库转来的,强迫自己用几次熟悉了,就觉得cube好用了。
 楼主| gaoyang9992006 发表于 2022-2-14 20:53 | 显示全部楼层

哈哈,谢谢捧场。
 楼主| gaoyang9992006 发表于 2022-2-14 20:53 | 显示全部楼层
内政奇才 发表于 2022-2-14 11:00
大神又创新作,新作

手里有个模块,就测了一下试试。然后发帖分享一下经验。
qcliu 发表于 2022-3-8 16:52 | 显示全部楼层
什么控制模式啊
aoyi 发表于 2022-3-8 16:59 | 显示全部楼层
响应速度快不快
zljiu 发表于 2022-3-8 17:07 | 显示全部楼层
精度取决于超声波模块本身是吗
nawu 发表于 2022-3-8 17:15 | 显示全部楼层
数字通讯还是采集的模拟量啊
tfqi 发表于 2022-3-8 17:22 | 显示全部楼层
开发的时候干扰厉害吗
janewood 发表于 2022-4-1 12:47 | 显示全部楼层
最大测距范围是多少?
tabmone 发表于 2022-4-11 17:13 | 显示全部楼层
没有超时判断吗?
 楼主| gaoyang9992006 发表于 2022-4-11 18:32 | 显示全部楼层
tabmone 发表于 2022-4-11 17:13
没有超时判断吗?

简单的演示一下,实际使用肯定要的。另外还可以设置检测是否设备在线。
modesty3jonah 发表于 2022-4-11 19:43 | 显示全部楼层
这个测距精度怎么样
uiint 发表于 2022-4-25 14:38 | 显示全部楼层
超声波模块还是比较基础的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:如果你觉得我的分享或者答复还可以,请给我点赞,谢谢。

2052

主题

16403

帖子

222

粉丝
快速回复 在线客服 返回列表 返回顶部