From e3e2a78fe08be3f3a90fe4e689e5e1cdf9bd7f1c Mon Sep 17 00:00:00 2001 From: EN Date: Sun, 14 Jul 2024 16:47:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9delay=5Fus=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E7=9B=B8=E5=85=B3=E7=9A=84=E8=B6=85=E5=A3=B0?= =?UTF-8?q?=E6=B3=A2=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Inc/syscalls.h | 79 +++++++++++++++++++++++++++++++++++++++++++++ Core/Src/hcsr04.c | 26 +++------------ Core/Src/main.c | 20 +++--------- Core/Src/usart.c | 13 -------- 4 files changed, 88 insertions(+), 50 deletions(-) create mode 100644 Core/Inc/syscalls.h diff --git a/Core/Inc/syscalls.h b/Core/Inc/syscalls.h new file mode 100644 index 0000000..81c09c0 --- /dev/null +++ b/Core/Inc/syscalls.h @@ -0,0 +1,79 @@ +#ifndef __SYSCALLS_H +#define __SYSCALLS_H + +#include +#include +#include +#include +#include +#include + +extern UART_HandleTypeDef huart2; + +extern int __io_putchar(int ch) __attribute__((weak)); +extern int __io_getchar(void) __attribute__((weak)); + +__attribute__((weak)) int _read(int file, char *ptr, int len) { + (void)file; + int DataIdx; + + for (DataIdx = 0; DataIdx < len; DataIdx++) { + *ptr++ = __io_getchar(); + } + + return len; +} + +__attribute__((weak)) int _write(int file, char *ptr, int len) { + (void)file; + int DataIdx; + + for (DataIdx = 0; DataIdx < len; DataIdx++) { + __io_putchar(*ptr++); + } + return len; +} + +// 条件编译 +#ifdef __GNUC__ +#define PUTCHAR_PROTOTYPE int __io_putchar(int ch) +#define GETCHAR_PROTOTYPE int __io_getchar(void) +#else +#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) +#define GETCHAR_PROTOTYPE int fgetc(FILE *f) +#endif /* __GNUC__ */ + +/** + * @brief 重定向 C 标准库 printf 函数到串口 huart2 + * @author Suroy + * @param ch + * @param f + * @return int + * + * @usage printf("USART1_Target:\r\n"); + */ +PUTCHAR_PROTOTYPE { + HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); // 阻塞式无限等待 + return ch; +} + +/** + * @brief 重定向 c库函数 getchar,scanf到 DEBUG_USARTx + * 重定向 C 标准库 scanf 函数到串口 huart2 + * 注意以 空格 为结束 + * @param f + * @return int + * + * @usage scanf("%c", &RecData); + */ +GETCHAR_PROTOTYPE { + uint8_t ch = 0; + HAL_UART_Receive(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); + return ch; +} + +void delay_us(uint16_t us) { + uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us); + while (delay--); +} +#endif diff --git a/Core/Src/hcsr04.c b/Core/Src/hcsr04.c index 87950c4..4fa6432 100644 --- a/Core/Src/hcsr04.c +++ b/Core/Src/hcsr04.c @@ -1,12 +1,10 @@ #include "hcsr04.h" #include "tim.h" -#include -static uint64_t time; // 声明变量,用来计时 -static uint64_t time_end; // 声明变量,存储回波信号时间 -static int16_t delay_time = -1; // 用于延时 -uint8_t sonor_state; // 测距状态变量,0:未测量,1:正在测量,2:已测量 -uint32_t sonor_distance; // 测量出的距离,单位mm +static uint64_t time; // 声明变量,用来计时 +static uint64_t time_end; // 声明变量,存储回波信号时间 +uint8_t sonor_state; // 测距状态变量,0:未测量,1:正在测量,2:已测量 +uint32_t sonor_distance; // 测量出的距离,单位mm void delay_us(uint16_t us); void HCSR04_HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim); @@ -37,28 +35,12 @@ uint32_t sonar_mm(void) return sonor_distance; } -/// @brief 微秒级延时 -/// @param us 取值范围0-32767 -void delay_us(uint16_t us) -{ - if (us == 0) - return; - delay_time = us; - time = 0; - HAL_TIM_Base_Start_IT(&htim2); - while (time < us) - ; - delay_time = -1; -} - void HCSR04_HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if (htim == &htim2) { // 每10us自增一 ++time; - if (delay_us > 0 && time * 10 >= delay_time) - HAL_TIM_Base_Stop_IT(&htim2); } } diff --git a/Core/Src/main.c b/Core/Src/main.c index 3a69611..be73bb0 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -19,14 +19,15 @@ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "dma.h" +#include "gpio.h" #include "i2c.h" #include "tim.h" #include "usart.h" -#include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include +#include "syscalls.h" #include "bluetooth.h" #include "led.h" #include "hcsr04.h" @@ -109,22 +110,11 @@ int main(void) /* USER CODE BEGIN WHILE */ while (1) { - int Distance_mm = sonar_mm(); - int Distance_m = Distance_mm / 1000; - int Distance_m_p = Distance_mm % 1000; - printf("Distance_mm = %d\n", Distance_mm); - printf("Distance_m = %d\n", Distance_m); - printf("Distance_m_p = %d\n", Distance_m_p); + printf("test\n"); - if (Distance_mm < 100) + if (sonor_state == 0) { - LED_Start(LED_R); - MOTOR_Start(); - } - else - { - LED_Stop(LED_R); - MOTOR_Stop(); + sonar_mm(); } HAL_Delay(300); // 延时300毫秒 diff --git a/Core/Src/usart.c b/Core/Src/usart.c index a3f577c..fbdd64d 100644 --- a/Core/Src/usart.c +++ b/Core/Src/usart.c @@ -219,18 +219,5 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) } /* USER CODE BEGIN 1 */ -int fputc(int c, FILE* f) -{ - static uint8_t ch; - ch = c; - HAL_UART_Transmit(&huart1, &ch, 1, 1000); - return c; -} -int fgetc(FILE* f) -{ - static uint8_t ch; - HAL_UART_Receive(&huart1, &ch, 1, 0xffff); - return ch; -} /* USER CODE END 1 */