C语言stm32hc-sr04超声波测距代码
时间: 2023-05-30 11:02:27 浏览: 385
基于STM32F10X的HC-SR04超声测距源码.7z
以下是C语言STM32HC-SR04超声波测距代码:
```
#include "stm32f10x.h"
#define TRIG_PIN GPIO_Pin_0
#define ECHO_PIN GPIO_Pin_1
#define TRIG_PORT GPIOA
#define ECHO_PORT GPIOA
void delay_us(uint32_t n)
{
uint32_t i;
for(i=0;i<n;i++)
{
asm("NOP");
}
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
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_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ECHO_PORT, &GPIO_InitStructure);
TIM_TimeBaseInitStruct.TIM_Prescaler = 72-1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 65535;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse = 0;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStruct);
TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICFilter = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStruct);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE);
while (1)
{
GPIO_SetBits(TRIG_PORT, TRIG_PIN);
delay_us(10);
GPIO_ResetBits(TRIG_PORT, TRIG_PIN);
while(GPIO_ReadInputDataBit(ECHO_PORT, ECHO_PIN) == RESET);
TIM_SetCounter(TIM2, 0);
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
while(GPIO_ReadInputDataBit(ECHO_PORT, ECHO_PIN) == SET);
TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE);
float distance = (float)TIM_GetCapture1(TIM2) * 0.017;
}
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
}
}
```
在该代码中,我们使用了STM32F10x作为开发板,使用了PA0和PA1作为超声波传感器的触发和接收引脚。我们使用了TIM2作为计时器,并在输入捕获模式下捕获了超声波信号的上升沿和下降沿。最后,我们用计时器的计数值计算了距离。
在代码中,我们首先初始化了GPIO和TIM2的相关设置,然后循环等待超声波信号的返回。当我们发送超声波信号时,我们将PA0置为高电平10微秒,然后将其置为低电平,以触发超声波传感器。接下来,我们等待超声波信号的返回,同时启用输入捕获模式,并在上升沿时捕获计数器的计数值。当信号下降时,我们禁用输入捕获模式,并计算距离。
值得注意的是,由于我们使用的是72MHz的晶振,因此我们需要将计时器的预分频器设置为72-1,以使计时器的时钟频率为1MHz。在计算距离时,我们将计数值乘以0.017,这是因为声波在空气中的传播速度约为340m/s,即每秒传播34000厘米,因此1微秒的时间内声波可传播0.034厘米,而我们的计数器单位是微秒,因此需要将计数值乘以0.017以得到距离的单位是厘米。
阅读全文