STM32F407 study notes-HC-SR04 module (basic ranging application)

STM32F407 study notes-HC-SR04 module (basic ranging application)

1. Basic principle:
define the GPIO of stm32, give Trig high level (more than 10us can be given here is 20us), then pull down to send ultrasound, the ultrasound will return to the ultrasound module when it hits an obstacle, and Echo will output high power If you get the Echo high level duration through the timer, you can calculate the distance to the obstacle.

2. Code function:
Control the ultrasonic module through stm32 to feedback the measured distance on the serial port.

3. Wiring: Trig——PA6, Echo——PA7

4. Code part:
HC-SR04.h

#ifndef _sr04_H
#define _sr04_H
#include "sys.h"

#define TRIG_Send PAout(6) 
#define ECHO_Reci PAin(7) 

float Senor_Using(void);
void TIM4_Int_Init(void);




#endif

HC-SR04.c

#include "HC-SR04.h"
#include "delay.h"
#include "math.h"

int overcount=0; 


//GPIO及定时器设置
void TIM4_Int_Init()
{
    
    
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

GPIO_InitStruct.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);

GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_7;
GPIO_Init(GPIOA,&GPIO_InitStruct);

TIM_TimeBaseStructure.TIM_Period = 999; 
TIM_TimeBaseStructure.TIM_Prescaler =71; 
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE );

NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

TIM_Cmd(TIM4, DISABLE);
}
//测距函数
float Senor_Using(void) 
{
    
    
float length=0,sum=0;
u16 tim;
unsigned int i=0;

while(i!=5)
{
    
    
PAout(6)=1; 
delay_us(20); 
PAout(6)=0; 
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)==RESET);
TIM_Cmd(TIM4,ENABLE);
i+=1; 
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)==SET);
TIM_Cmd(TIM4,DISABLE);
tim=TIM_GetCounter(TIM4); 
length=(tim+overcount*1000)/58.0; 
sum=length+sum;
TIM4->CNT=0; 
overcount=0; 
delay_ms(100);
}
length=sum/5;//取均值
return length;
}
//中断函数
void TIM4_IRQHandler(void)
{
    
    
if (TIM_GetITStatus(TIM4,TIM_IT_Update)!= RESET) 
{
    
    
TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); 
overcount++;
}
}

main.c

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "HC-SR04.h"

int main(void)
{
    
    
float length;
delay_init(168);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(9600);
TIM4_Int_Init();
while(1)
{
    
    
length = Senor_Using(); 
	printf("¾àÀëΪ:%.3fcm\n",length);
delay_ms(1000);
}
}

PS:
(1): The template used is a punctual atom
(2): Ultrasonic module 5v power supply
(3): Based on stm32f407zgt6 successfully tested

Only for beginners to learn and use

Guess you like

Origin blog.csdn.net/weixin_54121461/article/details/113996294