当前位置: 代码迷 >> 综合 >> STM32F103—有关HC-SR04超声波测距模块的代码
  详细解决方案

STM32F103—有关HC-SR04超声波测距模块的代码

热度:77   发布时间:2023-11-27 07:04:32.0

关于HC-SR04超声波测距模块(Trig和Echo的引脚可以自定义,我用的TRIG是PBout(11) ECHO是PBin(10))

硬件连接:

 

Vcc :5V输入(最好不要用3.3V输入)
Trig :触发信号输入(直接给Trig高电平,然后读取Echo引脚是否为高电平,若为高电平,则开启定时器,然后继续检测等待其为低电平的时候,获取计数器值,然后进行计算)
Echo :回响信号输出(开启外部中断,先将Echo配置上升沿中断,当中断来临的时候,在中断函数里面开启定时器,再将其配置为下降沿中断,等待下降沿中断来临的时候,获取计数器值)
Gnd:连接地线

在使用HC-SR04超声波测距模块时,前5次测量求取平均值,减少测量的误差

#include "hcsr04.h"
#include "SysTick.h"
#include "usart.h"	u16 msHcCount = 0;void hcsr04_NVIC()//定时器4设置
{NVIC_InitTypeDef NVIC_InitStructure;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;             NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;       NVIC_Init(&NVIC_InitStructure);
}void Hcsr04Init()//IO口初始化 及其他初始化
{  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;   GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(HCSR04_CLK, ENABLE);GPIO_InitStructure.GPIO_Pin =HCSR04_TRIG;      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(HCSR04_PORT, &GPIO_InitStructure);GPIO_ResetBits(HCSR04_PORT,HCSR04_TRIG);GPIO_InitStructure.GPIO_Pin =   HCSR04_ECHO;     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(HCSR04_PORT, &GPIO_InitStructure);  GPIO_ResetBits(HCSR04_PORT,HCSR04_ECHO);    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);   TIM_DeInit(TIM2);TIM_TimeBaseStructure.TIM_Period = (1000-1);TIM_TimeBaseStructure.TIM_Prescaler =(72-1); //1us进入一次更新TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);          TIM_ClearFlag(TIM4, TIM_FLAG_Update);  TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);    hcsr04_NVIC();TIM_Cmd(TIM4,DISABLE);     
}static void OpenTimerForHc() //打开定时器4 
{TIM_SetCounter(TIM4,0);msHcCount = 0;TIM_Cmd(TIM4, ENABLE); 
}static void CloseTimerForHc() //关闭定时器4   
{TIM_Cmd(TIM4, DISABLE); 
}void TIM4_IRQHandler(void)//定时器4中断  
{if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)  {TIM_ClearITPendingBit(TIM4, TIM_IT_Update  ); msHcCount++;}
}u32 GetEchoTimer(void)//获取定时器4计数器值
{u32 t = 0;t = msHcCount*1000;t += TIM_GetCounter(TIM4);TIM4->CNT = 0;  delay_ms(50);return t;
}float Hcsr04GetLength(void)//通过定时器4计数器值推算距离
{u32 t = 0;int i = 0;float lengthTemp = 0;float sum = 0;while(i!=5){TRIG_Send = 1;      delay_us(20);TRIG_Send = 0;while(ECHO_Reci == 0);      OpenTimerForHc();        i = i + 1;while(ECHO_Reci == 1);CloseTimerForHc();        t = GetEchoTimer();        lengthTemp = ((float)t/58);//cmsum = lengthTemp + sum ;}lengthTemp = sum/5.0;return lengthTemp;
}
#ifndef __HCSR04_H
#define __HCSR04_H	#include "stm32f10x.h"
#include "sys.h"//超声波硬件接口定义
#define HCSR04_PORT     GPIOB
#define HCSR04_CLK      RCC_APB2Periph_GPIOB
#define HCSR04_TRIG     GPIO_Pin_11
#define HCSR04_ECHO     GPIO_Pin_10#define TRIG_Send  PBout(11)
#define ECHO_Reci  PBin(10)void hcsr04_NVIC(void);//定时器4设置
void Hcsr04Init(void);//IO口初始化 及其他初始化
static void OpenTimerForHc(void);//打开定时器4
static void CloseTimerForHc(void);//关闭定时器4
void TIM4_IRQHandler(void);//定时器4终中断
u32 GetEchoTimer(void);//获取定时器4计数器值
float Hcsr04GetLength(void);//通过定时器4计数器值推算距离#endif

(1条消息) STM32F103C8T6基本功能代码_墨云景阳的博客-CSDN博客