#include "stm32f10x.h"

//定义LED0所在的GPIO端口
#define LED0_GPIO_PORT GPIOB
#define LED0_GPIO_PIN GPIO_Pin_5

//定义LED1所在的GPIO端口
#define LED1_GPIO_PORT GPIOB
#define LED1_GPIO_PIN GPIO_Pin_6

//定义LED2所在的GPIO端口
#define LED2_GPIO_PORT GPIOB
#define LED2_GPIO_PIN GPIO_Pin_7

//定义LED3所在的GPIO端口
#define LED3_GPIO_PORT GPIOB
#define LED3_GPIO_PIN GPIO_Pin_8

//定义LED4所在的GPIO端口
#define LED4_GPIO_PORT GPIOB
#define LED4_GPIO_PIN GPIO_Pin_9

//定义LED5所在的GPIO端口
#define LED5_GPIO_PORT GPIOB
#define LED5_GPIO_PIN GPIO_Pin_10

//定义LED6所在的GPIO端口
#define LED6_GPIO_PORT GPIOB
#define LED6_GPIO_PIN GPIO_Pin_11

//定义LED7所在的GPIO端口
#define LED7_GPIO_PORT GPIOB
#define LED7_GPIO_PIN GPIO_Pin_12

//定义LED8所在的GPIO端口
#define LED8_GPIO_PORT GPIOB
#define LED8_GPIO_PIN GPIO_Pin_13

//定义LED9所在的GPIO端口
#define LED9_GPIO_PORT GPIOB
#define LED9_GPIO_PIN GPIO_Pin_14

//定义LED10所在的GPIO端口
#define LED10_GPIO_PORT GPIOB
#define LED10_GPIO_PIN GPIO_Pin_15

//定义LED11所在的GPIO端口
#define LED11_GPIO_PORT GPIOC
#define LED11_GPIO_PIN GPIO_Pin_13

//定义LED12所在的GPIO端口
#define LED12_GPIO_PORT GPIOC
#define LED12_GPIO_PIN GPIO_Pin_14

//定义LED13所在的GPIO端口
#define LED13_GPIO_PORT GPIOC
#define LED13_GPIO_PIN GPIO_Pin_15

//定义LED14所在的GPIO端口
#define LED14_GPIO_PORT GPIOD
#define LED14_GPIO_PIN GPIO_Pin_2

//定义LED15所在的GPIO端口
#define LED15_GPIO_PORT GPIOD
#define LED15_GPIO_PIN GPIO_Pin_3

//定义LED所在的GPIO端口数组
GPIO_TypeDef* LED_GPIO_PORT[16] = {LED0_GPIO_PORT, LED1_GPIO_PORT, LED2_GPIO_PORT, LED3_GPIO_PORT, LED4_GPIO_PORT, LED5_GPIO_PORT, LED6_GPIO_PORT, LED7_GPIO_PORT, LED8_GPIO_PORT, LED9_GPIO_PORT, LED10_GPIO_PORT, LED11_GPIO_PORT, LED12_GPIO_PORT, LED13_GPIO_PORT, LED14_GPIO_PORT, LED15_GPIO_PORT};

//定义LED所在的GPIO引脚数组
uint16_t LED_GPIO_PIN[16] = {LED0_GPIO_PIN, LED1_GPIO_PIN, LED2_GPIO_PIN, LED3_GPIO_PIN, LED4_GPIO_PIN, LED5_GPIO_PIN, LED6_GPIO_PIN, LED7_GPIO_PIN, LED8_GPIO_PIN, LED9_GPIO_PIN, LED10_GPIO_PIN, LED11_GPIO_PIN, LED12_GPIO_PIN, LED13_GPIO_PIN, LED14_GPIO_PIN, LED15_GPIO_PIN};

//定义LED状态数组
uint8_t LED_Status[16] = {0};

//定义LED初始化函数
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
uint8_t i;

//使能GPIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);

//配置GPIO为推挽输出模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

for(i=0; i<16; i++)
{
GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN[i];
GPIO_Init(LED_GPIO_PORT[i], &GPIO_InitStructure);
}
}

//定义LED点亮函数
void LED_On(uint8_t num)
{
if(num < 16)
{
LED_GPIO_PORT[num]->BSRR = LED_GPIO_PIN[num];
LED_Status[num] = 1;
}
}

//定义LED熄灭函数
void LED_Off(uint8_t num)
{
if(num < 16)
{
LED_GPIO_PORT[num]->BRR = LED_GPIO_PIN[num];
LED_Status[num] = 0;
}
}

//定义LED反转函数
void LED_Toggle(uint8_t num)
{
if(num < 16)
{
if(LED_Status[num] == 0)
{
LED_On(num);
}
else
{
LED_Off(num);
}
}
}