Light up F1 series LED based on STM32cubeIDE

By default, STM32cubeMX has been used to define pins and generate initialization code

Create a new KEY_LED folder and create keyled.c and keyled.h files below.

First define the macro function for LED operation in keyled.h

Introduce functions: Find the HAL_GPIO_ReadPin() and HAL_GPIO_TogglePin() functions in stm32f4xx_hal_gpio.h, copy them to keyled.h, and replace void->#define

Then replace the functions: replace the GPIO_TogglePin() function with LED_R_Toggle(), replace the HAL_GPIO_ReadPin() function with LED_R_ON(), replace the HAL_GPIO_ReadPin() function with LED_R_OFF()

Then replace the function parameters: first define the function "main.h", then find the parameters in "main.h" as shown below, and replace the GPIO and Pin. Use GPIO_PIN_SET to replace GPIO_PinState

Finally add the macro compilation directive #ifdef

Alt+↑Move text up one line
Alt+/Autocomplete file
Ctrl+F replacement function
#include "main.h"
#ifdef LED_R_Pin
    #define LED_R_Toggle()    HAL_GPIO_TogglePin(LED_R_GPIO_Port , LED_R_Pin);
    #define LED_R_ON()    HAL_GPIO_ WritePin(LED_R_GPIO_Port, LED_R_Pin, GPIO_PIN_SET);
    #define LED_R_OFF()    HAL_GPIO_ WritePin(LED_R_GPIO_Port, LED_R_Pin, GPIO_PIN_RESET);
#endif

#ifdef LED_G_Pin
    #define LED_G_Toggle()    HAL_GPIO_TogglePin(LED_G_GPIO_Port , LED_G_Pin);
    #define LED_G_ON()    HAL_GPIO_ WritePin(LED_G_GPIO_Port, LED_G_Pin, GPIO_PIN_SET);
    #define LED_G_OFF()    HAL_GPIO_ WritePin(LED_G_GPIO_Port, LED_G_Pin, GPIO_PIN_RESET);
#endif

#ifdef LED_B_Pin
    #define LED_B_Toggle()    HAL_GPIO_TogglePin(LED_B_GPIO_Port , LED_B_Pin);
    #define LED_B_ON()    HAL_GPIO_ WritePin(LED_B_GPIO_Port, LED_B_Pin, GPIO_PIN_SET);
    #define LED_B_OFF()    HAL_GPIO_ WritePin(LED_B_GPIO_Port, LED_B_Pin, GPIO_PIN_RESET);
#endif

Write a function in keyled.c to detect whether KEY is pressed

Define function ScanPressedKey, while(1) infinite loop

Introduce HAL_GPIO_ReadPin (the parameters are modified the same as LED) to read the pin value, which is equal to GPIO_PIN_RESET

Modify the return value of the key pressed, which is defined as an enumeration type in keyled.h

Borrow the HAL_GetTick() function in hal.h to obtain the count value of the system tick signal

Call the function at the beginning and end of each while loop, record the initial signal value, and the signal value until the end of execution. If the time is greater than the defined time, it will be considered that the button has bounced, and the loop will be jumped out.

To reduce glitches, increase latency.

Finally, add the written function to the .h file

Code added to .h file

typedef enum
{
    KEY_0=0,
    KEY_1=1,
    KEY_NONE,
}KEYS;

#define KEY_WAIT_ALWAYS    0

KEYS ScanPressedKey(uint32_t timeout);

All code in .c file

#include "keyled.h"

KEYS ScanPressedKey(uint32_t timeout)//如果有某个函数的引脚被拉低,确实可以向这里返回0或者1
{
    uint32_t tickstart=HAL_GetTick();
    const uint32_t btnDelay=20;
    while(1)
    {
#ifdef KEY1_Pin
        if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin)==GPIO_PIN_RESET)
        {
            HAL_Delay(btnDelay);
            if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin)==GPIO_PIN_RESET)
                return KEY_0;
        }
#endif
#ifdef KEY2_Pin
        if(HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin)==GPIO_PIN_RESET)
        {
            HAL_Delay(btnDelay);
            if(HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin)==GPIO_PIN_RESET)
                return KEY_1;
        }
#endif
        if(timeout!=KEY_WAIT_ALWAYS)
        {
            if((HAL_GetTick()-tickstart)>timeout)
                break;
        }
    }
    return KEY_NONE;
}

Add KEY-LED to the project Add to the project's header file search path and source program search path

project->properties->C/C++General->Paths and Symbols

include->ADD input KEY_LED

Source Location->ADD Folder select the KEY_LED folder

At this point, the initialization of the MCU system and peripherals has been achieved.

The code will be written in the sandbox of main.c

#include "keyled.h"
 while (1)
  {
    KEYS curKey = ScanPressedKey(KEY_WAIT_ALWAYS);
    switch(curKey)
    {
    case KEY_0:
        LED_R_Toggle();
        break;
    case KEY_1:
        LED_G_Toggle();
        break;
    default:
        break;
    }
      HAL_Delay(200);

To achieve user requirements: button 1 reverses LED_R, button 2 reverses LED_G

Guess you like

Origin blog.csdn.net/m0_64580886/article/details/128626085