HC-SR04模块的优势及应用领域
HC-SR04模块优势:
此模块性能稳定,测度距离精确,模块高精度,盲区小。
产品应用领域:
1、机器人避障
2、物体测距
3、液位检测
4、公共安防
5、停车场检测
超声波测距模块工作原理
(1)采用IO口TRIG触发测距,给至少10us的高电平信号;
(2)模块自动发送8个40khz的方波,自动检测是否有信号返回;
(3)有信号返回,通过IO口ECHO输出一个高电平,高电平持续的时间就是超声波从发射到返回的时间。测试距离=(高电平时间*声速(340M/S))/2;
(4)本模块使用方法简单,一个控制口发一个10US以上的高电平,就可以在接收口等待高电平输出。一有输出就可以开定时器计时,当此口变为低电平时就可以读定时器的值,此时就为此次测距的时间,方可算出距离。如此不断的周期测,即可以达到你移动测量的值
操作:
初始化时将trig和echo端口都置低,首先向给trig 发送至少10 us的高电平脉冲(模块自动向外发送8个40K的方波),然后等待,捕捉 echo 端输出上升沿,捕捉到上升沿的同时,打开定时器开始计时,再次等待捕捉echo的下降沿,当捕捉到下降沿,读出计时器的时间,这就是超声波在空气中运行的时间,按照 测试距离=(高电平时间*声速(340M/S))/2 就可以算出超声波到障碍物的距离。
Count函数
void Count(void)
{
long long s1;
long long temp1=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(115200);
TIM13_PWM_Init(1000-1,84-1);
TIM5_CH1_Cap_Init(0XFFFFFFFF,84-1);
while(1)
{
delay_ms(10);
TIM_SetCompare1(TIM13,TIM_GetCapture1(TIM13)+1);
if(TIM_GetCapture1(TIM13)==10)
TIM_SetCompare1(TIM13,0);
if(TIM5CH2_CAPTURE_STA&0X80)
{
temp1=TIM5CH2_CAPTURE_STA&0X3F;
temp1*=0XFFFFFFFF;
temp1+=TIM5CH2_CAPTURE_VAL;
s1=temp1/2.0*340.0*100*0.000001;
printf("diatance:%lld cm\r\n",s1);
TIM5CH2_CAPTURE_STA=0;
}
delay_ms(200);
}
}
timer.c
#include "timer.h"
#include "usart.h"
void TIM13_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_PinAFConfig(GPIOF,GPIO_PinSource8,GPIO_AF_TIM13);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM13,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OC1Init(TIM13, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM13, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM13,ENABLE);
TIM_Cmd(TIM13, ENABLE);
}
void TIM5_CH1_Cap_Init(u32 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM5_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM5);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC2,ENABLE);
TIM_Cmd(TIM5,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
u8 TIM5CH2_CAPTURE_STA=0;
u32 TIM5CH2_CAPTURE_VAL;
void TIM5_IRQHandler(void)
{
if((TIM5CH2_CAPTURE_STA&0X80)==0)
{
if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
{
if(TIM5CH2_CAPTURE_STA&0X40)
{
if((TIM5CH2_CAPTURE_STA&0X3F)==0X3F)
{
TIM5CH2_CAPTURE_STA|=0X80;
TIM5CH2_CAPTURE_VAL=0XFFFFFFFF;
}else TIM5CH2_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM5, TIM_IT_CC2) != RESET)
{
if(TIM5CH2_CAPTURE_STA&0X40)
{
TIM5CH2_CAPTURE_STA|=0X80;
TIM5CH2_CAPTURE_VAL=TIM_GetCapture2(TIM5);
TIM_OC2PolarityConfig(TIM5,TIM_ICPolarity_Rising);
}
else
{
TIM5CH2_CAPTURE_STA=0;
TIM5CH2_CAPTURE_VAL=0;
TIM5CH2_CAPTURE_STA|=0X40;
TIM_Cmd(TIM5,DISABLE );
TIM_SetCounter(TIM5,0);
TIM_OC2PolarityConfig(TIM5,TIM_ICPolarity_Falling);
TIM_Cmd(TIM5,ENABLE );
}
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update|TIM_IT_CC2);
}
timer.h
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM5_CH1_Cap_Init(u32 arr,u16 psc);
void TIM13_PWM_Init(u16 arr,u16 psc);
#endif