[STM32F103 running water lamp lighting version 1]

?

Foreword
STM32CubeMX is a graphical configuration tool launched by STMicroelectronics. Relevant configurations can be achieved through fool-proof operations. It can ultimately generate C language code and supports multiple tool chains, such as MDK, IAR For ARM, TrueStudio, etc. It is particularly worth mentioning that TrueStudio has been acquired by ST and provides a completely free version. Through plug-in installation, STM32CubeMX can be integrated into an IDE, which is very convenient to use.

1. Installation Tutorial

  1. STM32CubeMX Introduction
    STM32CubeMX is a graphical configuration tool for STM32 chips that STMicroelectronics has strongly recommended in recent years. Its purpose is to facilitate developers and allow users to use graphical wizards to generate C initialization code, which can greatly reduce development work, time and costs, and improve development efficiency. . STM32CubeMX covers almost the entire range of STM32 chips
    On CubeMX, relevant configurations can be achieved through fool-proof operations, and finally C language code can be generated, supporting a variety of tool chains, such as MDK, IAR For ARM, TrueStudio, etc., which saves us the time of configuring various peripherals, which greatly saves time

A total of three are required to install CubeMx
JRE (Java Runtime Environment): Java runtime environment, a collection of environments necessary to run JAVA programs

STM32CubeMX

HAL library: STM32HAL firmware library, another library officially launched by ST

  1. Install JRE
    The STM32CubeMX software runs based on the JAVA environment, so you need to install JRE (Java Runtime Environment) to use it. Remember to choose a 64-bit installation.
    Installation address: https://www.java.com/en/download/manual.jsp

Double-click the application package to install:

Modify the path and click Next:

Wait for the installation to be successful:


  1. Install STM32CubeMX 6.4.0
    Baidu Netdisk: https://pan.baidu.com/s/1yKgxQk2vxyqAy4CrGHZYzA?pwd=8m4m
    Extraction code: 8m4m

If you don’t want to download from Baidu Netdisk, you can go to the official website to download:
Official website address: www.st.com/stm32cubemx

Double-click the application file to install:

Agree to the agreement, next step:

Just check the first one (the second option is whether you agree to ST’s collection of your personal use information, etc.):

Custom installation path:
Click YES (this sentence means that installing this software may conflict with the files in the folder, causing the files in the folder to be lost, just click Continue):

Just click NEXT, no other settings are needed, and then start the installation:

When the installation is complete, click Done to exit:

  1. Install HAL library
    4.1 What is HAL firmware library?
    STM32 HAL firmware library is the abbreviation of Hardware Abstraction Layer. The Chinese name is: Hardware Abstraction Layer
    The HAL library is ST’s latest abstraction layer embedded software for STM32 MCUs, making it easier to achieve maximum portability across STM32 products. With the launch of the HAL library, it can be said that ST has slowly abandoned the original standard firmware library, which has also made many old users dissatisfied. However, when the HAL library was launched, a lot of third-party middleware was also added, including RTOS, USB, TCP/IP and graphics, etc.
    Compared with the standard library, the STM32 HAL library is more abstract. The ultimate goal of ST is to achieve seamless porting between STM32 series MCUs, and even to achieve rapid porting to other MCUs.
    And since 2016, ST has gradually stopped updating the standard firmware library, preferring to update the HAL firmware library and Low-layer underlying library, and stopped updating the standard library, which means that STM32CubeMX will be used to configure HAL/ The LL library is the mainstream configuration environment
    4.2 HAL library installation method
    Online installation
    Offline installation
    4.3 HAL library online installation
    Open the installed STM32CubeMX software and click Help -> Manage embedded software packages:

Find STM32F4 and click the triangular arrow:
Purchase the latest firmware package:
Click “Install Now” to perform online installation, as shown below:

start installation:

The box turns green, indicating that the firmware package was successfully installed:

2. Preparation

  1. Question requirements
    Install stm32CubeMX, use cubemx to complete the initialization process, and use HAL library programming to implement: Under the STM32 minimum system core (STM32F103C8T6), use the three pins GPIOA-5, GPIOB-9, and GPIOC-14 to control the LED lights (the highest clock 2Mhz), flashing in turn, with an interval of 1 second
    Use software simulation to run the above code under Keil, and use a virtual logic analyzer to observe the output waveform (high-low level conversion) on the corresponding pin to see if it is a 1 second period
    You can refer to my previous blog to learn how to write the running water lamp code:
    https://blog.csdn.net/qq_53112972/article/details/127153401
  2. Experimental tools
    STM32CubeMX 6.4.0,keil C51
  3. STM32CubeMX 6.4.0 environment introduction
    Operation key introduction:

Peripheral configuration:
① System Core: used to configure GPIO, clock source, interrupt system and system-related peripherals
② Analog: used to configure analog/digital and digital/analog conversion peripherals
③ Timers: used to configure timers and real-time clock peripherals
④ Connectivity: used to configure connection peripherals such as I2C, SPI and UART
⑤ Multimedia: used to configure audio data transmission peripherals such as I2S
⑥ Computing: used to configure CRC verification peripherals
⑦Middleware: used to configure middleware such as RTOS and GUI

