区区钴钥金励志搓个库、框架。
根据我的注释提示即可进行器件的增删,其他部分不用管。
1、根据提示增删修改底层(GPIO配置)
2、中间层(接口函数与其他配置)不用动
3、根据需求调用接口函数编写应用层(业务逻辑)
led.c文件
#include "led.h"
/*需要配置的led的数量,这里不需要改喔*/
#define led_cfg_num sizeof(led_cfg)/sizeof(led_cfg[0])
可根据需求修改下方宏
#define led1_rcc RCC_APB2Periph_GPIOC //时钟
#define led1_port GPIOC //端口
#define led1_pin GPIO_Pin_0 //引脚
#define led2_rcc RCC_APB2Periph_GPIOC //时钟
#define led2_port GPIOC //端口
#define led2_pin GPIO_Pin_1 //引脚
#define led3_rcc RCC_APB2Periph_GPIOC //时钟
#define led3_port GPIOC //端口
#define led3_pin GPIO_Pin_2 //引脚
#define led4_rcc RCC_APB2Periph_GPIOC //时钟
#define led4_port GPIOC //端口
#define led4_pin GPIO_Pin_3 //引脚
#define led5_rcc RCC_APB2Periph_GPIOC //时钟
#define led5_port GPIOC //端口
#define led5_pin GPIO_Pin_4 //引脚
#define led6_rcc RCC_APB2Periph_GPIOC //时钟
#define led6_port GPIOC //端口
#define led6_pin GPIO_Pin_5 //引脚
#define led7_rcc RCC_APB2Periph_GPIOC //时钟
#define led7_port GPIOC //端口
#define led7_pin GPIO_Pin_6 //引脚
#define led8_rcc RCC_APB2Periph_GPIOC //时钟
#define led8_port GPIOC //端口
#define led8_pin GPIO_Pin_7 //引脚
//可参考以上格式修改或添加
/*添加完之后在 led_init() 函数里的 led_cfg[] 数组里进行注册*/
//...
/
/***************************************************************************/
//在以下区间定义开关灯函数/
static void led1_on(void)
{
/*根据电路图看是高电平点亮还是低电平点亮选择注释掉*/
// GPIO_SetBits(led1_port,led1_pin);//1点亮
GPIO_ResetBits(led1_port,led1_pin); //0点亮
}
static void led1_off(void)
{
/*根据电路图看是高电平熄灭还是低电平熄灭选择注释掉*/
GPIO_SetBits(led1_port,led1_pin); //1熄灭
// GPIO_ResetBits(led1_port,led1_pin); //0熄灭
}
static void led2_on(void)
{
/*根据电路图看是高电平点亮还是低电平点亮选择注释掉*/
// GPIO_SetBits(led1_port,led1_pin);//1点亮
GPIO_ResetBits(led2_port,led2_pin); //0点亮
}
static void led2_off(void)
{
/*根据电路图看是高电平熄灭还是低电平熄灭选择注释掉*/
GPIO_SetBits(led2_port,led2_pin); //1熄灭
// GPIO_ResetBits(led1_port,led1_pin); //0熄灭
}
static void led3_on(void)
{
/*根据电路图看是高电平点亮还是低电平点亮选择注释掉*/
// GPIO_SetBits(led1_port,led1_pin);//1点亮
GPIO_ResetBits(led3_port,led3_pin); //0点亮
}
static void led3_off(void)
{
/*根据电路图看是高电平熄灭还是低电平熄灭选择注释掉*/
GPIO_SetBits(led3_port,led3_pin); //1熄灭
// GPIO_ResetBits(led1_port,led1_pin); //0熄灭
}
static void led4_on(void)
{
/*根据电路图看是高电平点亮还是低电平点亮选择注释掉*/
// GPIO_SetBits(led1_port,led1_pin);//1点亮
GPIO_ResetBits(led4_port,led4_pin); //0点亮
}
static void led4_off(void)
{
/*根据电路图看是高电平熄灭还是低电平熄灭选择注释掉*/
GPIO_SetBits(led4_port,led4_pin); //1熄灭
// GPIO_ResetBits(led1_port,led1_pin); //0熄灭
}
static void led5_on(void)
{
/*根据电路图看是高电平点亮还是低电平点亮选择注释掉*/
// GPIO_SetBits(led1_port,led1_pin);//1点亮
GPIO_ResetBits(led5_port,led5_pin); //0点亮
}
static void led5_off(void)
{
/*根据电路图看是高电平熄灭还是低电平熄灭选择注释掉*/
GPIO_SetBits(led5_port,led5_pin); //1熄灭
// GPIO_ResetBits(led1_port,led1_pin); //0熄灭
}
static void led6_on(void)
{
/*根据电路图看是高电平点亮还是低电平点亮选择注释掉*/
// GPIO_SetBits(led1_port,led1_pin);//1点亮
GPIO_ResetBits(led6_port,led6_pin); //0点亮
}
static void led6_off(void)
{
/*根据电路图看是高电平熄灭还是低电平熄灭选择注释掉*/
GPIO_SetBits(led6_port,led6_pin); //1熄灭
// GPIO_ResetBits(led1_port,led1_pin); //0熄灭
}
static void led7_on(void)
{
/*根据电路图看是高电平点亮还是低电平点亮选择注释掉*/
// GPIO_SetBits(led1_port,led1_pin);//1点亮
GPIO_ResetBits(led7_port,led7_pin); //0点亮
}
static void led7_off(void)
{
/*根据电路图看是高电平熄灭还是低电平熄灭选择注释掉*/
GPIO_SetBits(led7_port,led7_pin); //1熄灭
// GPIO_ResetBits(led1_port,led1_pin); //0熄灭
}
static void led8_on(void)
{
/*根据电路图看是高电平点亮还是低电平点亮选择注释掉*/
// GPIO_SetBits(led1_port,led1_pin);//1点亮
GPIO_ResetBits(led8_port,led8_pin); //0点亮
}
static void led8_off(void)
{
/*根据电路图看是高电平熄灭还是低电平熄灭选择注释掉*/
GPIO_SetBits(led8_port,led8_pin); //1熄灭
// GPIO_ResetBits(led1_port,led1_pin); //0熄灭
}
//可参考以上格式修改或添加
/*添加完之后在下面的 led_on_pointer[] 和 led_off_pointer [] 数组里分别进行注册*/
//...
/
/*定义函数指针类型*/
typedef void(* led_function_pointer)(void);
//在这里的下面注册 led_on,只需函数名即可
static led_function_pointer led_on_pointer [] =
{
led1_on,
led2_on,
led3_on,
led4_on,
led5_on,
led6_on,
led7_on,
led8_on,
//可参考以上格式修改或添加
//...
};
//在这里的下面注册 led_off,只需函数名即可
static led_function_pointer led_off_pointer [] =
{
led1_off,
led2_off,
led3_off,
led4_off,
led5_off,
led6_off,
led7_off,
led8_off,
//可参考以上格式修改或添加
//...
};
/
下面这个函数不用改动喔!!!!///
//led任务:对外的接口函数
//参数1: led号,选择第几个led,这个是要按照上面的 函数指针数组里注册的顺序填写喔
//参数2: led的状态,给1亮,给0灭
//返回值: 返回值是一个函数指针,指向一个 返回值为 void 类型指针、参数为 void 的函数
void (* led_task(unsigned char led_num, unsigned char led_sta))(void)
{
if(led_sta)
return led_on_pointer[led_num];
else
return led_off_pointer[led_num];
}
/
void led_init(void)
{
//根据需求在该数组进行注册
led_cfg_t led_cfg[] =
{
/*时钟*/ /*端口*/ /*引脚*/
{led1_rcc, led1_port, led1_pin},
{led2_rcc, led2_port, led2_pin},
{led3_rcc, led3_port, led3_pin},
{led4_rcc, led4_port, led4_pin},
{led5_rcc, led5_port, led5_pin},
{led6_rcc, led6_port, led6_pin},
{led7_rcc, led7_port, led7_pin},
{led8_rcc, led8_port, led8_pin},
//.....
};
/*只需要在上面注册,下面的东西不要动喔*/
GPIO_InitTypeDef GPIO_InitStruct;
led_function_pointer led_action;
for(int i = 0; i<led_cfg_num; i++)
{
RCC_APB2PeriphClockCmd(led_cfg[i].Led_RCC,ENABLE);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin=led_cfg[i].Led_Pin;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(led_cfg[i].Led_Port,&GPIO_InitStruct);
//熄灭所有的led
led_action = led_task(i,0);
led_action();
}
}
led.h文件
#ifndef _LED_H_
#define _LED_H_
#include "system.h"
//led 配置结构体
typedef struct
{
uint32_t Led_RCC; //时钟
GPIO_TypeDef *Led_Port; //端口
uint16_t Led_Pin; //引脚
}led_cfg_t;
//LED初始化
void led_init(void);
//LED任务
void (* led_task(unsigned char led_num, unsigned char led_sta))(void);
#endif
main.c文件
#include "led.h"
#include "delay.h"
#include "system.h"
#include "usart.h"
//定义一个 函数指针数据类型 返回值为void 参数为 void
typedef void(* function_pointer)(void);
function_pointer led_action;
int main()
{
delay_init(72);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
USART1_Init(9600);
led_init();
while(1)
{
//初始化了8个灯,这个8是手动给的
for(int i = 0; i< 8; i++)
{
led_action = led_task(i,1);
led_action();
delay_ms(1000);
led_action = led_task(i,0);
led_action();
delay_ms(1000);
}
}
}