LED is a semiconductor device. In fact, everyone is familiar with LED. The indicator lights of various electrical appliances, mobile phone keyboard lights, LED LCD screen backlights, high-brightness LED flashlights, etc., all emit light through LED. LED has two main uses, one is as an indicator light; the other is lighting. LEDs used for lighting are generally high-power LEDs, which require a large voltage and current to work properly. What we are going to use here is a low-power LED used as an indicator light. Common low-power LEDs will emit light when a voltage of about 3V is added to its positive and negative poles. When it emits light normally, the current is about 2~5mA. It should be noted here that LEDs have positive and negative poles, and they will not emit light if connected in reverse. In addition, the voltage should not be too high, otherwise the LED will burn out.
/********************************************************************************
* @file bsp_led.c
* @author jianqiang.xue
* @version V1.0.0
* @date 2021-04-03
* @brief LED control
********************************************************************************/
/* 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 initialization enable pin clock configuration as push-free output
* @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 deinitialization configures the pin as a floating input
* @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 Light up the LED
* @note NULL
* @param ch: LED group number
* @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 Turn off LED
* @note NULL
* @param ch: LED group number
* @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 Flip LED
* @note NULL
* @param ch: LED group number
* @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 /* 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
Previous article:[MCU framework][bsp layer][cx32l003][bsp_key] KEY configuration and use
Next article:[MCU framework][bsp layer][cx32l003][bsp_flash] FLASH configuration and use
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 28335 floating point performance test
- What's your Chinese character of the year?
- [NXP Rapid IoT Review] + Rapid IoT Studio WEB/GUI programming tool learning and use
- [NXP Rapid IoT Review] + Unable to add devices again (Summary of the third week review)
- About atmel studio 7 to generate .lib library files
- Basic knowledge of power supply that you must know
- AD PCB cannot be saved, showing file save failed
- How to modify the AT32F4xx SRAM space size
- About software testing of commercial appliances
- EEWORLD University ---- Operating System Harbin Institute of Technology