04 cubemx+keil+proteus simulation STM32 – GPIO (III)

Time:2022-6-1

The example of this paper refers to “STM32 single chip microcomputer development example – based on Proteus virtual simulation and hal/ll library”
Source code:https://github.com/LanLinnet/STM33F103R6

Project requirements

Realize the effect of running water lamp.

hardware design

  1. staySection IAs shown in the figure below, a circuit is added to Proteus, in which we add a drain resistor RX8.


    According to the circuit diagram, we can know that when STM32 MCU outputs low level, the LED is on.

  2. Open cubemx and configure pc0-pc7 pin as GPIO according to the setup project_ Output。

  3. Click “project manager” — “advanced settings”, change the GPIO library to the LL Library (we need to use the functions in the LL library later), and click “generator code” to generate the Keil project.

Software writing

  1. This time, we need to realize the water lamp. We can write each pin separately or use the look-up table method. This time, we use the look-up table method. The corresponding table of LED water lamp status bytes is as follows:

  2. Click “open project” to open the project in keil, and double-click the “main.c” file.

  3. In this simulation, we newly use the write port output functionLL_GPIO_WriteOutputPort()The official document API is shown in the following figure.

  4. We need to read the status bytes of the flow lamp by looking up the table, so we first set an array at the beginning of the function to store the status bytes of the flow lamp.

/* USER CODE BEGIN PV */

Through the loop method, we read the values in the state table array in turn, so we first define a loop variable at the beginning of the main function.

/* USER CODE BEGIN 1 */

Finally, we add the following code to the while loop

/* USER CODE BEGIN WHILE */

Joint commissioning

  1. Click Run to generate hex file.
  2. Load the corresponding hex file in Proteus and click Run. You can see that the LED lights are lit from top to bottom to form a cycle.