STM32 | 超声波实战

发布于:2024-06-02 ⋅ 阅读:(128) ⋅ 点赞:(0)

​01、上节回顾

STM32 | HC-SR04 超声波测距模块 | DHT11数字温湿度传感器(第七天)
STM32 | 数字温湿度传感器DHT11
STM32 | HC-SR04 超声波测距模块
STM32 | DHT11数字温湿度传感器实战

02、超声波图示

03、超声波头文件

#ifndef __SR04_H#define __SR04_H#include "stm32f4xx.h"#include "delay.h"void Sr04_Init(void);u16 Get_Sr04_Value(void);#endif

04、超声波源文件

#include "sr04.h"/*********************************引脚说明:PA2做为普通输出(TRIG)PA3做为普通输入(ECHO)**********************************/void Sr04_Init(void){
    GPIO_InitTypeDef        GPIO_InitStruct;  TIM_TimeBaseInitTypeDef    TIM_TimeBaseInitStruct;      //使能GPIOA组时钟  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);  //1、能定时器时钟  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);    GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_2;    //引脚20  GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_OUT;  //输出模式  GPIO_InitStruct.GPIO_OType  = GPIO_OType_PP;  //推挽输出  GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_UP;    //上拉  GPIO_InitStruct.GPIO_Speed  = GPIO_Speed_50MHz; //速度   GPIO_Init(GPIOA, &GPIO_InitStruct);        GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_3;    //引脚3  GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IN;    //输入模式  GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_UP;    //上拉  GPIO_Init(GPIOA, &GPIO_InitStruct);        TIM_TimeBaseInitStruct.TIM_Prescaler  = 84-1;          // 84分频 84MHZ/84 = 1MHZ  TIM_TimeBaseInitStruct.TIM_Period    = 50000-1;            TIM_TimeBaseInitStruct.TIM_CounterMode  = TIM_CounterMode_Up;  // 向上计数  TIM_TimeBaseInitStruct.TIM_ClockDivision= TIM_CKD_DIV1;     

网站公告

今日签到

点亮在社区的每一天
去签到