From bc0a2d20344a19c2efd62c7c2b2a3636fe81af35 Mon Sep 17 00:00:00 2001 From: "lxbpxylps@126.com" Date: Mon, 11 Oct 2021 17:12:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20LED=20=E5=BA=93=E5=92=8C?= =?UTF-8?q?=E4=B8=B2=E5=8F=A3=E5=9B=9E=E6=98=BE=E6=97=B6=20LED=20=E9=97=AA?= =?UTF-8?q?=E7=83=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Hardware/led/led.c | 49 ++++++++++++++++++++++++++++++++++++++ Hardware/led/led.h | 26 ++++++++++++++++++++ Project/MasterNode.uvprojx | 9 ++++++- User/Main/main.c | 7 +++++- 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 Hardware/led/led.c create mode 100644 Hardware/led/led.h diff --git a/Hardware/led/led.c b/Hardware/led/led.c new file mode 100644 index 0000000..a866326 --- /dev/null +++ b/Hardware/led/led.c @@ -0,0 +1,49 @@ +/** + * @file led.c + * @author Myth + * @version 0.1 + * @date 2021.10.11 + * @brief stm32f103x6 LED 库 + */ + +#include "sys.h" +#include "systick.h" + +#include "led.h" + +/** + * @brief LED 初始化 + */ +void LED_Init(void) +{ + GPIO_InitTypeDef GPIO_Initure; + __HAL_RCC_GPIOC_CLK_ENABLE(); //开启 PA 时钟 + + GPIO_Initure.Pin = GPIO_PIN_13; //PC13 + GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP; //推挽输出 + GPIO_Initure.Pull = GPIO_PULLUP; //上拉 + GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH; //高速 + HAL_GPIO_Init(GPIOC, &GPIO_Initure); //初始化 PC13 + + LED1_Off; + SysTick_StartTimer(LED1_SYSTICK_TIMER_ID, 50); +} + +/** + * @brief LED1 慢速翻转,无阻塞,防止因调用过快导致无法观察 + * @param led_num: 序号 + */ +void LED_Slow_Toggle(uint8_t led_num) +{ + switch (led_num) + { + case 1: + { + if (SysTick_CheckTimer(LED1_SYSTICK_TIMER_ID)) + { + HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); + SysTick_StartTimer(LED1_SYSTICK_TIMER_ID, 50); + } + } + } +} diff --git a/Hardware/led/led.h b/Hardware/led/led.h new file mode 100644 index 0000000..4758da7 --- /dev/null +++ b/Hardware/led/led.h @@ -0,0 +1,26 @@ +/** + * @file led.h + * @author Myth + * @version 0.1 + * @date 2021.10.11 + * @brief stm32f103x6 LED 库 + * @details 提供 LED 开、关、闪烁、慢速闪烁功能 + * @note LED1: PC13 + */ + +#ifndef _LED_H +#define _LED_H + +#include "sys.h" + +#define LED1(n) (n ? HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET) : HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET)) +#define LED1_On LED1(0) +#define LED1_Off LED1(1) +#define LED1_Toggle HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13) //LED1 翻转 +#define LED1_SYSTICK_TIMER_ID 0 +#define LED1_Slow_Toggle LED_Slow_Toggle(1) //LED1 慢速翻转,无阻塞,防止因调用过快导致无法观察 + +void LED_Init(void); +void LED_Slow_Toggle(uint8_t led_num); + +#endif diff --git a/Project/MasterNode.uvprojx b/Project/MasterNode.uvprojx index 6cc4919..4c60cb0 100644 --- a/Project/MasterNode.uvprojx +++ b/Project/MasterNode.uvprojx @@ -339,7 +339,7 @@ USE_HAL_DRIVER, STM32F103x6 - ..\Core;..\Libraries\HAL_Lib\Inc;..\User\Main;..\System\sys;..\System\systick;..\System\uart + ..\Core;..\Libraries\HAL_Lib\Inc;..\User\Main;..\System\sys;..\System\systick;..\System\uart;..\Hardware\led @@ -657,6 +657,13 @@ Hardware + + + led.c + 1 + ..\Hardware\led\led.c + + diff --git a/User/Main/main.c b/User/Main/main.c index d8ed874..48191dd 100644 --- a/User/Main/main.c +++ b/User/Main/main.c @@ -5,13 +5,15 @@ * @date 2021.10.11 * @brief 工程主函数文件 * @details 初始化及主循环 - * @note 工程模版实现的功能:串口回显 + * @note 工程模版实现的功能:串口回显,回显时 PC13 上的 LED 闪烁 */ #include "sys.h" #include "systick.h" #include "uart.h" +#include "led.h" + void Echo(uint8_t byte); int main(void) @@ -24,6 +26,8 @@ int main(void) SysTick_Init(); //初始化 SysTick 和软件定时器 UART_Init(); //初始化串口 + LED_Init(); //初始化 LED + UART_BindReceiveHandle(COM1, Echo); //绑定 COM1 串口接收中断至 Echo 函数 while (1) @@ -40,5 +44,6 @@ int main(void) */ void Echo(uint8_t byte) { + LED1_Toggle; UART_SendChar(COM1, byte); }