在线时间400 小时
UID2075309
注册时间2014-2-25
NXP金币56
TA的每日心情 | 开心 2019-10-17 08:07 |
---|
签到天数: 273 天 [LV.8]以坛为家I
金牌会员
- 积分
- 7359
- 最后登录
- 2021-8-30
|
发这种帖子的太多了,但是希望你还是看看!我也是记个笔记
/**
* \brief LED信息
*/
typedef struct am_led_info {
uint32_t pin; /**< \brief LED占用的引脚 */
bool_t active_low; /**< \brief LED点亮是否低电平*/
} am_led_info_t;
/** \brief LED 索引ID号 */
#define LED0 0
#define LED1 1
#define LED2 2
#define LED3 3
#define LED4 4
#define LED5 5
#define LED6 6
#define LED7 7
const am_led_info_t g_led_info[] = {//把板上所有的LED引脚都写到这个数组里面
{PIO0_7, TRUE},
{PIO0_13, TRUE},
{PIO0_16, TRUE},
{PIO0_17, TRUE},
{PIO0_19, TRUE},
{PIO0_27, TRUE},
{PIO0_28, TRUE},
{PIO0_18, TRUE},
};
const uint32_t g_led_num = sizeof(g_led_info) / sizeof(g_led_info[0]);//获取板子上有多少个LED灯
/*******************************************************************************
公共函数
*******************************************************************************/
/**
* \brief 初始化LED
*/
void am_led_init (const am_led_info_t *p_led_info, const uint32_t led_num)
{
int i;
if (p_led_info != NULL && led_num != 0) {
__pg_led_info = (am_led_info_t *)p_led_info;
__g_led_num = (uint32_t)led_num;
}
for (i = 0; i < led_num; i++) {
am_gpio_pin_cfg(p_led_info.pin, AM_GPIO_OUTPUT_INIT_HIGH);//引脚功能配置
/* 熄灭LED */
am_gpio_set(p_led_info.pin,
(int)p_led_info.active_low);
}
}
/**
* \brief 设置LED状态
*/
void am_led_set (uint8_t led_id, bool_t state)
{
if (led_id < __g_led_num) {
state = (bool_t)(state ^ (__pg_led_info[led_id].active_low));
am_gpio_set(__pg_led_info[led_id].pin, (int)state);
}
}
/**
* \brief 点亮LED
*/
void am_led_on (uint8_t led_id)
{
if (led_id < __g_led_num) {
if (__pg_led_info[led_id].active_low == TRUE) {
am_gpio_set(__pg_led_info[led_id].pin, (int)FALSE);
} else {
am_gpio_set(__pg_led_info[led_id].pin, (int)TRUE);
}
}
}
/**
* \brief 熄灭LED
*/
void am_led_off (uint8_t led_id)
{
if (led_id < __g_led_num) {
if (__pg_led_info[led_id].active_low == TRUE) {
am_gpio_set(__pg_led_info[led_id].pin, (int)TRUE);
} else {
am_gpio_set(__pg_led_info[led_id].pin, (int)FALSE);
}
}
}
/**
* \brief 获取指定LED的状态
*/
bool_t am_led_get (uint8_t led_id)
{
bool_t state;
if (led_id < __g_led_num) {
state = (bool_t)__pg_led_info[led_id].active_low;
state ^= (bool_t)am_gpio_get(__pg_led_info[led_id].pin);
return state;
}
return FALSE;
}
/**
* \brief 翻转指定LED的状态
*/
void am_led_toggle (uint8_t led_id)
{
if (led_id < __g_led_num) {
am_gpio_toggle(__pg_led_info[led_id].pin);
}
}
调用公共函数:
am_led_init(g_led_info, g_led_num);//初始化所有的LED
主函数,让所有的灯都闪烁起来:
while (1) {
am_led_on(LED0);am_led_on(LED1);am_led_toggle(LED2);am_led_toggle(LED3);
am_led_on(LED4);am_led_on(LED5);am_led_toggle(LED6);am_led_toggle(LED7);
am_mdelay(300);
am_led_off(LED0);am_led_off(LED1);am_led_toggle(LED2);am_led_toggle(LED3);
am_led_off(LED4);am_led_off(LED5);am_led_toggle(LED6);am_led_toggle(LED7);
am_mdelay(300);
}
点灯程序.rar
(195 Bytes, 下载次数: 106)
|
|