文件说明
- bsp_led.c负责LED的初始化配置
- bsp_led.h负责外设LED函数的声明,以及宏定义,提高移植性
- main.c实现功能点亮LED
代码
1.mai.c
#include "stm32f4xx.h"
#include "bsp_led.h"
int main(void)
{
while(1)
{
LED_GPIO_Config();
}
}
2.bsp_led.c
#include "bsp_led.h"
void LED_GPIO_Config(void)
{
//定义一个结构体指针对象 GPIO_InitStruct来调用成员
GPIO_InitTypeDef GPIO_InitStruct;
//0-打开系统时钟(因为STM32F429默认时钟是关闭的,需要打开才能驱动外设)
RCC_AHB1PeriphClockLPModeCmd(LED_GPIO_CLK,ENABLE);
/*
*下面写好的的配置函数都存放在头文件里面,可利用ctrl+f查找
*/
//1-设置引脚
GPIO_InitStruct.GPIO_Pin = LED_GPIO_PIN;
//2-设置为输出模式
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
//3-设置为推挽输出类型
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
//4-设置为上拉
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
//5-设置速度50MHZ
GPIO_InitStruct.GPIO_Speed = GPIO_Fast_Speed;
//把引脚写进寄存器的函数,第二个参数传进去的是指针,需要加取址符
GPIO_Init(LED_GPIO_PORT,&GPIO_InitStruct);
}
3.bsp_led.h
#ifndef __LED_H //防止重定义
#define __LED_H
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
void LED_GPIO_Config(void); //进行函数声明
//对端口,引脚和时钟进行宏定义,方便移植,仅需要修改此处即可
#define LED_GPIO_PIN GPIO_Pin_10
#define LED_GPIO_PORT GPIOH
#define LED_GPIO_CLK RCC_AHB1Periph_GPIOH
#endif /*__LED_H*/