add delay ms safe & add led heart beat

This commit is contained in:
2025-08-13 00:56:55 +08:00
parent 1dde17d211
commit e9a96aec6a
6 changed files with 68 additions and 2 deletions

View File

@@ -1,5 +1,42 @@
#include "led.h"
/**
* @brief LED心跳指示灯功能
* @details 实现类似心跳的LED闪烁模式快闪两次然后暂停
* 适合在SysTick中断中调用通过计数器控制闪烁节拍
* @note 假设SysTick中断频率为1ms心跳周期约为2秒
* 心跳模式亮200ms->灭200ms->亮200ms->灭1400ms循环
*/
void led_heart_beat(void)
{
static uint16_t heart_beat_counter = 0;
// 心跳周期2000ms (假设SysTick为1ms中断)
// 模式亮200ms -> 灭200ms -> 亮200ms -> 灭1400ms
heart_beat_counter++;
if (heart_beat_counter <= 200) {
// 第一次亮0-200ms
led_on();
}
else if (heart_beat_counter <= 400) {
// 第一次灭200-400ms
led_off();
}
else if (heart_beat_counter <= 600) {
// 第二次亮400-600ms
led_on();
}
else if (heart_beat_counter <= 2000) {
// 长时间灭600-2000ms
led_off();
}
else {
// 重置计数器,开始新的心跳周期
heart_beat_counter = 0;
}
}
void led_init(void) {
rcu_periph_clock_enable(LED_RCU);
gpio_mode_set(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN);