文件說明
- 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*/