代码拉取完成,页面将自动刷新
轻量级的LED控制软件框架,可以十分方便地控制LED的各种状态,甚至多个LED组合控制,如跑马灯等。
轻量级的LED控制模块代码:
cotLed_Ctrl
调用周期为1毫秒(优先级需要最高,或者定时器调度效果最好)cot_led.c 和 cot_led.h
实现具体功能,对外提供的 API 接口函数
cotLed_Init
,添加所有指示灯
需要实现写入IO状态的函数,然后初始化结构体的
pfnLedCtrl
即可。
cotLed_Ctrl
, 入参为毫秒级的系统时长,用来具体控制LED状态不支持重复调用,重复调用设置函数相关信息会复位
typedef enum
{
LED_0 = 0,
LED_1,
LED_2,
/* 勿删除,用来统计LED的数目 */
LED_MAX_NUM
} LedType_e;
static void InitLedIo(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_5);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE, GPIO_Pin_5);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD, GPIO_Pin_5);
}
void CtrlLed1(cotLedState_e state)
{
state == COT_LED_ON ? GPIO_ResetBits(GPIOB, GPIO_Pin_5) : GPIO_SetBits(GPIOB, GPIO_Pin_5);
}
void CtrlLed2(cotLedState_e state)
{
state == COT_LED_ON ? GPIO_ResetBits(GPIOE, GPIO_Pin_5) : GPIO_SetBits(GPIOE, GPIO_Pin_5);
}
void CtrlLed3(cotLedState_e state)
{
state == COT_LED_ON ? GPIO_SetBits(GPIOD, GPIO_Pin_5) : GPIO_ResetBits(GPIOD, GPIO_Pin_5);
}
void FML_LED_Init(void)
{
static cotLedCfg_t s_ledTable[LED_MAX_NUM] = {
{.pfnLedCtrl = CtrlLed1},
{.pfnLedCtrl = CtrlLed2},
{.pfnLedCtrl = CtrlLed3},
};
InitLedIo();
cotLed_Init(s_ledTable, LED_MAX_NUM);
}
周期调用函数 cotLed_Ctrl
,入参为毫秒累计计时。
void LedTask(void)
{
uint32_t time;
while (1)
{
cotLed_Ctrl(GetTimerMs());
cotOs_WaitFor(1);
}
}
// 或者
int main(void)
{
int time1ms_cnt = 0;
while(1)
{
if (flag_1ms)
{
flag_1ms = 0;
cotLed_Ctrl(time1ms_cnt);
time1ms_cnt++;
}
}
}
/************ 普通模式 ***********/
cotLed_SetState(LED_0, COT_LED_OFF); // 设置灯灭
cotLed_SetState(LED_0, COT_LED_ON); // 设置灯亮
cotLed_SetStateWithTime(LED_0, COT_LED_ON, 1000); // 设置灯亮持续一秒
/************ 闪烁模式 ***********/
cotLed_Twinkle(LED_0, 500); // 500毫秒进行闪烁(亮灭间隔时间)
cotLed_TwinkleWithCount(LED_0, 500, 3, COT_LED_OFF); // 500毫秒进行闪烁3次(一亮一灭为一次),次数完成后灯灭
/************ 呼吸灯模式(软件模拟PWM) ***********/
cotLed_Breathe(LED_0, 2000); // 2秒完成一次呼吸亮灭操作
cotLed_BreatheWithCount(LED_0, 2000, 3, COT_LED_OFF); // 2秒完成一次呼吸亮灭操作,总共3次,次数完成后灯灭
/************ 自定义模式 ***********/
// 2 秒内 3 次快闪,总共5次,次数完成后灯灭
cotLed_CustomWithCount(LED_0, 5, COT_LED_OFF, 100, -100, 100, -100, 100, -100, -1400, 0);
// 2 秒内 1 次快闪,无限制次数
cotLed_Custom(LED_0, 100, -1900, 0);
/************ 多个LED控制模式 ***********/
IoLedType_e led[] = {LED_0, LED_1, LED_2};
cotLed_Waterfall(led, 3, 300); // 流水灯,不限次数
cotLed_MarqueeWithCount(led, 3, 500, 3, COT_LED_OFF); // 跑马灯执行3次
代码链接:stm32 工程
const_zpc@163.com
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。