stm32单片机两路超声波测速代码
时间: 2023-07-09 10:03:48 浏览: 85
stm32超声波代码
以下是基于STM32的两路超声波测速代码,使用的是STM32CubeMX和HAL库。
```c
#include "main.h"
#include "string.h"
#include "stdio.h"
#define TRIG1_Pin GPIO_PIN_10
#define TRIG1_GPIO_Port GPIOB
#define ECHO1_Pin GPIO_PIN_11
#define ECHO1_GPIO_Port GPIOB
#define TRIG2_Pin GPIO_PIN_12
#define TRIG2_GPIO_Port GPIOB
#define ECHO2_Pin GPIO_PIN_13
#define ECHO2_GPIO_Port GPIOB
TIM_HandleTypeDef htim2;
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_TIM2_Init(void);
uint32_t pulse1 = 0;
uint32_t pulse2 = 0;
float distance1 = 0;
float distance2 = 0;
char buffer[100];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_TIM2_Init();
HAL_TIM_Base_Start_IT(&htim2);
while (1)
{
HAL_GPIO_WritePin(TRIG1_GPIO_Port, TRIG1_Pin, GPIO_PIN_SET);
HAL_Delay(10);
HAL_GPIO_WritePin(TRIG1_GPIO_Port, TRIG1_Pin, GPIO_PIN_RESET);
while(HAL_GPIO_ReadPin(ECHO1_GPIO_Port, ECHO1_Pin) == GPIO_PIN_RESET);
uint32_t start1 = HAL_GetTick();
while(HAL_GPIO_ReadPin(ECHO1_GPIO_Port, ECHO1_Pin) == GPIO_PIN_SET);
uint32_t end1 = HAL_GetTick();
pulse1 = end1 - start1;
distance1 = pulse1 * 0.034 / 2;
HAL_GPIO_WritePin(TRIG2_GPIO_Port, TRIG2_Pin, GPIO_PIN_SET);
HAL_Delay(10);
HAL_GPIO_WritePin(TRIG2_GPIO_Port, TRIG2_Pin, GPIO_PIN_RESET);
while(HAL_GPIO_ReadPin(ECHO2_GPIO_Port, ECHO2_Pin) == GPIO_PIN_RESET);
uint32_t start2 = HAL_GetTick();
while(HAL_GPIO_ReadPin(ECHO2_GPIO_Port, ECHO2_Pin) == GPIO_PIN_SET);
uint32_t end2 = HAL_GetTick();
pulse2 = end2 - start2;
distance2 = pulse2 * 0.034 / 2;
sprintf(buffer, "Distance 1: %.2f cm\r\n", distance1);
HAL_UART_Transmit(&huart2, (uint8_t*) buffer, strlen(buffer), 1000);
sprintf(buffer, "Distance 2: %.2f cm\r\n", distance2);
HAL_UART_Transmit(&huart2, (uint8_t*) buffer, strlen(buffer), 1000);
}
}
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();
}
}
static void MX_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 0xFFFFFFFF;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE();
HAL_GPIO_WritePin(TRIG1_GPIO_Port, TRIG1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(TRIG2_GPIO_Port, TRIG2_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = TRIG1_Pin|TRIG2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = ECHO1_Pin|ECHO2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2)
{
HAL_IncTick();
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
```
在此代码中,我们使用了两个超声波模块,并使用了PB10、PB11、PB12和PB13引脚进行控制。我们使用TIM2定时器来进行系统时钟的计数,以及使用UART2进行与计算机的通信。超声波模块的触发脚(TRIG)连接到PB10和PB12引脚,回波脚(ECHO)连接到PB11和PB13引脚。当触发脚被拉高时,模块会发送一个8个脉冲的40kHz信号,当回波脚检测到这个信号后,它会将回波信号发送回来。我们使用定时器来测量回波信号的脉冲宽度,从而计算出距离。
在代码中,我们使用HAL_Delay()函数来产生10微秒的触发信号,并使用HAL_GetTick()函数来获取回波脉冲的时间戳。我们还使用sprintf()函数将测量得到的距离数据存储在buffer数组中,并使用HAL_UART_Transmit()函数将数据发送到计算机。
请注意,此代码仅用于参考,并可能需要根据您的硬件设置进行修改。
阅读全文