介绍
单片机常用led驱动,可以十分方便的控制led闪烁频率和闪烁持续时间
移植教程
- 将 easy_led.c easy_led.h easy_led_port.c 添加到工程中
- 打开 easy_led_port.c 文件,自行实现里面的函数, 里面的函数主要是为了实现临界区保护
- 周期性的调用 void eled_ticks(void); 函数
使用说明
- led IO硬件初始化
- 初始化LED驱动并设置led默认的闪烁状态,注意闪烁状态的单位为ticks 也就是 void eled_ticks(void); 函数的执行周期
void eled_init(eLED_T * uLed, ledControl_t ledControl, uint16_t ledOnDefaultTicks, uint16_t ledOffDefaultTicks);
- 根据需要设置LED闪烁周期和持续时间,当持续时间结束后,LED会自动恢复到初始化时设置的默认闪烁状态
void eled_set(eLED_T * uLed, uint16_t ledOnTicks, uint16_t ledOffTicks, uint16_t keepTicks);
使用例子
eLED_T g_eled1, g_eled2, g_eled3, g_eled4;
void eled1_control(uint8_t level)
{
}
void eled2_control(uint8_t level)
{
}
void eled3_control(uint8_t level)
{
}
void eled4_control(uint8_t level)
{
}
int main(void)
{
eled_init(&g_eled1, eled1_control, 90, 10);
eled_init(&g_eled2, eled2_control, 100, 100);
eled_init(&g_eled3, eled3_control, 10, 10);
eled_init(&g_eled4, eled4_control, 50, 50);
eled_set(&g_eled4, 10, 10, 500);
eled_set(&g_eled3, 10, 90, 500);
while (1)
{
delay_ms(10);
eled_ticks();
}
}
easy_led.c
#include "easy_led.h"
static eLED_T * s_ledHead = 0;
extern void eled_port_init(void);
extern void eled_port_lock(void);
extern void eled_port_unlock(void);
static void eled_handler(eLED_T * eLed, uint16_t onTicks, uint16_t offTicks)
{
if(1 == eLed->ulTicks)
{
if(1 >= onTicks)
{
eLed->ledControl(0);
}
else
{
eLed->ledControl(1);
}
}
else if(eLed->ulTicks >= onTicks)
{
eLed->ledControl(0);
}
if(eLed->ulTicks >= offTicks)
{
eLed->ulTicks = 0;
}
}
void eled_init(eLED_T * eLed, ledControl_t ledControl, uint16_t ledOnDefaultTicks, uint16_t ledOffDefaultTicks)
{
if(0 == s_ledHead)
{
eled_port_init();
}
eled_port_lock();
eLed->ulTicks = 0;
eLed->ulLedOnDefaultTicks = ledOnDefaultTicks + 1;
eLed->ulLedOffDefaultTicks= ledOnDefaultTicks + ledOffDefaultTicks;
eLed->ulLedOnTicks = 0;
eLed->ulLedOffTicks = 0;
eLed->ulKeepTicks = 0;
eLed->ledControl = ledControl;
eLED_T * node = s_ledHead;
while(node)
{
if(node == eLed)
{
eled_port_unlock();
return;
}
else
{
node = node->ptNextLed;
}
}
eLed->ptNextLed = s_ledHead;
s_ledHead = eLed;
eled_port_unlock();
}
void eled_set(eLED_T * eLed, uint16_t ledOnTicks, uint16_t ledOffTicks, uint16_t keepTicks)
{
eled_port_lock();
eLed->ulTicks = 0;
eLed->ulLedOnTicks = ledOnTicks + 1;
eLed->ulLedOffTicks = ledOnTicks + ledOffTicks;
eLed->ulKeepTicks = keepTicks;
eled_port_unlock();
}
void eled_set_default(eLED_T * eLed, uint16_t ledOnDefaultTicks, uint16_t ledOffDefaultTicks)
{
eled_port_lock();
eLed->ulLedOnDefaultTicks = ledOnDefaultTicks + 1;
eLed->ulLedOffDefaultTicks= ledOnDefaultTicks + ledOffDefaultTicks;
eled_port_unlock();
}
void eled_stop(eLED_T * eLed)
{
eLED_T** curr;
for(curr = &s_ledHead; *curr; )
{
eLED_T* entry = *curr;
if (entry == eLed)
{
eled_port_lock();
*curr = entry->ptNextLed;
eled_port_unlock();
return;
}
else
curr = &entry->ptNextLed;
}
}
void eled_ticks(void)
{
eLED_T * target = s_ledHead;
eled_port_lock();
while(target)
{
if(target->ulTicks == TICKS_MAX)
{
target->ulTicks = 0;
}
target->ulTicks ++;
if(0 == target->ulKeepTicks)
{
eled_handler(target, target->ulLedOnDefaultTicks, target->ulLedOffDefaultTicks);
}
else
{
eled_handler(target, target->ulLedOnTicks, target->ulLedOffTicks);
target->ulKeepTicks --;
}
target = target->ptNextLed;
}
eled_port_unlock();
}
easy_led.h
#ifndef __EASY_LED_H
#define __EASY_LED_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define TICKS_MAX (0xFFFF)
typedef void (*ledControl_t)(uint8_t);
typedef struct eLED
{
uint16_t ulTicks;
uint16_t ulLedOnDefaultTicks;
uint16_t ulLedOffDefaultTicks;
uint16_t ulLedOnTicks;
uint16_t ulLedOffTicks;
uint16_t ulKeepTicks;
ledControl_t ledControl;
struct eLED * ptNextLed;
}eLED_T;
void eled_init(eLED_T * uLed, ledControl_t ledControl, uint16_t ledOnDefaultTicks, uint16_t ledOffDefaultTicks);
void eled_set(eLED_T * uLed, uint16_t ledOnTicks, uint16_t ledOffTicks, uint16_t keepTicks);
void eled_set_default(eLED_T * eLed, uint16_t ledOnDefaultTicks, uint16_t ledOffDefaultTicks);
void eled_stop(eLED_T * eLed);
void eled_ticks(void);
#ifdef __cplusplus
}
#endif
#endif
easy_led_port.c
#include "easy_led.h"
void eled_port_init(void)
{
}
void eled_port_lock(void)
{
}
void eled_port_unlock(void)
{
}
|