If the paper is original articles, reprint please indicate the original source This article blog: https://blog.csdn.net/qq21497936/article/details/117151734

The development technology set (including Qt practical technology, Raspberry Pi, 3D, OpenCV, OpenGL, FFmpeg, OSG, SCM, hard and soft combination, etc.) in the continuous update… (Click the portal)

MCU related development

The STM32 firmware library can be used in the STM32 series, and the basic functions of GPIO can be used in the STM32 series.


preface

STM32 series is one of the most commonly used MCU, different versions of the corresponding in addition to pins, peripherals, frequency, capacity and other ‘different, its development method is the same. This chapter explains how to use the GPIO pin function to drive LED lights and receive input from the Key button.


STM32 series GPIO driven LED lights

Firstly, the author uses GPIO to conduct LED operation according to the circuit of the development board used by him. The following diagram is the circuit diagram of the development board LED lamp used by him:

  

Pin PD6 is connected to LED1, PD12 is connected to LED2, PD13 is connected to LED3, and the resistance is a current limiting resistance (the calculation method is to subtract the voltage drop of LED1 from the high level, and then divide by the current that LED can flow through).

Copy the template file, change the name to LEDtest1, start the software Keil4, and open the renamed project LEDtest1.

Create a new folder Dev under the project directory, create a new folder LED under Dev, create a new file led.c and led.h under LED, and then deploy the path and files there:

  

  

After successful deployment, our project is as follows:

  

Then start to write the program, mian. C, led.C, led.H, the content of the program is as follows:

  

  

  

  

The main logic of the program is as follows: open LED1, delay, open LED2, delay, open LED3, delay, close LED1, LED2, LED3, delay, turn back to the beginning of the cycle.


The LED engineering code can be downloaded from CSDN

CSDN download address: https://download.csdn.net/detail/qq21497936/7998805 QQ free download: 1047134658 (click on the “file” search “LEDtest1”, in the same group and post an update)


STM32 series GPIO detection KEY

Now let’s use GPIO for keystroke operation. The most common input for keystrokes, most of the experienced people I have contacted advise me to use polling (query). The following diagram is the circuit diagram of the key part of the author’s development board:



According to the circuit diagram, the five pins of PE0, PE1, PE2, PE3 and PE4 are high levels with pull-up resistors when they are not pressed. When pressed, there are two 10K resistors for voltage division, 1.65V. Check the electrical characteristics of the chip level, and you will find that the lowest voltage is lower than the high level. PC13 only has pullup resistance, as for why the author is not too clear, so we copy LEDtest1 project, renamed it KEYtest1, and then continue to start the KEY writing, or in accordance with the process, in the Dev folder to establish KEY folder, in the KEY folder to establish key.h, Both key.c files are deployed into the project and the path is included.

We continue to use the help document for reference, which is version V3.5 (or V3.5 if V3.4 is not specified, it is recommended that you have both versions available when using the document), and open the source file below

  

Let’s imagine that the GPIO of LEDTEST1 is configured as output, then the key should be configured as input, and what input should be configured as (you can refer to the eight IO modes). Here we consider for beginners, try to rely only on the help document, so we continue to use the help document to write, the configuration as input also has an initialization, So in the source code we look, look at the image below



Line 00067 initializes the button, and we click this function to jump to the source code

  

Click 255 again to see the source code

  

As you all know, 00257 is for configuring the details of interrupts (how to generate interrupts), 00259 is for associating interrupts with the interrupt vector (you can configure the priority and interrupt response functions), let’s not use interrupts, let’s ignore the following two lines, continue to go down

  

00265 this is configured to float input, we think this should be what we want (right or wrong, for the time being), keep going



The bit that reads the input data, 00316, we click on to go to its description

  

So we know that it is mainly used to determine the current value of which port which pin, the input value is either 0 or 1, can write the detection code, continue to write the code, the finished code is as follows:

  

  

  

Debugging runs successfully. In addition, I have configured the pull-up, pull-down and float input in the GPIO mode, and they all work normally, including the UESR key.

Key project code download

CSDN download address: https://download.csdn.net/detail/qq21497936/7998809 QQ free download: 1047134658 (click on the “file” search “KEYtest1”, in the same group and post an update)


The STM32 firmware library can be used in the STM32 series, and the basic functions of GPIO can be used in the STM32 series.


If the paper is original articles, reprint please indicate the original source This article blog: https://blog.csdn.net/qq21497936/article/details/117151734