一、原理
本次实验采用HC-SR04超声波传感器,结构及功能如下图
超声波传感器可以用来测距。距离 = 声速(340m/s) *传播时间 / 2。
这个传感器有四个引脚,其中VCC接电源正极,GND接电源负极 , Trig : 用来启动测量 ,Echo用来返回结果。
在开始测量之前,我们要给Trig引脚施加一个大于10微秒的脉冲,然后T开始发送超声波,整个过程大约持续0.2毫秒,声波发送结束后Echo引脚上会出现一个上升沿,然后当8个周期 的超声波全部接收到后,Echo引脚上又会出现一个下降沿。这样在Echo引脚上就形成了一个脉冲,我们测量脉冲的宽度就能得到超声波在空气中传播的时间。
二、STM32CubeMX配置
1:设置调试接口
2:设置PA0
3:设置定时器——通道一选择上升沿直接,通道二选择下降沿间接
4:配置板载LED:PC13引脚
接下来用几张图回顾一下定时器的工作过程
分辨率:
所以
5:设置定时器参数
6:保存并且打开工程
三、电路连接图
四、编程接口
HAL_StatusTypeDef HAL_TIM_IC_Start(TIM_HandleTypeDef *htim , uint32_t Channel)
作用 :启动输入捕获
HAL_StatusTypeDef HAL_TIM_IC_Stop(TIM_HandleTypeDef *htim , uint32_t Channel)
作用 :停止输入捕获
参数说明:htim :定时器句柄指针
Channel : 通道编号
五、编程思路
1:让CNT的值归0
2:清除CCR1和CCR2标志位
CNT溢出时,会触发update,当update发生的时候,单片机会自动让update标志位从0到1;所以我们只需要查询一下update的标志位就可以知道CNT有没有溢出。当我们把定时器通道设置为输入捕获模式,当这些通道捕捉到了对应的信号变化时就会产生一个CCx时间,单片机会自动的将CCx标志位从0变到1,所以我们只需要查询一下CCx标注位就可以知道这个通道当前有没有捕捉到信号变化。在使用之前需要对其清0。
3:启动定时器
4:向Trig引脚发送脉冲:
向PA0写1
延迟
向PA0写0
5:等待测量结束
CC1标志位从0变到1——捕获到了上升沿
CC2的标志位从0变到1 ——捕获到了下降沿
__HAL_TIM_GET_FLAG(__HANDLE__,__FLAG__)
返回值0——标志位等于0
返回值非零——标志位等于1
6:关闭定时器
7:计算测量结果
uint16_t ccr1 = __HAL_TIM_GET_COMPARE(&htim1 , TIM_CHANNEL_1); //计算CCR1的值
uint16_t ccr2 = __HAL_TIM_GET_COMPARE(&htim1 , TIM_CHANNEL_2); //计算CCR2的值
float pulseWidth = (cc1 - ccr2) *1e-6f;
float distance = 340.0f *pulseWidth / 2.0f;
8:通过判断距离点亮/熄灭板载LED
六、代码
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
//1.让计数器CNT归零
__HAL_TIM_SET_COUNTER(&htim1 , 0);
//2.清除CC1/CC2的标志位
__HAL_TIM_CLEAR_FLAG(&htim1 , TIM_FLAG_CC1);
__HAL_TIM_CLEAR_FLAG(&htim1 , TIM_FLAG_CC2);
//3.启动输入捕获
HAL_TIM_IC_Start(&htim1 , TIM_CHANNEL_1);
HAL_TIM_IC_Start(&htim1 , TIM_CHANNEL_2);
//4.向Trig发送脉冲
HAL_GPIO_WritePin(GPIOA , GPIO_PIN_0 , GPIO_PIN_SET);
for (uint32_t i = 0 ; i < 10 ; i++);
HAL_GPIO_WritePin(GPIOA , GPIO_PIN_0 , GPIO_PIN_RESET);
//5.等待测量结束
uint8_t success = 0;
uint32_t expireTime = HAL_GetTick() + 50 ; //计算超时时间
while(expireTime > HAL_GetTick()) //判断是否超时
{
uint32_t cc1Flag = __HAL_TIM_GET_FLAG(&htim1,TIM_FLAG_CC1); //CC1标志位
uint32_t cc2Flag = __HAL_TIM_GET_FLAG(&htim1,TIM_FLAG_CC2); //CC2标志位
if (cc1Flag && cc2Flag )
{
success = 1 ;
break;
}
}
//6.关闭定时器
HAL_TIM_IC_Stop(&htim1 , TIM_CHANNEL_1);
HAL_TIM_IC_Stop(&htim1 , TIM_CHANNEL_1);
//7.计算测量结果
if(success == 1)
{
uint16_t ccr1 = __HAL_TIM_GET_COMPARE(&htim1 , TIM_CHANNEL_1); //计算CCR1的值
uint16_t ccr2 = __HAL_TIM_GET_COMPARE(&htim1 , TIM_CHANNEL_2);//计算CCR2的值
float pulseWidth = (ccr1 - ccr2) *1e-6f; //计算脉宽 秒 = 微秒 * 1e-6
float distance = 340.0f *pulseWidth / 2.0f; //计算距离
if(distance < 0.2) //距离是否小于0.2米
{
HAL_GPIO_WritePin(GPIOC , GPIO_PIN_13 , GPIO_PIN_RESET); //点亮板载LED
}
else
{
HAL_GPIO_WritePin(GPIOC , GPIO_PIN_13 , GPIO_PIN_SET); //熄灭板载LED
}
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
最后将代码下载编译,就可以看到当物体距离超声波传感器0.2m以内时板载LED点亮,大于0.2米时板载LED熄灭。