stm32CubeMX uses the HAL library to light the lamp, and uses the logic analyzer to observe the cycle

1. Experimental tools

The smallest core board of STM32CubeMX
Keil uVision5
mcuisp
STM32F103C8T6

2. STM32CubeMX generates code and uses HAL library to light up the water lamp

1. Introduction to STM32CubeMX

STM32CubeMX is a graphical configuration tool for STM32 chips strongly recommended by ST STMicroelectronics in recent years. The purpose is to facilitate developers and allow users to use graphical wizards to generate C initialization codes, which can greatly reduce development work, time and costs, and improve development efficiency. . STM32CubeMX covers almost all STM32 series chips.
On CubeMX, related configurations can be realized through foolish operations, and finally C language code can be generated to support multiple tool chains, such as MDK, IAR For ARM, TrueStudio, etc., saving us the time to configure various peripherals, greatly saves time.

2. Installation of STM32CubeMX

Official website download: https://www.st.com/zh/development-tools/stm32cubemx.html?sc=stm32cubemx#get-software
(Note: Official website download needs to bind email or register)
(1) Open the compressed package and run the installation Program, click next
insert image description here
(2) Click "I accept the terms of this license agreement", and then select Next:
insert image description here
(3) Check the first one, and the second option is whether to agree to ST's collection of your personal use information, etc. , click next:
insert image description here
(4) Select the installation location, the default location is to install in the C drive (note: the installation location should not appear in Chinese), click next: (5) Click
insert image description here
OK:
insert image description here
(6) Click NEXT directly, and start after other settings Installation:
insert image description here
(7) The installation is complete, click Done to exit:
insert image description here

2. Install the HAL library

Introduction to HAL:
STM32 HAL firmware library is the abbreviation of Hardware Abstraction Layer, and the Chinese name is: Hardware Abstraction Layer. The HAL library is the latest abstraction layer embedded software launched by ST for the STM32 MCU, in order to achieve the maximum portability across STM32 products more conveniently. With the launch of the HAL library, it can be said that ST has slowly abandoned the original standard firmware library, which also makes many old users dissatisfied. However, when the HAL library was launched, many third-party middleware were added, including RTOS, USB, TCP/IP, graphics, and so on.
Compared with the standard library, the HAL library of STM32 is more abstract. The ultimate goal of ST is to achieve seamless porting between STM32 series MCUs, and even fast porting among other MCUs.
And since 2016, ST company has gradually stopped updating the standard firmware library, and turned to the update of the HAL firmware library and the Low-layer underlying library, stopping the standard library update, which means that the use of STM32CubeMX to configure the HAL/ LL library is the mainstream configuration environment;
(1) Open the installed STMCubeMX
insert image description here
(2) Click HELP->Manage embedded software packages:
insert image description here
(3) A model selection interface will pop up and check the HAL library you want to install, click "Install Now” until the installation is successful. As shown below:
insert image description here

3. New project

(1) Go back to the main interface of STMCubeMX and create a new project:
insert image description here
(2) Select your own chip in the part name, click the specific chip information in the information column to select it, and click start project:
insert image description here
(3) Click system core to enter SYS, Select serial wire under debug:
insert image description here
(4) Configure the clock, enter the above rcc, there are two clocks, one is hse and lse, we want to use the GPIO interface, and these interfaces are in APB2: Next, observe the clock architecture
insert image description here
, The clock of the APB2 bus is controlled by hse, and at the same time, select PLLCLK on the right side of this interface:
insert image description here
(5) Set hse as Crystal/Ceramic Resonator:
insert image description here
(6) Click GPIO, and then click the corresponding pin to set the output register , that is the item of output, a total of three are selected, which are PA4, PB9, and PC15:
insert image description here
(7) Click project manager, configure your own path and project name, and then change the IDE item to MDK-ARM:
insert image description here
(8) Enter On the code generate interface, select to generate the initialization .c/.h file, then click generate code, select open project, and then go to KEIL5:
insert image description here

4.keil simulation debugging

(1) Open the .uvprojx file (or select open project in the previous step),
insert image description here
open the main.c file, and slip the part of the main function:
insert image description here
(2) Put the following code into the main function (replace the content inside)

SystemClock_Config();//系统时钟初始化
  MX_GPIO_Init();//gpio初始化
  while (1)
  {
    
    		
		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET);//PA4亮灯
		HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);//PB9熄灯
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);//PC15熄灯
		HAL_Delay(1000);//延时1s
		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);//PA4熄灯
		HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET);//PB9亮灯
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);//PC15熄灯
		HAL_Delay(1000);//延时1s		
		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);//PA4熄灯
		HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);//PB9熄灯
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_RESET);//PC15亮灯
		HAL_Delay(1000);//延时1s
	}

(3) Circuit connection
Connect the circuit according to the designed program:
for USB to TTL module and stm32f103c8t6 connection:
GND — GND
3v3 — 3v3
TXD — A10
RXD — A9

Total circuit:
Red——B9
Green——C15
Yellow——A4
(4) Burning and running
Serial port burning needs to be powered off and set boot0 to 0 to run normally:

LEAD TEST

(5) Observe the output waveform of the GPIO port
1. Select the magic wand, then click on the Target interface, and select the correct size of the crystal oscillator. I use an 8MHz external crystal oscillator. This option plays a very important role in software simulation. If the wrong choice is made, the waveform must be wrong because the time is not accurate.
insert image description here
2. Settings on the Debug page
insert image description here
3. Click Debug to enter the debug page
insert image description here
4. Select the logic analyzer:
insert image description here
5. Select the pin to be observed:
Click Setup Logic Analyzer:
insert image description here
Add the pin to be observed (note that you need to enter it yourself, such as viewing A4 pin is input to PORTA.4):
insert image description here
6. Related settings
insert image description here
insert image description here
7. Running program:
insert image description here
8. Observe the waveform:
insert image description here
the pin is low-level light on, high-level light is off, high-low level conversion cycle (LED flashing cycle ) is about 1s.

3. Summary

The visual interface provided by the HAL library greatly improves programming efficiency. Compared with using keil alone to write running lights, you have to write more related statements. Using HAL does not need to use code to configure pins, and the effect is very good.

4. References

https://blog.csdn.net/weixin_46129506/article/details/120780184
https://blog.csdn.net/qq_46467126/article/details/120847240

Guess you like

Origin blog.csdn.net/qq_55894922/article/details/127232999