From 57e7ee15e52358d530302b69aef87b98079a1a0f Mon Sep 17 00:00:00 2001 From: cirry <812852553@qq.com> Date: Tue, 11 Aug 2026 00:56:52 +0800 Subject: [PATCH] =?UTF-8?q?led=E6=B5=81=E6=B0=B4=E7=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BSP/Delay.c | 41 +++++++++++++++++++++++++++++++++++++++++ BSP/Delay.h | 8 ++++++++ CMakeLists.txt | 2 ++ User/main.c | 31 ++++++++++++++++++++++++++----- 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 BSP/Delay.c create mode 100644 BSP/Delay.h diff --git a/BSP/Delay.c b/BSP/Delay.c new file mode 100644 index 0000000..fbbc890 --- /dev/null +++ b/BSP/Delay.c @@ -0,0 +1,41 @@ +#include "stm32f10x.h" + +/** + * @brief 微秒级延时 + * @param xus 延时时长,范围:0~233015 + * @retval 无 + */ +void Delay_us(uint32_t xus) +{ + SysTick->LOAD = 72 * xus; //设置定时器重装值 + SysTick->VAL = 0x00; //清空当前计数值 + SysTick->CTRL = 0x00000005; //设置时钟源为HCLK,启动定时器 + while(!(SysTick->CTRL & 0x00010000)); //等待计数到0 + SysTick->CTRL = 0x00000004; //关闭定时器 +} + +/** + * @brief 毫秒级延时 + * @param xms 延时时长,范围:0~4294967295 + * @retval 无 + */ +void Delay_ms(uint32_t xms) +{ + while(xms--) + { + Delay_us(1000); + } +} + +/** + * @brief 秒级延时 + * @param xs 延时时长,范围:0~4294967295 + * @retval 无 + */ +void Delay_s(uint32_t xs) +{ + while(xs--) + { + Delay_ms(1000); + } +} diff --git a/BSP/Delay.h b/BSP/Delay.h new file mode 100644 index 0000000..cef7c3c --- /dev/null +++ b/BSP/Delay.h @@ -0,0 +1,8 @@ +#ifndef __DELAY_H +#define __DELAY_H + +void Delay_us(uint32_t us); +void Delay_ms(uint32_t ms); +void Delay_s(uint32_t s); + +#endif diff --git a/CMakeLists.txt b/CMakeLists.txt index 5de0473..5cd50a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,12 +29,14 @@ add_executable(${CMAKE_PROJECT_NAME} CMSIS/system_stm32f10x.c ${SPL_SRC} User/main.c + BSP/Delay.c ) target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE CMSIS # stm32f10x.h, core_cm3.h, system_stm32f10x.h StdPeriph_Driver/inc # stm32f10x_gpio.h 等外设头 User # stm32f10x_conf.h + BSP # delay.h ) target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE diff --git a/User/main.c b/User/main.c index 87dda32..7afd2eb 100644 --- a/User/main.c +++ b/User/main.c @@ -1,13 +1,34 @@ #include "stm32f10x.h" +#include "Delay.h" int main(void){ - RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13; + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_All; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(GPIOC, &GPIO_InitStruct); - // GPIO_SetBits(GPIOC, GPIO_Pin_13); - GPIO_ResetBits(GPIOC, GPIO_Pin_13); + GPIO_Init(GPIOA, &GPIO_InitStruct); + + // GPIO_SetBits(GPIOA, GPIO_Pin_0); // 关闭led灯 + // GPIO_ResetBits(GPIOA, GPIO_Pin_0); // 打开led灯 + // GPIO_WriteBit(GPIOA, GPIO_Pin_7, Bit_RESET); // 打开led灯 + // GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET); // 关闭led灯 + while(1){ + GPIO_Write(GPIOA, ~0x00000001); // 0000 0000 0000 0001 + Delay_ms(500); + GPIO_Write(GPIOA, ~0x00000002); // 0000 0000 0000 0010 + Delay_ms(500); + GPIO_Write(GPIOA, ~0x00000004); // 0000 0000 0000 0100 + Delay_ms(500); + GPIO_Write(GPIOA, ~0x00000008); // 0000 0000 0000 1000 + Delay_ms(500); + GPIO_Write(GPIOA, ~0x00000010); // 0000 0000 0001 0000 + Delay_ms(500); + GPIO_Write(GPIOA, ~0x00000020); // 0000 0000 0010 0000 + Delay_ms(500); + GPIO_Write(GPIOA, ~0x00000040); // 0000 0000 0100 0000 + Delay_ms(500); + GPIO_Write(GPIOA, ~0x00000080); // 0000 0000 1000 0000 + Delay_ms(500); } }