diff --git a/src/mylib/device/hc_sr04.c b/src/mylib/device/hc_sr04.c index 57ff69113b16afdab2fb8ffc3704c37e148442b5..6075b4614c9636f2e204b6a9e8554dd162e7544c 100644 --- a/src/mylib/device/hc_sr04.c +++ b/src/mylib/device/hc_sr04.c @@ -1,61 +1,79 @@ #include +#include -#if 0 -#include "bsp_SysTick.h" -#include "stm32f10x.h" -#include "stm32f10x_gpio.h" -#include "stm32f10x_rcc.h" -#include "stm32f10x_tim.h" -#include +inline static void hc_sr04_delay_us(uint32_t us) +{ + HAL_TIM_Base_Start(g_htim); // 启动定时器 + while (g_htim->Instance->CNT < us); + g_htim->Instance->CNT = 0; + HAL_TIM_Base_Stop(g_htim); // 关闭定时器 +} -uint32_t pulse_width_us = 0; -uint32_t distance_cm = 0; +inline static void hc_sr04_delay_ms(uint32_t ms) +{ + HAL_Delay(ms); +} -void HCSR04_Init(void) +void HCSR04_Init(TIM_HandleTypeDef* htim, GPIO_TypeDef* port, uint16_t T_pin, uint16_t E_pin) { - // 定义GPIO的结构体变量 - GPIO_InitTypeDef GPIO_InitStructure = {0}; - NVIC_InitTypeDef NVIC_InitStructure; + g_htim = htim; + g_port = port; + g_pin_T = T_pin; + g_pin_E = E_pin; +} +void SR04_Trigger(void) +{ + Trig_ON; + hc_sr04_delay_us(10); + Trig_OFF; +} - RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // 使能超声波的GPIO对应的时钟 - /* Trig引脚 */ - GPIO_InitStructure.GPIO_Pin = HC_R04_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 引脚输出速度设置为快 +void GPIO_EXTI_Callback(uint16_t g_pin) +{ + static int count = 0; + if (g_pin == HC_R04_PIN) { + if (HAL_GPIO_ReadPin(g_port, HC_R04_PIN)) { + HAL_TIM_Base_Start(g_htim); + __HAL_TIM_SET_COUNTER(g_htim, 0); //清空定时器的值 + } + else if (HAL_GPIO_ReadPin(g_port, HC_R04_PIN) == 0) { + HAL_TIM_Base_Stop(g_htim); // 停止定时器 + count = __HAL_TIM_GET_COUNTER(g_htim); //获取当前计数值 + distance_cm = count * 340 / 2 * 0.000001 * 100; + //printf("distance_cm is %d\r\n", distance_cm); + count = 0; + } + } +} - // 初始化引脚配置 - GPIO_Init(GPIOC, &GPIO_InitStructure); +void EXTI9_5_IRQHandler(void) +{ + //GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource9); + //中断部分不知道怎么处理 +} - /*Echo引脚*/ - GPIO_InitStructure.GPIO_Pin = HC_R04_PIN; // 选择超声波的引脚 - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 设置为双边沿触发外部中断 - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 引脚输出速度设置为快 +uint32_t Get_Distance(void) +{ + return distance_cm; +} - // 初始化引脚配置 - GPIO_Init(GPIOC, &GPIO_InitStructure); +#if 0 - NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //选择TIM4中断通道 - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级为0 - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级为1 - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断通道 - NVIC_Init(&NVIC_InitStructure); +#include "bsp_SysTick.h" +#include "stm32f10x.h" +#include "stm32f10x_gpio.h" +#include "stm32f10x_rcc.h" +#include "stm32f10x_tim.h" +#include - /*配置定时器4为us级定时器*/ - RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); +uint32_t pulse_width_us = 0; +uint32_t distance_cm = 0; - TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; - TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频因子 - TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数器 - TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF; //自动装载值 - TIM_TimeBaseInitStructure.TIM_Prescaler = 83; //分频系数 - TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; - TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure); -} //外部中断处理函数 void EXTI9_5_IRQHandler(void) @@ -63,13 +81,7 @@ void EXTI9_5_IRQHandler(void) GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource9); } -void SR04_Trigger(void) -{ - // 发送10us的高电平脉冲 - Trig_ON; - Delay_us(10); - Trig_OFF; -} + void GPIO_EXTI_Callback(uint16_t GPIO_Pin) { diff --git a/src/mylib/device/hc_sr04.h b/src/mylib/device/hc_sr04.h index d6a8c625ae72260894a51cf6b62f62d295b867a5..d40c2fd330ea396841df10c84fbb48e6adc020f9 100644 --- a/src/mylib/device/hc_sr04.h +++ b/src/mylib/device/hc_sr04.h @@ -1,18 +1,37 @@ #ifndef _HC_SR04_H #define _HC_SR04_H +#include #include /* PC8:Trig(发射引脚) PC9:Echo(接收引脚) */ -#define HC_R04_PIN GPIO_Pin_9 -#define Trig_ON GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_SET) -#define Trig_OFF GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_RESET) - - -void HCSR04_Init(void); +static TIM_HandleTypeDef* g_htim; +static GPIO_TypeDef* g_port; +static uint16_t g_pin_T; +static uint16_t g_pin_E; +uint32_t distance_cm = 0; +uint32_t pulse_width_us = 0; + +#define Trig_ON HAL_GPIO_WritePin(g_port, g_pin_T, GPIO_PIN_SET); +#define Trig_OFF HAL_GPIO_WritePin(g_port, g_pin_T, GPIO_PIN_RESET); +#define HC_R04_PIN g_pin_E + +typedef struct +{ + uint32_t gpio; + int8_t valid; +} hc_sr04_t; + +#define DEVICE_BUZZER_INITIALIZER(_gpio, _valid) \ + { \ + .gpio = _gpio, \ + .valid = _valid, \ + } + +void HCSR04_Init(TIM_HandleTypeDef* htim, GPIO_TypeDef* port, uint16_t T_pin, uint16_t E_pin); void SR04_Trigger(void);