[STM32] tutorial 3 for comparison between standard library and HAL Library -- using library functions to configure GPIO to light LED lights

1, Foreword

This chapter mainly introduces how to use the standard library project and cubemx configuration project to make the LED light flash. You can directly click the directory to see what you want to see. The article tutorial can let you learn how to use the standard library project and cubemx to make the LED flash step by step.

2, Preparatory work

  • STM32 development board (I use Z200 series of STM32F103ZE in Puzhong)
  • Schematic diagram of stm32cupemx, Keil5 (MDK) and development board
  • Standard library project template

The explanation of STM32 chip GPIO can be seen in this article: [STM32] SMT32 standard library and HAL library comparison learning tutorial special part - GPIO details
For the creation of library function project templates, see this article: [STM32] STM32 standard library and HAL library comparison learning tutorial II - library function template creation
I have uploaded the standard library template project file to Baidu online disk. The online disk link is: https://pan.baidu.com/s/1Ok_wIMi1V1cVAHdGETPJzQ
Password: lob1

3, LED hardware circuit

Hardware schematic diagram to see their own development board

It can be seen that the two LEDs of the development board are controlled by PE5 and PB5 pins respectively. As can be seen from the schematic diagram, when the two pins are at low level, the LED of the LED is turned on, the LED is lit, and the LED is off at high level.

4, Use the standard library project to light the LED

1. GPIO initialization general steps

  • Turn on the clock of the corresponding GPIO pin.
  • Defines GPIO port structure variables.
  • Configure structure variables (set IO, set mode, set rate).
  • Initialize the GPIO function.

2. Introduction to GPIO standard library functions

(1) GPIO peripheral library files:
stm32f10x_gpio.c,
stm32f10x_gpio.h
(2) GPIO common standard library functions:

void GPIO_Init(GPIO_TypeDef* GPIOx,GPIO_InitTypeDef* GPIO_InitStruct)

Function: initialize the working mode and output speed of one or more IO ports (the same group of ports).

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

Function: set an IO port to high level (multiple IOS of the same port can be set at the same time). The bottom layer is by configuring the BSRR register.

void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

Function: set an IO port to low level (multiple IOS of the same port can be set at the same time). The bottom layer is by configuring the BSRR register.

void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, 
BitAction BitVal);
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);

Function: set the output level of port pin, rarely used.

uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t 
GPIO_Pin);

Function: read the input level of a pin in the port. The bottom layer is by reading the IDR register.

uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);

Function: read the input level of a group of ports. The bottom layer is by reading the IDR register.

uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t 
GPIO_Pin);

Function: read the output level of a pin in the port. The bottom layer is by reading the ODR register.

uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);

Function: read the output level of a group of ports. The bottom layer is by reading the ODR register.

(3) Enable GPIO clock function

void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);

3. Use the library function to program the LED light to flash

(1) Copy the template project, rename it library function, and turn on the LED.

(2) Create a new App folder in the project file to store peripheral files.

(3) Create a new LED folder in App to store led. c and. h files.

The advantage of this is to facilitate the migration of peripheral files

(4) Open the project, create a new file, save the format as. h, and write the LED initialization statement.




  • Procedure:
#ifndef LED_H_
#define LED_H_

#include "stm32f10x.h"

/* Macro definition LED clock port and pin definition */
#define LED0_GPIO_Port   GPIOB
#define LED0_Pin         GPIO_Pin_5
#define LED0_Port_RCC    RCC_APB2Periph_GPIOB

#define LED1_GPIO_Port   GPIOE
#define LED1_Pin         GPIO_Pin_5
#define LED1_Port_RCC    RCC_APB2Periph_GPIOE

void LED_Init(void);  //LED initialization function

#endif

(5) Create a new file, save the format as. c, and write the LED initialization function.



  • Procedure:
#include "LED.h"


/*************************************************
*Function name: LED_Init
*Function: LED lamp initialization function
*Input: None
*Return value: None
**************************************************/
void LED_Init()
{
	GPIO_InitTypeDef GPIO_InitStructure; //Define structure variables
	
	RCC_APB2PeriphClockCmd(LED0_Port_RCC|LED1_Port_RCC,ENABLE); //Enable clock
	
	GPIO_InitStructure.GPIO_Pin=LED0_Pin;           //Select and set the IO port of LED0
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;  //Set push-pull output mode
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;	//Set transmission rate
	GPIO_Init(LED0_GPIO_Port,&GPIO_InitStructure);  /* Initialize GPIO */
	GPIO_SetBits(LED0_GPIO_Port, LED0_Pin);         //Output high level, LED0 off
	
	GPIO_InitStructure.GPIO_Pin=LED1_Pin;           //Select and set the IO port of LED1
	GPIO_Init(LED1_GPIO_Port,&GPIO_InitStructure);  /* Initialize GPIO */
	GPIO_SetBits(LED1_GPIO_Port, LED1_Pin);         //Output high level, LED01 goes out
}

(6) Add the file in the directory and add the file path.



(7) Write the main function and compile the burn-in program.
Procedure:

#include "LED.h"

/*************************************************
*Function name: delay
*Function function: delay function, which occupies CPU through while loop to achieve the purpose of delay
*Input: i
*Return value: None
**************************************************/
void delay(u32 i)
{
	while(i--);
}

/*************************************************
*Function name: main
*Function function: main function
*Input: None  
*Return value: None
**************************************************/
int main()
{
	LED_Init();
	while(1)
	{
		GPIO_SetBits(LED0_GPIO_Port, LED0_Pin);
		GPIO_ResetBits(LED1_GPIO_Port, LED1_Pin);
		delay(6000000);
		GPIO_SetBits(LED1_GPIO_Port, LED1_Pin);
		GPIO_ResetBits(LED0_GPIO_Port, LED0_Pin);
		delay(6000000);
	}
}

  • After compilation, there are no errors and warnings, and the program is burned into the development board.

I use the serial port burning software provided by Puzhong

4. Experimental effect

5, Use cubemx to generate HAL library project and turn on the LED light

For some usage of cubemx, see: [STM32] STM32 standard library and HAL library comparison learning tutorial II - library function template creation
For the function of cubemx, the following configuration will not be introduced.

1. cubemx configuration generation project

(1) Open cubemx and click ACCESS TO MCU SELECTOR.

(2) Choose the chip of your own development board.

(3) Configure SYS.

(4) Configure RCC and select external clock.

(5) Configure PB5 and PE6 pins.



PE5 operation is the same

(6) Click GPIO configuration mode.

PE5 operation is the same

(7) Configure the clock tree.

For those who don't understand the clock tree, see this article: [STM32] STM32 standard library and HAL library comparison learning tutorial special chapter - system clock RCC

(8) Click Project Manager, configure the file and generate the project.


Important reminder: the path and project name should not be in Chinese


2. Introduction to HAL library control function

(1) GPIO peripheral library files:
stm32f10xx_hal_gpio.h,
stm32f10xx_hal_gpio.c
(2) Common HAL library functions for operating GPIO:

GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);

Function: read the level of one pin.

void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState);

Function: write the level of a pin. You can write high level or low level.

void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);

Function: flip the level of one pin.

3. Write a program to flash the LED

Procedure:

HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
HAL_Delay(500);  //ms level delay function of HAL Library
HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_RESET);
HAL_Delay(500);

0 error and 0 warning after compilation, burning to the development board.

4. Experimental effect


That's it!

Tags: Single-Chip Microcomputer stm32 ARM

Posted on Wed, 10 Nov 2021 14:53:42 -0500 by lisnaskea