Key - Edge Trigger
This sample uses the GPIO input function to implement key functionality. The key is edge-triggered.
Users can modify key information through different macro configurations. For specific macro configurations, refer to Configurations.
Requirements
For hardware requirements, please refer to the Requirements.
Wiring
Connect P0_0 and KEY1 on the EVB.
The hardware connection of GPIO key edge sample code is shown in the figure below.

GPIO Key Sample Code Hardware Connection Diagram
Configurations
The following macros can be configured to modify key pin definitions.
#define KEY_PIN ADC_0
The following macros can be configured to modify the debounce time of the key.
#define KEY_DEBOUNCE_TIME (10) //10ms
The entry function is as follows, call this function in
main()
to run this sample code. For more details, please refer to the Initialization.gpio_edge_key();
Building and Downloading
For building and downloading, please refer to the Building and Downloading.
Experimental Verification
Press the Reset button on the EVB.
Press KEY1, the following message is printed in the Debug Analyzer.
key_handler: Key press
Release KEY1, the following message is printed in the Debug Analyzer.
key_handler: Key release
Code Overview
This section introduces the code and process description for initialization and corresponding function implementation in the sample.
Source Code Directory
The directory for project file and source code are as follows.
For project directory, please refer to Source Code Directory.
Source code directory:
sdk\sample\io_demo\gpio\key\edge\gpio_edge_key.c
.
Initialization
The initialization flow for peripherals can refer to Initialization Flow.
Call
hal_gpio_init()
to enable GPIO clock.hal_gpio_init();
Call
hal_gpio_int_init()
to initialize GPIO interrupt.hal_gpio_int_init();
Call
hal_gpio_set_debounce_time()
to set GPIO debounce time.hal_gpio_set_debounce_time(30);
Call
hal_gpio_init_pin()
to initialize the GPIO peripheral and callhal_gpio_set_up_irq()
to initialize interrupt-related parameters. The GPIO initialization parameters are configured as shown in the table below.
Key Hardware Parameters |
Key |
---|---|
Pin Number |
|
GPIO Type |
|
GPIO Mode |
|
GPIO Pull Value |
|
Interrupt Type |
|
Interrupt Polarity |
|
Debounce Enable |
|
Call
hal_gpio_register_isr_callback()
to register gpio interrupt callback.Call
hal_gpio_irq_enable()
to enable gpio interrupt.
Functional Implementation
When KEY1 is pressed, an interrupt is triggered once, and when KEY1 is released, another interrupt is triggered.
The interrupt handler function key_handler
will perform the following actions.
Call
hal_gpio_irq_disable()
to disable GPIO interrupt.Call
hal_gpio_get_input_level()
to get the input level of key.Based on the key’s level, change the interrupt polarity by calling
hal_gpio_irq_change_polarity()
.Call
hal_gpio_irq_enable()
to enable GPIO interrupt.
static void key_handler(uint32_t key_index)
{
/* Disable GPIO interrupt */
hal_gpio_irq_disable(key_index);
key_status = hal_gpio_get_input_level(key_index);
if (key_status)
{
IO_PRINT_INFO0("key_handler: Key release");
hal_gpio_irq_change_polarity(key_index, GPIO_IRQ_ACTIVE_LOW);
}
else
{
IO_PRINT_INFO0("key_handler: Key press");
hal_gpio_irq_change_polarity(key_index, GPIO_IRQ_ACTIVE_HIGH);
}
/* Enable GPIO interrupt */
hal_gpio_irq_enable(key_index);
}