From 560343cced3e5bb3426d3ddb9cdfd9e88e084d4f Mon Sep 17 00:00:00 2001 From: 423A35C7 <609514299@qq.com> Date: Tue, 8 Oct 2024 21:36:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E5=AE=9E=E9=AA=8C-?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A1=EF=BC=9A=E6=8A=8A=E5=BE=AE=E7=A7=92?= =?UTF-8?q?=E7=BA=A7=E5=BB=B6=E8=BF=9F=E6=94=BE=E5=88=B0main=E9=87=8C?= =?UTF-8?q?=EF=BC=8C=E7=94=A8for=E5=BE=AA=E7=8E=AF=E4=BA=A7=E7=94=9F10?= =?UTF-8?q?=E5=BE=AE=E7=A7=92=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exp1/Core/Inc/main.h | 4 ++-- exp1/Core/Src/main.c | 29 +++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/exp1/Core/Inc/main.h b/exp1/Core/Inc/main.h index 2c97d0a..c92ef5c 100644 --- a/exp1/Core/Inc/main.h +++ b/exp1/Core/Inc/main.h @@ -46,14 +46,14 @@ extern "C" { /* Exported macro ------------------------------------------------------------*/ /* USER CODE BEGIN EM */ - +#define SYSTICKCLK 170 // Systick Frequency 170MHz /* USER CODE END EM */ /* Exported functions prototypes ---------------------------------------------*/ void Error_Handler(void); /* USER CODE BEGIN EFP */ - +void delay_us(uint32_t nus); /* USER CODE END EFP */ /* Private defines -----------------------------------------------------------*/ diff --git a/exp1/Core/Src/main.c b/exp1/Core/Src/main.c index 56a05b5..ba0990f 100644 --- a/exp1/Core/Src/main.c +++ b/exp1/Core/Src/main.c @@ -56,7 +56,24 @@ void SystemClock_Config(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ - +void delay_us(uint32_t nus) { + uint32_t ticks; + uint32_t told, tnow, tcnt=0; + uint32_t reload = SysTick->LOAD; // LOAD的值 + ticks = nus * SYSTICKCLK; // 需要的节拍数 + told = SysTick->VAL; // 刚进入时的计数器值 + while (1) { + tnow = SysTick->VAL; + if (tnow != told) { + if (tnow < told) + tcnt += told - tnow; // SYSTICK是一个递减的计数器就可以了 + else + tcnt += reload - tnow + told; + told = tnow; + if (tcnt >= ticks) break; // 事件超过/等于要延迟的时间,则退出 + } + } +} /* USER CODE END 0 */ /** @@ -104,10 +121,14 @@ int main(void) } if (sSysTickTimer.bTimeOk) { sSysTickTimer.bTimeOk = 0; - HAL_GPIO_TogglePin(TickClock_LED_GPIO_Port, TickClock_LED_Pin); + // HAL_GPIO_TogglePin(TickClock_LED_GPIO_Port, TickClock_LED_Pin); } - // HAL_GPIO_TogglePin(TickClock_LED_GPIO_Port, TickClock_LED_Pin); - // HAL_Delay(100); + HAL_GPIO_TogglePin(TickClock_LED_GPIO_Port, TickClock_LED_Pin); + // HAL_Delay(500); + for (int i = 0; i < 50000; i++) { + delay_us(10); // 用for循环产生10微秒 + } + // delay_us(500000); } /* USER CODE END 3 */ }