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); }