Input Interrupt
This sample uses the GPIO input function to detect the GPIO input signal by interrupt.
It is necessary to configure the GPIO to input mode and enable the interrupt function. When the input signal changes, it will trigger the GPIO interrupt, and the input level information will be printed in the interrupt handler.
Users can modify pin information through different macro configurations. For specific macro configurations, refer to Configurations.
Requirements
For requirements, please refer to the Requirements.
Wiring
Connect P4_0 to the external input signal.
Configurations
The following macro can be configured to modify the pin definition.
#define INPUT_PIN P4_0
Building and Downloading
For building and downloading, please refer to the Building and Downloading.
Experimental Verification
After resetting the EVB, observe the log messages as shown in the Debug Analyzer.
Start input interrupt test!
The external input signal is controlled to change from high to low, and P4_0 detects the falling edge signal to trigger an interrupt. The interrupt handler function
GPIO_Input_Handler
prints the following information.enter GPIO_Pin_Handler success
Code Overview
This section mainly introduces the code and process description for initialization and corresponding function implementation in the example.
Source Code Directory
The directory for project file and source code are as follows:
Project directory:
sdk\samples\peripheral\gpio\input_interrupt\proj
Source code directory:
sdk\samples\peripheral\gpio\input_interrupt\src
Initialization
The initialization flow for peripherals can refer to Initialization Flow in General Introduction.
Call
Pad_Config()
andPinmux_Config()
to configure the PAD and PINMUX of the corresponding pins.void board_gpio_init(void) { Pad_Config(INPUT_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_ENABLE, PAD_OUT_HIGH); Pinmux_Config(INPUT_PIN, DWGPIO); }
Call
RCC_PeriphClockCmd()
to enable the GPIO clock.Initialize the GPIO peripheral:
Define the
GPIO_InitTypeDef
typeGPIO_InitStruct
, and callGPIO_StructInit()
to pre-fillGPIO_InitStruct
with default values.Modify the
GPIO_InitStruct
parameters as needed. The initialization parameter configurations for GPIO are shown in the table below. CallGPIO_Init()
to initialize the GPIO peripheral.
GPIO Hardware Parameters |
Setting in the |
GPIO |
---|---|---|
GPIO pin |
|
|
GPIO direction |
||
GPIO interrupt |
||
GPIO interrupt trigger |
||
GPIO interrupt polarity |
||
Debounce function |
||
Debounce clock source |
||
Debounce clock divide |
||
Debounce Limit |
31 |
Functional Implementation
When connecting P4_0 to GND (a falling edge on P4_0), detecting a falling edge of P4_0 triggers a GPIO interrupt.
The interrupt handler function GPIO_Pin_Handler
prints interrupt information.
In the interrupt handler, disabling and masking the corresponding GPIO interrupt should be executed first, and clearing the interrupt flag, unmasking, and enabling the GPIO interrupt should be executed last.
/* Mask and disable interrupt */
GPIO_INTConfig(GPIO_PORT, GPIO_PIN, DISABLE);
GPIO_MaskINTConfig(GPIO_PORT, GPIO_PIN, ENABLE);
DBG_DIRECT("enter GPIO_Pin_Handler success");
/* Clear int flag, unmask and enable interrupt */
GPIO_ClearINTPendingBit(GPIO_PORT, GPIO_PIN);
GPIO_MaskINTConfig(GPIO_PORT, GPIO_PIN, DISABLE);
GPIO_INTConfig(GPIO_PORT, GPIO_PIN, ENABLE);