写一个STM32与HC-SR04通信的代码
时间: 2023-05-22 14:05:48 浏览: 137
好的,以下是一个STM32与HC-SR04通信的代码示例:
```
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
#define Echo_Pin GPIO_Pin_2
#define Echo_Port GPIOA
#define Trig_Pin GPIO_Pin_1
#define Trig_Port GPIOA
volatile uint16_t time = 0; // 用于计算回声脉冲的时间
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = Trig_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Trig_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Echo_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Echo_Port, &GPIO_InitStructure);
GPIO_ResetBits(Trig_Port, Trig_Pin); // 置 Trig 引脚为低电平
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC3, ENABLE);
}
void Trig_HC_SR04(void)
{
GPIO_SetBits(Trig_Port, Trig_Pin); // 置 Trig 引脚为高电平
delay_us(15); // 延时 15us
GPIO_ResetBits(Trig_Port, Trig_Pin); // 置 Trig 引脚为低电平
}
float Get_Distance(void)
{
float distance = 0;
time = 0;
Trig_HC_SR04(); // 触发 HC-SR04 中的一次测距
while (!GPIO_ReadInputDataBit(Echo_Port, Echo_Pin)) {} // 等待 HC-SR04 的回声脉冲
TIM_SetCounter(TIM2, 0); // 重置定时器的计数器
TIM_Cmd(TIM2, ENABLE); // 启动定时器计数
while (GPIO_ReadInputDataBit(Echo_Port, Echo_Pin)) {} // 等待 HC-SR04 的回声脉冲结束
TIM_Cmd(TIM2, DISABLE); // 关闭定时器计数
distance = (float) time / 58; // 计算距离
return distance;
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM_Configuration();
while (1)
{
float distance = Get_Distance();
printf("Distance: %.2fcm\n", distance);
delay_ms(500); // 每隔500ms获取一次距离
}
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC3) == SET)
{
time = TIM_GetCapture3(TIM2); // 获取定时器计数器的当前值
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3); // 清除定时器 CC3 中断标志位
}
}
void delay_us(uint32_t us)
{
uint32_t ticker = us * 8;
while (ticker--);
}
void delay_ms(uint32_t ms)
{
while (ms--)
{
delay_us(1000);
}
}
```
此代码用于控制STM32与超声波测距模块HC-SR04进行通信。它通过定时器TIM2来计算超声波回声脉冲的时间,然后根据时间计算出距离。
阅读全文