STM32 GPIO input – key detection

Main function code

/**
  *************************************************** ****************************
  * @file main.c
  * @author fire
  * @version V1.0
  * @date 2015-xx-xx
  * @brief Use buttons to control lanterns
  *************************************************** ****************************
  * @attention
  *
  * Experimental platform: Wildfire STM32 F429 development board
  * Forum: http://www.firebbs.cn
  * Taobao: https://fire-stm32.taobao.com
  *
  *************************************************** ****************************
  */
  
#include "stm32f4xx.h"
#include "./led/bsp_led.h"
#include "./key/bsp_key.h"



/**
  * @brief main function
  * @param None
  * @retval None
  */
int main(void)
{
/* LED port initialization */
LED_GPIO_Config();
  
  /*Initialization button*/
  Key_GPIO_Config();
\t
 
/* Poll the button status and invert the LED if the button is pressed */
while(1)
{
if( Key_Scan(KEY1_GPIO_PORT,KEY1_PIN) == KEY_ON )
{
/*LED1 reverse*/
LED1_TOGGLE;
}
    
    if( Key_Scan(KEY2_GPIO_PORT,KEY2_PIN) == KEY_ON )
{
/*LED2 inversion*/
LED2_TOGGLE;
}
}
}



/******************************************END OF FILE* *********************/

Description

This code is an embedded C language program for controlling colored lights, which runs on the Wildfire STM32 F429 development board. Below is an explanation of the main functions and components of the code:

1. File comment section: There is a comment at the beginning of the code to provide basic information about the file, including author, version, date, brief description, and other relevant information. This is a standard code comment style used to help developers understand and maintain code.
2. Include header files: The code includes some header files for STM32 development, such as “stm32f4xx.h” and custom LED and button control header files (“./led/bspled.h” and “./key/bspkey.h”). These header files contain necessary macro definitions, function prototypes, and other information so that the program can interact with the hardware.
3. Main function: The main function is the entry point of the program and executes the main logic of the program. The main steps include:

4. LED initialization: Initialize the LED control pin by calling the LED_GPIO_Config() function so that the LED can be controlled on and off later.
5. Button initialization: Initialize the control pin of the button by calling the Key_GPIO_Config() function so that the status of the button can be read later.
6. Polling key status: In an infinite loop, the program continuously checks the status of the key. If button 1 (KEY1) is pressed (the state is KEY_ON), the state of LED1 is reversed, that is, if LED1 is on, it becomes off, if it is off, it becomes on. Similarly, if key 2 (KEY2) is pressed, the program will also reverse the state of LED2.

7. Comment at the end of the program: There is a comment at the end of the code, marking the end of the file.

Summary: This code is a simple embedded program for controlling LEDs and buttons on the STM32 F429 development board. When button 1 is pressed, the state of LED1 will switch; when button 2 is pressed, the state of LED2 will also switch. This program is mainly used to demonstrate how to use the GPIO pins of the STM32 development board to control external hardware to achieve basic interactive functions.

LED code and KYE code

/**
  *************************************************** ****************************
  * @file bsp_led.c
  * @author fire
  * @version V1.0
  * @date 2015-xx-xx
  * @brief led application function interface
  *************************************************** ****************************
  * @attention
  *
  * Experimental platform: Wildfire STM32 F429 development board
  * Forum: http://www.firebbs.cn
  * Taobao: https://fire-stm32.taobao.com
  *
  *************************************************** ****************************
  */
  
#include "./led/bsp_led.h"

 /**
  * @brief Initialize the IO to control the LED
  * @param None
  * @retval None
  */
void LED_GPIO_Config(void)
{
/*Define a structure of type GPIO_InitTypeDef*/
GPIO_InitTypeDef GPIO_InitStructure;

/*Turn on the LED-related GPIO peripheral clock*/
RCC_AHB1PeriphClockCmd ( LED1_GPIO_CLK|
LED2_GPIO_CLK|
LED3_GPIO_CLK, ENABLE);

/*Select the GPIO pin to be controlled*/
GPIO_InitStructure.GPIO_Pin = LED1_PIN;

/*Set the pin mode to output mode*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    
    /*Set the output type of the pin to push-pull output*/
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    
    /*Set the pin to pull-up mode*/
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

/*Set the pin speed to 2MHz */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

/*Call the library function and initialize GPIO using the GPIO_InitStructure configured above*/
GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure);
    
    /*Select the GPIO pin to be controlled*/
GPIO_InitStructure.GPIO_Pin = LED2_PIN;
    GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure);
    
    /*Select the GPIO pin to be controlled*/
GPIO_InitStructure.GPIO_Pin = LED3_PIN;
    GPIO_Init(LED3_GPIO_PORT, &GPIO_InitStructure);
\t\t
/*Turn off RGB lights*/
LED_RGBOFF;
}
/******************************************END OF FILE* *********************/

LCD description

This code is an initialization function used to control LEDs. It contains some initialization codes for STM32 development, mainly used to configure LED-related GPIO pins and other parameters. Here’s a detailed explanation of the code:

1. File comment section: Like the previously mentioned code, this code also contains basic information about the file, including author, version, date and brief description. This helps developers understand the purpose and context of the file.
2. Include header files: The code includes a custom LED control header file “./led/bsp_led.h”, which is a header file used to configure LED control functions and macros. By including this header file, LED-related definitions and functions are available.
3.LEDGPIOConfig function: This is a function that initializes the LED. Its purpose is to configure the GPIO pins related to the LED so that the on and off status of the LED can be controlled. The following are the main steps of this function:

