一、成果图
说明:本程序的源码地址:https://github.com/Lfy-YF/Campus-Project
CSDN下载连接:https://download.csdn.net/download/weixin_58849239/87829470
源码包含四个版本,涉及单路测距、三路测距、增加外部中断(用来版本记录的~)
运行效果:本程序实现脱机测距,共左中右三路超声波传感器同时实时工作,数据实时通过屏幕显示,并且当测距距离小于75cm(正常男性与女性的步幅平均值)时红色led灯亮,蜂鸣器报警。
二、HC-SR04超声波模块说明
以上时序图表明你只需要提供一个 10uS 以上脉冲触发信号,该模块内部将发出 8 个 40kHz 周期电平并检测回波。一旦检测到有回波信号则输出回响信号。回响信号的脉冲宽度与所测的距离成正比。由此通过发射信号到收到的回响信号时间间隔可以计算得到距离。公式: uS/58-厘米或者 uS/148=英寸;或是:距离高电平时间*声速(340M/S) /2:建议测量周期为 60ms 以上,以防止发射信号对回响信号的影响。
三、引脚接线
VCC 供 5V电源,GND为地线,TRIG 触发控制信号输入,ECHO 回响信号输出等四个接口端。
我的接线方式: (不知道怎么接线的可以去看板子的说明)
1.一号超声波(左侧)
Trig接IO口PF0
Echo接IO口PF1
VCC与GND接5V
2.二号超声波(右侧)
Trig接IO口PF2
Echo接IO口PF3
VCC与GND接5V
3.三号超声波(中间)
Trig接IO口PF4
Echo接IO口PF5
VCC与GND接5V
四、主函数
(1)单路
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "hcsr04.h"
#include "lcd.h"
#include "beep.h"
#include "led.h"
float Distance=0;
int main(void)
{
u32 i=0;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LCD_Init();
LED_Init();
BEEP_Init();
Hcsr04Init();
POINT_COLOR=RED; //画笔颜色
LED0=1;
LED1=~LED0;
BEEP=LED1;
while(1)
{
i++;
Distance = (Hcsr04GetLength());
if(Distance<=60.0 && BEEP==0){
BEEP=1;
LED1=BEEP;
LED0=~LED1;
}
if(Distance>60.0 && BEEP==1){
BEEP=0;
LED1=BEEP;
LED0=~LED1;
}
LCD_ShowString(30,40,210,24,24,"The distance is :");
LCD_ShowNum(30,60,Distance,5,24);
LCD_ShowString(120,60,210,24,24," cm");
delay_ms(100);
if(i==100)
{
LCD_Clear(WHITE);
POINT_COLOR=RED;
i=0;
}
}
}
(2)三路
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "hcsr04.h"
#include "lcd.h"
#include "beep.h"
#include "led.h"
float Distance=0; //定义距离变量1(左)
float Distance1=0; //定义距离变量1(右)
float Distance2=0; //定义距离变量1(中)
int main(void)//主函数
{
u32 i=0;
delay_init(); //延时器初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断优先级NVIC
uart_init(115200); //串口比特率
LCD_Init(); //LCD初始化
LED_Init(); //LED初始化
BEEP_Init(); //BEEP初始化
Hcsr04Init(); //HC-SR04初始化
POINT_COLOR=RED; //画笔颜色
LED0=1;//蜂鸣器与LED小灯的逻辑关系
LED1=~LED0;
BEEP=LED1;
while(1)
{
i++;
Distance = (Hcsr04GetLength());//对距离变量进行赋值(左)
Distance1 = (Hcsr04GetLength1());//对距离变量进行赋值(右)
Distance2 = (Hcsr04GetLength2());//对距离变量进行赋值(中)
if((Distance<=75.0||Distance1<=75.0||Distance2<=75.0) && BEEP==0){ //逻辑关系
BEEP=1;
LED1=BEEP;
LED0=~LED1;
}
if((Distance>75.0&&Distance1>75.0&&Distance2>75.0) && BEEP==1){ //逻辑关系
BEEP=0;
LED1=BEEP;
LED0=~LED1;
}
//LCD屏显示
LCD_ShowString(30,40,210,24,24,"Left distance:");
LCD_ShowNum(30,70,Distance,5,24);
LCD_ShowString(120,70,210,24,24," cm");
LCD_ShowString(30,100,210,24,24,"Right distance:");
LCD_ShowNum(30,130,Distance1,5,24);
LCD_ShowString(120,130,210,24,24," cm");
LCD_ShowString(30,160,210,24,24,"Middle distance:");
LCD_ShowNum(30,190,Distance2,5,24);
LCD_ShowString(120,190,210,24,24," cm");
delay_ms(100);//延时设置
if(i==100)
{
LCD_Clear(WHITE);
POINT_COLOR=RED;
i=0;
}
}
}
五、外部中断函数
//新增加中断
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//HCSR04_ECHO
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; //HCSR04_ECHO
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;//上升下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能外部中断
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOF,GPIO_PinSource1);
//HCSR04_ECHO1
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //HCSR04_ECHO1
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
EXTI_InitStructure.EXTI_Line = EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;//上升下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能外部中断
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOF,GPIO_PinSource3);
//HCSR04_ECH2
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //HCSR04_ECHO2
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;//上升下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能外部中断
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOF,GPIO_PinSource5);
//新增加中断
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器时钟
TIM_TimeBaseStructure.TIM_Period = (1000-1); //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =(72-1); //设置用来作为TIM3时钟频率除数的预分频
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //根据指定的参数初始化TIMx的时间基数单位
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //使能指定的TIM3中断,允许更新中断
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_Cmd(TIM3,DISABLE);
六、注意事项
1.HC-SR04模块不宜带电连接,会影响模块的正常工作。(若要带电连接可以先让模块的GND端先连接)文章来源:https://www.toymoban.com/news/detail-675369.html
2.HC-SR04的被测物体的面积不要少于0.5平方米并且尽量平整,不然影响测量结果。(我使用的是A4大小的课本)文章来源地址https://www.toymoban.com/news/detail-675369.html
到了这里,关于基于STM32F103ZET6的(单/三)路HC-SR04超声波测距+TFTLCD实时显示+距离报警的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!