[cpp] view plain copy
#ifndef __BSP_LED_
#define __BSP_LED_
#include <MM32x103.h> // 这个换成STM32的库文件就行
#include "type.h"
//==================================================================================
#define LED1_RUN_GRP GPIOC
#define LED1_RUN_IDX GPIO_Pin_6
#define LED1_RUN_OFF() GPIO_ResetBits(LED1_RUN_GRP, LED1_RUN_IDX)
#define LED1_RUN_ON() GPIO_SetBits(LED1_RUN_GRP, LED1_RUN_IDX)
#define LED1_RUN_IS_OFF() GPIO_ReadOutputDataBit(LED1_RUN_GRP, LED1_RUN_IDX)
#define LED2_RUN_GRP GPIOA
#define LED2_RUN_IDX GPIO_Pin_11
#define LED2_RUN_OFF() GPIO_ResetBits(LED2_RUN_GRP, LED2_RUN_IDX)
#define LED2_RUN_ON() GPIO_SetBits(LED2_RUN_GRP, LED2_RUN_IDX)
#define LED2_RUN_IS_OFF() GPIO_ReadOutputDataBit(LED2_RUN_GRP, LED2_RUN_IDX)
#define LED3_RUN_GRP GPIOA
#define LED3_RUN_IDX GPIO_Pin_12
#define LED3_RUN_OFF() GPIO_ResetBits(LED3_RUN_GRP, LED3_RUN_IDX)
#define LED3_RUN_ON() GPIO_SetBits(LED3_RUN_GRP, LED3_RUN_IDX)
#define LED3_RUN_IS_OFF() GPIO_ReadOutputDataBit(LED3_RUN_GRP, LED3_RUN_IDX)
//==================================================================================
#define LED_COUNT 3 // LED个数自己定义
typedef enum
{
RED = 0x04,
GREEN = 0x02,
BLUE= 0x01
} led_t;
typedef struct
{
GPIO_TypeDef *GPIOx;
uint32_t pos;
} led_port_t;
//==================================================================================
void GPIOConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIOMode_TypeDef GPIO_Mode);
void LEDConfig((uint8_t value) // value:1/2/4 --- RED GREEN BLUE
void LEDSet(uint8_t value);
void LEDReset(uint8_t value);
void LEDToggle(uint8_t value);
#endif /* __BSP_LED_ */
[cpp] view plain copy
#include "bsp_led.h"
static led_port_t led_port[LED_COUNT] =
{
{LED1_RUN_GRP, LED1_RUN_IDX}, /*蓝灯*/
{LED2_RUN_GRP, LED2_RUN_IDX}, /*绿灯*/
{LED3_RUN_GRP, LED3_RUN_IDX}, /*红灯*/
};
// GPIO配置
void GPIOConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIOMode_TypeDef GPIO_Mode)
{
GPIO_InitTypeDef GPIO_InitStructure;//定义GPIO初始化结构体变量
// 这个GPIO口需要改的,根据自己情况而定
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOA, ENABLE);//开启GPIO时钟
//配置连接LED的GPIO为推挽输出模式
GPIO_InitStructure.GPIO_Pin=GPIO_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode;
GPIO_Init(GPIOx, &GPIO_InitStructure);
}
// LED GPIO配置
void LEDConfig((uint8_t value) // value:1/2/4 --- RED GREEN BLUE
{
uint8_t i, mask = 1;
for(i = 0; i < LED_COUNT; ++i)
{
if(value & mask)
{
GPIOConfig(led_port[i].GPIOx, led_port[i].pos, GPIO_Mode_Out_PP);
GPIO_ResetBits(led_port[i].GPIOx, led_port[i].pos);
}
mask <<= 1;
}
}
/* 根据参数来打开LED灯 */
void LEDSet(uint8_t value)
{
uint8_t i, mask = 1;
for(i = 0; i < LED_COUNT; ++i)
{
if(value & mask)
{
GPIO_SetBits(led_port[i].GPIOx, led_port[i].pos);
}
mask <<= 1;
}
}
/* 根据参数来关闭LED灯 */
void LEDReset(uint8_t value)
{
uint8_t i, mask = 1;
for(i = 0; i < LED_COUNT; ++i)
{
if(value & mask)
{
GPIO_ResetBits(led_port[i].GPIOx, led_port[i].pos);
}
mask <<= 1;
}
}
// 取反
void LEDToggle(uint8_t value)
{
uint8_t i, mask = 1;
for(i = 0; i < LED_COUNT; ++i)
{
if(value & mask)
{
if(GPIO_ReadOutputDataBit(led_port[i].GPIOx, led_port[i].pos) == 0)
{
GPIO_SetBits(led_port[i].GPIOx, led_port[i].pos);
}
else
{
GPIO_ResetBits(led_port[i].GPIOx, led_port[i].pos);
}
}
mask <<= 1;
}
}