#一、超声波HC_SR04简介 #二、超声波工作原理 #三、超声波测距步骤 #四、硬件接线 #五、项目代码
一、超声波HC_SR04简介
超声波传感器模块上面通常有两个超声波元器件,一个用于发射,一个用于接收。电路板上有四个引脚:VCC、GND、Trig(触发)、Echo(回应)
工作电压与电流:5V,15mA 感应距离:2~400cm 感测角度:不小于15度 被测物体的面积不要小于50平方厘米并且尽量平整 具备温度补偿电路 超声波模块的触发脚(Trig)输入10us以上的高电位,即可发射超声波,发射超声波后,与接收到收回的超声波之前,“响应“脚(Echo)位呈现高电平。因此,程序可以“响应”脚位(Echo)的高电平脉冲持续时间,换算出被测物的距离。
二、超声波原理
距离公式: 高电平持续时间*声速(340m/s)/2
三、超声波测距步骤
1.配置GPIO引脚结构体(Trig,Echo) 2.配置定时器结构体 3.配置定时器中断结构体 4.开启时钟(定时器、GPIO) 5.Trig引脚输出高电平(10us以上),然后关闭 6.等待Echo引脚输出高电平开始,定时器打开->开启计数器计数 7.等待Echo引脚输出高电平结束,定时器关闭->停止计数器计数
四、硬件接线
1.GND——GND 2.VCC——5V 3.Trig——PB11 4.Echo——PB10
五、项目代码
HC_SR04.C
#include "stm32f10x.h"
#include "hc_sr04.h"
#include "systick.h"
extern uint16_t mscount=0;
void HC_SR04Config(void)
{
GPIO_InitTypeDef GPIO_hcsr04init;
TIM_TimeBaseInitTypeDef TIM_hcsr04init;
NVIC_InitTypeDef NVIC_hcsr04init;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
GPIO_hcsr04init.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_hcsr04init.GPIO_Pin = GPIO_Pin_11;
GPIO_hcsr04init.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_hcsr04init);
GPIO_hcsr04init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_hcsr04init.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOB,&GPIO_hcsr04init);
TIM_hcsr04init.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_hcsr04init.TIM_CounterMode = TIM_CounterMode_Up;
TIM_hcsr04init.TIM_Period = 100-1;
TIM_hcsr04init.TIM_Prescaler = 72-1;
TIM_TimeBaseInit(TIM4,&TIM_hcsr04init);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM4,DISABLE);
NVIC_hcsr04init.NVIC_IRQChannel = TIM4_IRQn;
NVIC_hcsr04init.NVIC_IRQChannelCmd = ENABLE;
NVIC_hcsr04init.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_hcsr04init.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_hcsr04init);
}
void Open_Tim4(void)
{
TIM_SetCounter(TIM4,0);
mscount=0;
TIM_Cmd(TIM4,ENABLE);
}
void Close_Tim4(void)
{
TIM_Cmd(TIM4,DISABLE);
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update)!=RESET)
{
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
mscount++;
}
}
int GetEcho_time(void)
{
uint32_t t=0;
t=mscount*1000;
t+= TIM_GetCounter(TIM4);
TIM4->CNT=0;
ms_delay(50);
return t;
}
float Getlength(void)
{
int i=0;
uint32_t t=0;
float length=0;
float sum=0;
while(i!=5)
{
TRIG_Send(1);
us_delay(20);
TRIG_Send(0);
while(ECHO_Reci==0);
Open_Tim4();
i=i+1;
while(ECHO_Reci==1);
Close_Tim4();
t=GetEcho_time();
length=((float)t/58.0);
sum=sum+length;
}
length=sum/5.0;
return length;
}
HC_SR04.h
#include "stm32f10x.h"
#ifndef _HC_SR04_H
#define _HC_SR04_H
void HC_SR04Config(void);
void Open_Tim4(void);
void Close_Tim4(void);
int GetEcho_time(void);
float Getlength(void);
#define TRIG_Send(a) if(a)\
GPIO_SetBits(GPIOB,GPIO_Pin_11);\
else\
GPIO_ResetBits(GPIOB,GPIO_Pin_11)
#define ECHO_Reci GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10)
#endif
usart.c
#include "stm32f10x.h"
#include "usart.h"
#include <stdio.h>
void usart_init(void)
{
GPIO_InitTypeDef gpioinstructure;
USART_InitTypeDef usartinstructure;
NVIC_InitTypeDef nvicinstructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
gpioinstructure.GPIO_Mode = GPIO_Mode_AF_PP;
gpioinstructure.GPIO_Pin = GPIO_Pin_9 ;
gpioinstructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&gpioinstructure);
gpioinstructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpioinstructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA,&gpioinstructure);
usartinstructure.USART_BaudRate = 115200;
usartinstructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usartinstructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx ;
usartinstructure.USART_Parity = USART_Parity_No;
usartinstructure.USART_StopBits = USART_StopBits_1;
usartinstructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&usartinstructure);
USART_Cmd(USART1,ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
nvicinstructure.NVIC_IRQChannel = USART1_IRQn;
nvicinstructure.NVIC_IRQChannelCmd = ENABLE;
nvicinstructure.NVIC_IRQChannelPreemptionPriority = 1;
nvicinstructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&nvicinstructure);
}
void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data)
{
USART_SendData(USARTx, Data);
while(USART_GetFlagStatus(USARTx,USART_FLAG_TXE)==RESET);
}
void USARTSendStr(USART_TypeDef* USARTx, char *str)
{
uint16_t i=0;
do
{
USARTSendByte(USARTx,*(str+i));
i++;
}while(*(str+i)!='\0');
while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)==RESET);
}
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(uint8_t)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return (ch);
}
int fgetc(FILE *f)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET);
return (int)USART_ReceiveData(USART1);
}
usart.h
#include "stm32f10x.h"
#include <stdio.h>
void usart_init(void);
void USARTSendStr(USART_TypeDef* USARTx, char *str);
main.c
#include "stm32f10x.h"
#include "main.h"
#include "LED.h"
#include "usart.h"
#include "relay.h"
#include "shake.h"
#include "exti.h"
#include "tim.h"
#include "motor.h"
#include "systick.h"
#include "hc_sr04.h"
void delay(uint16_t time)
{
uint16_t i=0;
while(time--)
{
i=12000;
while(i--);
}
}
int main()
{
float Length=0;
usart_init();
HC_SR04Config();
while(1)
{
Length=Getlength();
printf("%.3f\r\n",Length);
ms_delay(500);
}
}
|