Firmware Library Lights LED
Prerequisites to build a firmware library engineering template can refer to the following blog
https://blog.csdn.net/cainaiolin/article/details/52012786
Hardware foundation
There are three led lights on my development board. I choose to light PB5.
Say little about the code: Create a new file named "bsp_led.h"
The abbreviation of BoardSupport Packet (Board Support Packet) is often used in files defined by users themselves.
#ifndef __BSP_LED_H #define __BSP_LED_H //R red light /*-----LED GPIO port of lamp, GPIO pin number and GPIO port clock*/ #define LED1_GPIO_PORT GPIOB #define LED1_GPIO_CLK RCC_APB2Periph_GPIOB #define LED1_GPIO_PIN GPIO_Pin_5 /*IO Control by Direct Operating Register*/ #define DiitalHi (p, i) {p-> BSRR = i;}// Output High Level #define DiitalLo (p, i) {p-> BRR = i;}// Output is low level #define digitalToggle(p,i) {p-> ODR ^= i;} // The output is in an abnormal state or /*Define macros that control IO*/ #define LED1_TOGGLE digitalToggle(LED1_GPIO_PORT,LED1_GPIO_PIN) #define LED1_OFF digitalHi(LED1_GPIO_PORT,LED1_GPIO_PIN) #define LED1_ON digitalLo(LED1_GPIO_PORT,LED1_GPIO_PIN) /*LED_GPIO Initialization function*/ void LED_GPIO_Config(void) { /*Define a structure of type GPIO_InitTypeDef*/ GPIO_InitTypeDef GPIO_InitStructure; /*Turn on the relevant GPIO peripheral clock of LED*/ RCC_APB2PeriphClockCmd(LED1_GPIO_CLK,ENABLE); /*Selection of GPIO pins to be controlled*/ GPIO_InitStructure.GPIO_Pin = LED1_GPIO_PIN; /*Set pin mode to universal push-pull output mode*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*Set the output rate to 50MHz*/ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /*Select pins to be controlled*/ GPIO_InitStructure.GPIO_Pin = LED1_GPIO_PIN; /*Initialize the GPIO port by calling the GPIO_Init function*/ GPIO_Init(LED1_GPIO_PORT,&GPIO_InitStructure); /*Turn off all lights*/ GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN); } #endif
Interpretation of Initialization Function Step of LED_GPIO_Config
1. Define a variable of type GPIO_InitTypeDef, which contains three variables uint16_t GPIO_Pin; GPIOSpeed_TypeDef GPIO_Speed; GPIOMode_TypeDef GPIO_Mode set pins, output rate and mode selection respectively.
2. Clocks that turn on relevant GPIO ports
3. Assignment of variables of type GPIO_InitTypeDef
4. Initialize the GPIO port by calling the GPIO_Init function
5.LED is turned off by default
Main function:main
#include"stm32f10x.h" #include"bsp_led.h" /* *@brief Main program *@param None *@retval Nane */ int main(void) { LED_GPIO_Config(); while(1) { LED1_ON ; } }