03 cubemx+keil+proteus simulation STM32 – GPIO (II)

Time:2022-6-3

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

Press the button to control the LED light on and off.

hardware design

  1. staySection IOn the basis of, add circuits to Proteus as shown in the following figure.


  2. Open cubemx and configure PC0 pin as GPIO according to the setup project_ Output, PC1 pin is GPIO_ Input。 Click “generator code” to generate the Keil project.


Software writing

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


  2. In this simulation, we newly use the read pin state functionHAL_GPIO_ReadPin()The official document API is shown in the following figure.


  3. When the key is pressed, the internal metal foil will be in an unstable on-off state for about 10ms under the action of mechanical vibration at the moment of on-off. Here, we use the software to eliminate the influence by means of chattering (delay).

    Enter the following code in the while (1) loop
    If (hal\gpio\u readpin (gpioc, gpio\u pin\u 1) = =0) / / judge whether the key is pressed. Press the key to low level

Joint commissioning

  1. Click Run to generate hex file.
  2. Load the corresponding hex file in Proteus, click Run, and you can see that the LED light is on. Press the button, and the LED light is off