修改delay_us实现以及相关的超声波实现

This commit is contained in:
EN 2024-07-14 16:47:03 +08:00
parent c96a1e3f0d
commit e3e2a78fe0
4 changed files with 88 additions and 50 deletions

79
Core/Inc/syscalls.h Normal file
View File

@ -0,0 +1,79 @@
#ifndef __SYSCALLS_H
#define __SYSCALLS_H
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stm32f1xx_hal_uart.h>
#include <time.h>
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

View File

@ -1,10 +1,8 @@
#include "hcsr04.h"
#include "tim.h"
#include <stdio.h>
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
@ -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);
}
}

View File

@ -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 <stdio.h>
#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毫秒

View File

@ -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 */