GPIO key for HAL library with zero base
3.1 introduction
In the previous section, we learned about the lighting operation, which is equivalent to learning how to configure the GPIO output. Next, the GPIO input completes the key scanning operation.
preparation
- STM32 various types of boards (I use F103Rc and F407ZE).
- CubeMx, Keil_IDE.
3.2 the KEY button
Similarly, let’s first look at the schematic part of the button module:
When the key is not pressed, the key part is equivalent to the circuit breaker, and the voltage of PC13 is equivalent to the voltage of 3.3V at both ends of the capacitor.
When pressed, the button is partially short-circuited (i.e. a wire), and the voltage of PC13 connects to GND is 0V.
So the key is never pressed down to pressed down is equivalent to a high level to low level jump, referred to as falling edge.
1. New construction projects
After searching or filtering chips, click Start Project to successfully create a Project.
2. Configure the clock
Click RCC to enter the clock configuration and configure the high-speed clock as external crystal oscillator. The software automatically configures the pins of two crystal oscillators as shown in PD0 and PD1
3. Set Debug to DAP
4. Input mode configuration
- ① Select the pins as shown in the schematic diagram, where PC14/PC15 is the LED pin and PC12/PC13 is the KEY pin. Set the output and input modes respectively
- ② Select the GPIO module and configure the pin mode
- ③ Set the specific mode of PC12,
- GPIO mode: indicates the input mode.
- GPIO Pull: It can be seen from the schematic diagram that the level is high when no key is pressed, so the Pull up mode is selected.
- User Label: the User Label is used to identify the port and Pin of the corresponding Pin to facilitate API call. The key is KEY1/KEY2 and the indicator is LED1/LED2.
5. Clock configuration
Here, we still set the highest clock frequency as 72M. Enter 72 in the corresponding box and press Enter to see the changes in the following two pictures. Press the following figure before returning:
Press Enter as shown below:
6. Project management configuration and code generation configuration
Similarly, we fill in the project name KEY, select the file path, and the IDE and version number
After selecting these two, click the button in the upper right corner to generate the code:
7. Business logic code
7.1 API
Let’s look at the API functions corresponding to GPIO module:
/* HAL_GPIO_Init: cubemx automatically calls HAL_GPIO_DeInit after generating code: */ void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init); */ void HAL_GPIO_Init(GPIO_TypeDef *GPIO_Init); void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin); HAL_GPIO_ReadPin: Read pin level return value is (GPIO_PIN_RESET/GPIO_PIN_SET) HAL_GPIO_WritePin: Write pin level HAL_GPIO_TogglePin: flip pin level HAL_GPIO_LockPin: so pin current level will not be able to change HAL_GPIO_EXTI_IRQHandler: */ GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin); void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState); void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin); HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin); void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin); void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);Copy the code
The main () function
We can see the user tags defined earlier in main.h as follows:
/* Private defines -----------------------------------------------------------*/
#define KEY2_Pin GPIO_PIN_13
#define KEY2_GPIO_Port GPIOC
#define LED1_Pin GPIO_PIN_14
#define LED1_GPIO_Port GPIOC
#define LED2_Pin GPIO_PIN_15
#define LED2_GPIO_Port GPIOC
#define KEY1_Pin GPIO_PIN_12
#define KEY1_GPIO_Port GPIOC
Copy the code
Here is the mian() function:
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ WHILE (1) {// There is a button 1 pressed If (HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin) == GPIO_PIN_RESET) {HAL_Delay(20); /* flip LED1 */ HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin); /* While (HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)! = GPIO_PIN_SET); } if(HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin) == GPIO_PIN_RESET) {HAL_Delay(20); /* flip LED2 */ HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin); /* While (HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)! = GPIO_PIN_SET); } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }Copy the code
Download the code to the board, we press different keys different LED corresponding level reversal operation.
Ok, GPI key input is so simple to end, how is not the feeling is also very simple!