GPIO configuration:

Clock configuration:

3. Project Creation

  1. New Construction
    Open STMCubeMX, click ACCEE TO MCU SELECTOR, and create a project:

Select your own chip in Part Name:

This experiment uses the STM32F103C8 development board. Click on the specific chip information in the information bar to select:

Click Start Project and you can see the chip of your choice:

  1. Pin configuration
    The development board interface is divided into pin preview and system preview:

Pin preview: You can view what functions are configured for the pins and the location of each pin; click on any pin to set the various functions of the pin. Green indicates successful configuration
System preview: You can view the status of various configured peripherals and GPIOs.

When configuring the pins, you only need to set the corresponding pin of the target LED to GPIO_Output. According to the question requirements, choose here: PA5, PB9, PC14

  1. Peripheral configuration
    Type system core, enter SYS, and select serial wire under debug:

Click RCC and set HSE to Crystal/Ceramic Resonator:

Configure GPIO:

GPIO output level: Low initializes the serial port to a low level. Of course, you can also choose High, which means to pull the initial level high. According to the connection method of our circuit, it should be pulled low;
GPIO mode: Outpull Pushpull means selecting the output mode as push-pull output mode, and there are several other output modes;
GPIO Pull-up/Pull-down: No pull-up and no pull-down means that the output is set to have no pull-up and pull-down output. In fact, from an logic perspective, we need to give our LED a pull-down resistor before it can light up. Yes, but in actual application, for simplicity, it is found that it can also be lit, and because of the large current, the LED brightness is very high;
Maximum output speed: Low configures the output speed. Here, the output speed is configured as low. In most cases encountered so far, just choose low.

  1. Clock configuration
    The clock of the APB2 bus is controlled by HSE. Click Clock Configuration. In this interface, you must select PLLCL, change 8MHz to 72, and press Enter. The following prompt box will appear, click OK:

  2. Project configuration
    Notice:
    The storage directory cannot contain Chinese characters;
    At Project, there are various configurations of the project. You only need to set a few and leave the others as default.
    Click Project Manager and configure as shown:

Click Code Generator for further configuration:

Click GENERATE CODE to create a project:

  1. Programming
    Note: Please place all self-written codes between /* USER CODE BEGIN XXX / / USER CODE END XXX */
    This way the code you wrote will not be deleted when we modify the project.
    In the while loop under the main function of the main.c file, add the following lines of code:
 HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);//PA4
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);//PB9
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_SET);//PC14
HAL_Delay(1000);//1s
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);//PA4
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET);//PB9
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_SET);//PC14
HAL_Delay(1000);//1s
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);//PA4
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);//PB9
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_RESET);//PC14
HAL_Delay(1000);//1s

Compilation found no error:

4. Hardware burning and running

  1. Burning tool
    Hardware tools:

st-link v2
STM32F103C8 development board
LED light
Several lines
software tools:

keil c51
2. Results display

HAL library implements running lights

5. Simulation operation in Keil environment

  1. Simulation environment settings
    Click the magic wand, select the target, and select the crystal oscillator as 8MHZ:
    (Note: 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 inaccurate)

Click the magic wand again, click debug, check Use simulator and Run to main(), and change the Dialog DLL to DARMSTM.DLL and the other to TARMSTIM.DLL; the Parameter in the two places is changed to -pSTM32F103C8:

Click OK to complete the simulation environment configuration.

  1. Virtual logic analyzer to observe output waveforms
    After the settings are completed, turn on the debugging mode and open the logic analyzer:

Add the IO port to be observed:
Method 1: Enter the dir vtreg command on the command line in the lower left corner to see which pins can be detected

Method 2: Type PORTA., PORTB. directly to get the target port; such as: porta.5

Method 3: Enter directly in the form of PORTX >>

Here, use method three to add the IO ports that need to be observed, namely GPIOA-5, GPIOB-9, and GPIOC-14 ports:
Note: When creating a new pin, be sure to set the Display Type to Bit!

After setting up, the IO port just set will appear on the left side of the logic analyzer:

Click to run at full speed; after executing for a period of time, click the Stop button:
Another: Click in or out to set the size of the Grid to 1s. Check signal info and cursor to set the starting line and see relevant information:

After running for a period of time, click Pause;
In order to observe whether the delay function is accurate: move the mouse to a falling edge of the first waveform and click to lock the time at this moment; then move the mouse to the falling edge of the second waveform to overlap it, and observe the two time:

As can be seen from the image, it is almost close to a complete second

Summary

Through this experiment, I not only understood the basic principles of STM32cubemx, but also used HAL library programming in the development environment of stm32cubeMX to realize the simulation operation of LED running lights and Keil environment, and observed its waveforms, which further deepened my understanding of Understanding and applying some knowledge.

?