typedef enum {
LED_ON,
LED_OFF,
}led_level_def;
typedef struct {
uint16_t last_time_show_cycle; // 状态查询时间
uint16_t set_time_cycle; // 循环时间
uint16_t set_last_time; // 上一次电平输出时间
uint8_t curr_number; // 当前
uint8_t next_number; // 下一个指示次数
void (*led_set)(led_level_def);
}led_para_def;
// LED 设置
void led_set_level(led_level_def level)
{
if(level == LED_ON){
GPIO_ResetBits(GPIOB, GPIO_Pin_13);
}
else{
GPIO_SetBits(GPIOB, GPIO_Pin_13);
}
}
// 参数初始化
led_para_def led_para =
{
.set_time_cycle = (uint16_t)-1,
.curr_number = 0,
.led_set = led_set_level,
};
// 调用频率小于 10 ms
void led_set_handle(led_para_def *p_led_para, uint32_t time)
{
// 设置 LED
if(((uint16_t)(time - p_led_para->set_last_time)) >= p_led_para->set_time_cycle) {
p_led_para->set_last_time = time;
if(p_led_para->curr_number) {
p_led_para->led_set(((p_led_para->curr_number & 1) == 1) ? LED_OFF : LED_ON);
p_led_para->curr_number--;
if(p_led_para->curr_number == 0) {
p_led_para->set_time_cycle = (uint16_t)-1;// 下一个周期不再进入
}
}
}
// 更新当前参数(1.5 s 更新一次)
if(((uint16_t)(time - p_led_para->last_time_show_cycle)) >= LED_TIME_CYCLE){
p_led_para->last_time_show_cycle = time;
if(!p_led_para->curr_number){
uint8_t number = p_led_para->next_number; // 获取当前指示次数
// 限制闪烁次数
if(number < LED_TIME_CYCLE / LED_TIME_OUTPUT / 2){
p_led_para->curr_number = number * 2 - 1;
p_led_para->set_last_time = time;
p_led_para->set_time_cycle = LED_TIME_OUTPUT;
p_led_para->led_set(LED_ON);
}
}
}
}
// number 闪烁次数
void led_show(led_para_def *p_led_para, uint8_t number)
{
p_led_para->next_number = number;
}
-THE END-
如果对你有帮助,记得转发分享哦
微信公众号「鱼鹰谈单片机」
每周一更单片机知识
长按后前往图中包含的公众号关注
鱼鹰,一个被嵌入式耽误的畅销书作家
个人微信「EmbeddedOsprey」
长按后打开对方的名片关注