Development board: K210 AIRV R3 version widora
Development environment: kendryte IDE official
Required reference documents: Standalone SDK Programming Guide v0.5.0, and Widora schematic
(1) This section is for key input scanning
Create a new folder 02my_code_keyInput
Open the folder and select the folder 02my_code
Similarly, create a new document
To use key input, then I will copy the previous LED lighting program, find the previous lighting folder, and copy the content of main.c
Installation dependencies
Clean + build
The environment is set up and start writing code
(2) Look at the schematic diagram first
There are 2 available buttons + 2 LEDs are used
IO17 18 corresponds to LED
IO19 20 corresponding button
Configuration key input port
Set to input mode
GPIO_DM_INPUT should be a floating input, right? Not sure, wait for verification, (there is a pull-up resistor on the board, you can test the floating input)
GPIO_DM_INPUT_PULL_UP is up input
Then read the input
When the button is pressed, it is low level, so to judge whether it is pressed, read Pin to judge
Press the button, turn on the light, release the light, and the light will go out, here we don’t consider jitter
Clean + build + download and burn
effect
Press and hold the button, the light keeps on, release it to go off
All off when the button is not pressed
PS: GPIO_DM_INPUT is indeed a floating input
The overall code is as follows
#include <stdio.h>
#include "fpioa.h"
#include "gpio.h"
int main () {
printf("Hello World\n");
gpio_init ();
fpioa_set_function(17, FUNC_GPIO0);
fpioa_set_function(18, FUNC_GPIO1);
gpio_set_drive_mode(0, GPIO_DM_OUTPUT);
gpio_set_drive_mode(1, GPIO_DM_OUTPUT);
fpioa_set_function(19, FUNC_GPIO3);
fpioa_set_function(20, FUNC_GPIO4);
gpio_set_drive_mode(3, GPIO_DM_INPUT); //Floating input?
gpio_set_drive_mode(4, GPIO_DM_INPUT_PULL_UP); //Pull up input
while (1) {
if (!gpio_get_pin(3)) {
gpio_set_pin(0, GPIO_PV_HIGH);
} else
gpio_set_pin(0, GPIO_PV_LOW);
if (!gpio_get_pin(4)) {
gpio_set_pin(1, GPIO_PV_HIGH);
} else
gpio_set_pin(1, GPIO_PV_LOW);
}
return 1;
}