preface
External interrupt is a common function in embedded system. The flexible use of interrupt greatly enhances the real-time performance of the system. How to configure a pin interrupt using the latest HAL Library of STM32?
Configuration interrupt
With the visual software provided by STM32: STM32CubeMX, you can easily configure pin interrupt, system clock, manage various peripherals, and generate Keil MDK and IAR projects. It is efficient and convenient to use. There is no need to code a pile of pin configuration codes by yourself, and the tool will generate them automatically. It only needs to be modified later.
Open stm32subemx and select the model of STM32 first, which determines the specific configuration of pins.
In this way, the interrupt is configured. Click the [generate project] button to generate the project of Keil MDK5. Open the project and you can see the pin interrupt configured by the software.
In MX_ GPIO_ In the init function, the pin is configured and the NVIC interrupt is enabled.
/** * @brief GPIO Initialization Function * @param None * @retval None */ void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6, GPIO_PIN_RESET); /*Configure GPIO pin : PE4 */ GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); /*Configure GPIO pins : KEY_RIGHT_Pin KEY_LEFT_Pin KEY_PA_Pin */ GPIO_InitStruct.Pin = KEY_RIGHT_Pin|KEY_LEFT_Pin|KEY_PA_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /* EXTI interrupt init*/ HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); }
At this time, compile and download, and press to enter the interrupt processing function:
/** * @brief This function handles EXTI line[15:10] interrupts. */ void EXTI15_10_IRQHandler(void) { /* USER CODE BEGIN EXTI15_10_IRQn 0 */ /* USER CODE END EXTI15_10_IRQn 0 */ HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_12); HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13); HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_15); /* USER CODE BEGIN EXTI15_10_IRQn 1 */ /* USER CODE END EXTI15_10_IRQn 1 */ }
Interrupt handling function
Where is your interrupt function added?
You can use STM32 HAL library without modifying} void exti15_ 10_ Functions such as irqhandler (void).
Can override: void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
void KEY_RIGHT_CallBack(void) { HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET); } void KEY_LEFT_CallBack(void) { HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET); } void KEY_PA_CallBack(void) { static uint8_t flag = 0x00; if (flag == 0x00) { HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6, GPIO_PIN_RESET); } else { HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6, GPIO_PIN_SET); } flag ^= 0x01; } /** * @brief EXTI line detection callbacks. * @param GPIO_Pin: Specifies the pins connected EXTI line * @retval None */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if (GPIO_Pin == GPIO_PIN_12) { KEY_RIGHT_CallBack(); } else if (GPIO_Pin == GPIO_PIN_13) { KEY_LEFT_CallBack(); } else if (GPIO_Pin == GPIO_PIN_15) { KEY_PA_CallBack(); } }
Rewrite: HAL_GPIO_EXTI_Callback, this means that all pin interrupts will enter. Therefore, it is necessary to enter different [user interrupt processing function] according to the pin.
Software debugging
After the key is found, the GPIO interrupt is generated and enters: EXTI15_10_IRQHandler, and then execute: HAL_GPIO_EXTI_Callback, and finally execute the key interrupt function defined by yourself.
Use STM32 standard library, directly in: EXTI15_10_IRQHandler can handle user interrupts.
Summary
In addition to the simple GPIO control of [water lamp], interrupt is a more important application.
Using the official tool of ST: STM32CubeMX can make the development of STM32 more efficient and convenient.
Interrupt real-time processing of the system has inherent advantages over polling pin status, which can make the CPU idle for other purposes.
Interrupt is very popular in low-power scenario applications, such as waking up MCU to ensure low power consumption and communication.