Input Polling
This sample uses the GPIO input function to detect the GPIO input signal by polling.
It is necessary to configure the GPIO as input mode and print the input level status within the while loop.
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 P1_0 and VDDIO to detect input high level. Connect P1_0 and GND to detect input low level.
Note
The VDDIO can be connected by referring to the instructions for IC Power Voltage Selection in EVB Guide .
Configurations
The following macro can be configured to modify the pin definition.
#define INPUT_PIN P1_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 polling test!
When a high level input is detected, the following message is printed in the Debug Analyzer every 2 seconds.
gpio_input_data = 1 gpio_input_data = 1 gpio_input_data = 1 ...
When a low level input is detected, the following message is printed in the Debug Analyzer every 2 seconds.
gpio_input_data = 0 gpio_input_data = 0 gpio_input_data = 0 ...
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_polling\proj
Source code directory:
sdk\samples\peripheral\gpio\input_polling\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_NONE, 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 |
Functional Implementation
After initialization, call the GPIO_ReadInputDataBit()
function within the while loop and print the read input level information.
while (1)
{
uint8_t gpio_input_data = GPIO_ReadInputDataBit(GPIO_PORT, GPIO_PIN);
DBG_DIRECT("gpio_input_data = %d", gpio_input_data);
platform_delay_ms(2000);
}