4. Define the GPIOInitTypeDef structure: First, define a structure of GPIO_InitTypeDef type named GPIOInitStructure, which is used to configure the properties of the GPIO pin.
5. Enable LED-related GPIO peripheral clocks: Enable the clocks of the GPIO pins where LED1, LED2 and LED3 are located through the RCC_AHB1PeriphClockCmd function to ensure that these pins can be accessed.
6. Configure the LED1 pin: Select the GPIO pin (LED1_PIN) where LED1 is located, and then configure the following parameters:

7. Set the pin mode to output mode (GPIOModeOUT).
8. The output type of the pin is set to push-pull output (GPIOOTypePP).
9. The pin is set to pull-up mode (GPIOPuPdUP).
10. Pin speed is set to 2MHz.
11. Use the GPIO_Init function to initialize the GPIO: By calling the GPIO_Init function, use the GPIO_InitStructure configured above to initialize the GPIO pin where LED1 is located.
12. Configure LED2 and LED3 pins: Repeat the above steps to configure the GPIO pins where LED2 and LED3 are located so that they have the same configuration parameters as LED1.
13. Turn off the RGB lights: Turn off the RGB lights (if present) by calling the LED_RGBOFF macro.

14. Comment at the end of the program: There is a comment at the end of the code, marking the end of the file.

Summary: This code is mainly responsible for configuring the LED pins on the STM32 development board to ensure that they can be used to control the on and off states of the LED. By using different GPIO pins, multiple LEDs can be controlled, which helps achieve various LED effects. This code is executed during the initialization phase to ensure that the LED can be controlled in the main program.

/**
  *************************************************** ****************************
  * @file bsp_key.c
  * @author fire
  * @version V1.0
  * @date 2015-xx-xx
  * @brief button application bsp (scan mode)
  *************************************************** ****************************
  *@attention
  *
  * Experimental platform: Wildfire STM32 F429 development board
  * Forum: http://www.firebbs.cn
  * Taobao: https://fire-stm32.taobao.com
  *
  *************************************************** ****************************
  */
  
#include "./key/bsp_key.h"

/// Inaccurate delay
void Key_Delay(__IO u32 nCount)
{
for(; nCount != 0; nCount--);
}

/**
  * @brief configure the I/O port used by the button
  * @param none
  * @retval None
  */
void Key_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
\t
/*Turn on the clock of the button GPIO port*/
RCC_AHB1PeriphClockCmd(KEY1_GPIO_CLK|KEY2_GPIO_CLK,ENABLE);
\t
  /*Select the pin of the button*/
GPIO_InitStructure.GPIO_Pin = KEY1_PIN;
  
  /*Set the pin to input mode*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  
  /*Set the pin to neither pull up nor pull down*/
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
\t
  /*Use the above structure to initialize the buttons*/
GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);
  
  /*Select the pin of the button*/
GPIO_InitStructure.GPIO_Pin = KEY2_PIN;
  
  /*Use the above structure to initialize the buttons*/
GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStructure);
}

/**
  * @brief Detect whether a key is pressed
  * @param GPIOx: specific port, x can be (A...K)
* @param GPIO_PIN: specific port bit, which can be GPIO_PIN_x (x can be 0...15)
  * @retval button status
  * @arg KEY_ON: key pressed
  * @arg KEY_OFF: The button is not pressed
  */
uint8_t Key_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin)
{
/*Check whether a button is pressed */
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON )
{
/*Wait for key release */
while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON);
return KEY_ON;
}
else
return KEY_OFF;
}
/******************************************END OF FILE* *********************/

KEY description

This code is a C file used to configure and read keys (buttons) in embedded systems. It appears to be written for the STM32 F429 development board and uses the STM32 HAL (Hardware Abstraction Layer) library to configure and manage GPIO pins.
Here is a detailed explanation of the code:

1. This file seems to be part of a larger project because there is #include “./key/bsp_key.h” at the beginning, which means that it includes a header file named bsp_key.h, which contains key-related Definitions and functions.
2.Key_Delay is a simple delay function that performs a busy waiting delay. The parameter nCount specifies the number of iterations, and the function will loop nCount times to introduce delay. This is a basic form of software delay, but is less precise.
3.Key_GPIO_Config function is used to configure the GPIO pin for the key. It initializes the GPIO pin of the key (button). In this case, it appears to be configured with two keys, KEY1 and KEY2. It enables the clocking of the GPIO ports used with these buttons, sets them as input pins, and specifies that they have no pull-up or pull-down resistors.
4.Key_Scan function is used to detect whether the key is pressed. It accepts two parameters: GPIOx, which specifies the GPIO port, and GPIO_Pin, which specifies a specific pin on that port.

5. It checks whether the GPIO pin is in pressed state (KEY_ON) by calling GPIO_ReadInputDataBit. If pressed, the function will wait for the key to be released, by constantly checking the pin status.
6. When the key is released, the function returns KEY_ON, indicating that the key has been pressed.
7. If the key is not pressed, the function will return KEY_OFF, indicating that the key is not pressed.
The values of KEY_ON and KEY_OFF are used to represent the status of the key, where KEY_ON means that the key has been pressed, and KEY_OFF means that it has not been pressed.

This code is used to read the basic settings of key input on the STM32 microcontroller. It initializes the GPIO pins and provides a simple function to check the status of the keys. The functionality that actually handles key presses or responds to key events may be implemented in other parts of the project that include this code.