当前位置:网站首页>Esp32 esp-idf GPIO key interrupt response
Esp32 esp-idf GPIO key interrupt response
2022-07-01 06:37:00 【Morning breeze】
Chen Tuo 2022/06/19-2022/06/19
1. summary
- This example shows how to configure GPIO And how to use it in case of interruption .
- Pin function
There is only one button on the development board connected to GPIO0. This pin needs to be pulled down when burning the firmware , When the program runs, it is input as a normal button .
- Official routine domestic mirror
https://gitee.com/EspressifSystems/esp-idf/tree/master/examples/peripherals/gpio/generic_gpio
2. development environment
《 Use Lexin domestic Gitee Image building ESP32 development environment 》
https://zhuanlan.zhihu.com/p/348106034
https://blog.csdn.net/chentuo2000/article/details/113424934?spm=1001.2014.3001.5501
3. Modify the code
Because we only have one button connected to PGIO0 On , Modify the code to capture the key press and release in an interrupt way , Corresponding to falling edge and rising edge interruption .
gpio_example_main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
/**
* Brief:
* This test code shows how to configure gpio and how to use gpio interrupt.
*
* GPIO0: input, pulled up, interrupt from rising edge and falling edge
*
*/
#define GPIO_INPUT_IO_0 0
#define GPIO_INPUT_PIN_SEL 1ULL<<GPIO_INPUT_IO_0
#define ESP_INTR_FLAG_DEFAULT 0
static xQueueHandle gpio_evt_queue = NULL;
static void IRAM_ATTR gpio_isr_handler(void* arg)
{
uint32_t gpio_num = (uint32_t) arg;
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
}
static void gpio_task_example(void* arg)
{
uint32_t io_num;
for(;;) {
if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));
}
}
}
void app_main(void)
{
gpio_config_t io_conf = {}; //zero-initialize the config structure.
//io_conf.intr_type = GPIO_INTR_POSEDGE; // The rising edge produces an interrupt
io_conf.intr_type = GPIO_INTR_ANYEDGE; // rising 、 Both falling edges are interrupted
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL; //bit mask of the pins, use GPIO0 here
io_conf.mode = GPIO_MODE_INPUT; //set as input mode
io_conf.pull_up_en = 1; //enable pull-up mode
gpio_config(&io_conf);
//create a queue to handle gpio event from isr
gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
//start gpio task
xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
//install gpio isr service
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
//hook isr handler for specific gpio pin
gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());
}4. Build the project
- Refresh esp-idf Environmental Science
get_idf
- Set the target chip
idf.py set-target esp32
- Configuration items
idf.py menuconfig
1) Set flash memory to 4MB
preservation , sign out .
- Compile the project
idf.py build
- Burn project
see USB Serial port COM slogan :
burning :
idf.py -p /dev/ttyS3 -b 115200 flash
- Enable monitor
idf.py monitor -p /dev/ttyS3
(Ctrl+] You can exit the monitor program )
When the key is pressed, the falling edge value is 0, When the key is released, the rising edge value is 1.
边栏推荐
- 解决The code generator has deoptimised the styling of xxxx.js as it exceeds the max of 500kb
- C#如何打印輸出原版數組
- SQL statement
- 基金定投是高风险产品吗?
- Chapitre V gestion des entrées / sorties
- 嵌入式系统
- Lxml module (data extraction)
- NOC 设计的一些坑
- [ManageEngine Zhuohao] helps Julia college, the world's top Conservatory of music, improve terminal security
- MySQL constraint learning notes
猜你喜欢

8 张图 | 剖析 Eureka 的首次同步注册表

Free trial of self-developed software noisecreater1.1

树莓派4的WiFi设置

Methods of downloading Foreign Periodicals

NOC 设计的一些坑

C language course set up salary management system (big homework)

图解事件坐标screenX、clientX、pageX, offsetX的区别
![[wechat applet] how to build a building block development?](/img/69/edb02c88b52b474a797307b96de369.jpg)
[wechat applet] how to build a building block development?
![[network security tool] what is the use of USB control software](/img/cc/20fc1f35c139c52c5922727368b835.png)
[network security tool] what is the use of USB control software
![[unity shader stroke effect _ case sharing first]](/img/bd/5cd1bef24e6b6378854114c2c05bd9.png)
[unity shader stroke effect _ case sharing first]
随机推荐
树莓派4的WiFi设置
Camouflage request header Library: Anti useragent
【#Unity Shader#自定义材质面板_第一篇】
【Unity Shader 消融效果_案例分享】
Lxml module (data extraction)
在支付宝上买基金安全吗?哪里可以买基金
Terminology description in the field of software engineering
[ManageEngine Zhuohao] use unified terminal management to help "Eurex group" digital transformation
[ManageEngine] terminal management system helps Huasheng securities' digital transformation
C how to print out the original array
H5 web page determines whether an app is installed. If it is installed, it will jump to the summary of the scheme to download if it is not installed
Record MySQL troubleshooting caused by disk sector damage
问题解决:OfficeException: failed to start and connect(一)
解决The code generator has deoptimised the styling of xxxx.js as it exceeds the max of 500kb
SQL learning notes 2
Free trial of self-developed software noisecreater1.1
@Transactional的传播属性REQUIRES_NEW深入理解
根据输入画有向图
ESP32 - ULP 协处理器在低功耗模式下读片内霍尔传感器HALL SENSOR
C语言课设学生选修课程系统(大作业)