LED是一种半导体器件,大家其实对LED并不陌生,各种电器的指示灯、手机键盘灯、LED液晶屏的背光、高亮LED手电筒等,都是通过LED发光的。LED有两种主要用途,一种是作为指示灯;另一种是照明。照明用的LED一般都是大功率LED,需要较大的电压电流才能正常工作。而这里我们要用到的则是用作指示灯的小功率LED。常见的小功率LED,在它的正负极加上3V左右的电压,就会发光,正常发光的时候,电流大概是2~5mA。这里要注意,LED有正负极之分,接反了不会发光。另外,电压也不可太高,那样会烧坏LED。
/********************************************************************************
* @file bsp_led.c
* @author jianqiang.xue
* @version V1.0.0
* @date 2021-04-03
* @brief LED控制
********************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "RTE_Components.h"
#include CMSIS_device_header
#include "bsp_gpio.h"
#include "bsp_led.h"
/* Private Includes ----------------------------------------------------------*/
#include "business_gpio.h"
#include "business_function.h"
/* Private Define ------------------------------------------------------------*/
#define LEDx_GPIO_CLK_ENABLE(__INDEX__) \
do \
{ \
if ((__INDEX__) == 0) \
BS_LED0_GPIO_CLK_ENABLE(); \
else if ((__INDEX__) == 1) \
BS_LED1_GPIO_CLK_ENABLE(); \
else if ((__INDEX__) == 2) \
BS_LED2_GPIO_CLK_ENABLE(); \
else if ((__INDEX__) == 3) \
BS_LED3_GPIO_CLK_ENABLE(); \
else if ((__INDEX__) == 4) \
BS_LED4_GPIO_CLK_ENABLE(); \
else if ((__INDEX__) == 5) \
BS_LED5_GPIO_CLK_ENABLE(); \
else if ((__INDEX__) == 6) \
BS_LED6_GPIO_CLK_ENABLE(); \
else if ((__INDEX__) == 7) \
BS_LED7_GPIO_CLK_ENABLE(); \
} while (0U)
/* Private Variables ---------------------------------------------------------*/
#if BS_LEDN != 0
static GPIO_Type *g_led_port[BS_LEDN] = {
#if (BS_LEDN > 0)
BS_LED0_GPIO_PORT
#endif
#if (BS_LEDN > 1)
,BS_LED1_GPIO_PORT
#endif
#if (BS_LEDN > 2)
,BS_LED2_GPIO_PORT
#endif
#if (BS_LEDN > 3)
,BS_LED3_GPIO_PORT
#endif
#if (BS_LEDN > 4)
,BS_LED4_GPIO_PORT
#endif
#if (BS_LEDN > 5)
,BS_LED5_GPIO_PORT
#endif
#if (BS_LEDN > 6)
,BS_LED6_GPIO_PORT
#endif
#if (BS_LEDN > 7)
,BS_LED7_GPIO_PORT
#endif
};
static const uint16_t g_led_pin[BS_LEDN] = {
#if (BS_LEDN > 0)
BS_LED0_PIN
#endif
#if (BS_LEDN > 1)
,BS_LED1_PIN
#endif
#if (BS_LEDN > 2)
,BS_LED2_PIN
#endif
#if (BS_LEDN > 3)
,BS_LED3_PIN
#endif
#if (BS_LEDN > 4)
,BS_LED4_PIN
#endif
#if (BS_LEDN > 5)
,BS_LED5_PIN
#endif
#if (BS_LEDN > 6)
,BS_LED6_PIN
#endif
#if (BS_LEDN > 7)
,BS_LED7_PIN
#endif
};
static const bool g_led_light_state[BS_LEDN] = {
#if (BS_LEDN > 0)
BS_LED0_LIGHT_LEVEL
#endif
#if (BS_LEDN > 1)
,BS_LED1_LIGHT_LEVEL
#endif
#if (BS_LEDN > 2)
,BS_LED2_LIGHT_LEVEL
#endif
#if (BS_LEDN > 3)
,BS_LED3_LIGHT_LEVEL
#endif
#if (BS_LEDN > 4)
,BS_LED4_LIGHT_LEVEL
#endif
#if (BS_LEDN > 5)
,BS_LED5_LIGHT_LEVEL
#endif
#if (BS_LEDN > 6)
,BS_LED6_LIGHT_LEVEL
#endif
#if (BS_LEDN > 7)
,BS_LED7_LIGHT_LEVEL
#endif
};
/* Public Function Prototypes -----------------------------------------------*/
/**
* @brief led初始化 使能引脚时钟 配置为推免输出
* @note
* @retval None
*/
void bsp_led_init(void)
{
uint8_t i;
for (i = 0; i < BS_LEDN; i++)
{
/* Enable the GPIO_LED Clock */
LEDx_GPIO_CLK_ENABLE(i);
/* Configure the GPIO_LED pin */
bsp_gpio_init_output(g_led_port[i], g_led_pin[i], BSP_GPIO_PIN_OUT_PP);
/* Reset PIN to switch off the LED */
bsp_gpio_set_pin(g_led_port[i], g_led_pin[i], (bsp_gpio_pin_state_t)!g_led_light_state[i]);
}
}
/**
* @brief led反初始化 将引脚配置为浮空输入
* @note NULL
* @retval None
*/
void bsp_led_deinit(void)
{
uint8_t i;
if (BS_LEDN == 0)
{
return;
}
for (i = 0; i < BS_LEDN; i++)
{
/* Turn off LED */
bsp_gpio_set_pin(g_led_port[i], g_led_pin[i], (bsp_gpio_pin_state_t)!g_led_light_state[i]);
/* DeInit the GPIO_LED pin */
bsp_gpio_deinit(g_led_port[i], g_led_pin[i]);
}
}
/**
* @brief 点亮LED
* @note NULL
* @param ch: LED组号
* @retval None
*/
void bsp_led_on(bsp_led_t ch)
{
if (ch >= BS_LEDN)
{
return;
}
bsp_gpio_set_pin(g_led_port[ch], g_led_pin[ch], (bsp_gpio_pin_state_t)!g_led_light_state[ch]);
}
/**
* @brief 关闭LED
* @note NULL
* @param ch: LED组号
* @retval None
*/
void bsp_led_off(bsp_led_t ch)
{
if (ch >= BS_LEDN)
{
return;
}
bsp_gpio_set_pin(g_led_port[ch], g_led_pin[ch], (bsp_gpio_pin_state_t)!g_led_light_state[ch]);
}
/**
* @brief 翻转LED
* @note NULL
* @param ch: LED组号
* @retval None
*/
void bsp_led_toggle(bsp_led_t ch)
{
if (ch >= BS_LEDN)
{
return;
}
bsp_gpio_set_toggle(g_led_port[ch], g_led_pin[ch]);
}
#else
void bsp_led_init(void)
{
}
void bsp_led_deinit(void)
{
}
void bsp_led_on(bsp_led_t ch)
{
}
void bsp_led_off(bsp_led_t ch)
{
}
void bsp_led_toggle(bsp_led_t ch)
{
}
#endif
/********************************************************************************
* @file bsp_led.h
* @author jianqiang.xue
* @version V1.0.0
* @date 2021-04-03
* @brief NULL
********************************************************************************/
#ifndef __BSP_LED_H
#define __BSP_LED_H
/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
/* Public enum ---------------------------------------------------------------*/
typedef enum
{
BSP_LED_0 = 0,
BSP_LED_1,
BSP_LED_2,
BSP_LED_3,
BSP_LED_4,
BSP_LED_5,
BSP_LED_6,
BSP_LED_7
} bsp_led_t;
/* Public Function Prototypes ------------------------------------------------*/
void bsp_led_init(void);
void bsp_led_deinit(void);
void bsp_led_on(bsp_led_t ch);
void bsp_led_off(bsp_led_t ch);
void bsp_led_toggle(bsp_led_t ch);
#endif