当前位置:网站首页>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.
边栏推荐
- 【Unity Shader 描边效果_案例分享第一篇】
- SQL learning notes 2
- Stored procedure learning notes
- Requests module (requests)
- Promise
- DML statement in SQL (data operation language)
- Rotate the animation component around the circle, take it and use it directly
- Async and await
- 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
- Detailed steps for installing redis on Windows system
猜你喜欢

ESP32深度睡眠电流怎样低于10uA

概率论学习笔记

Student attendance system for C language course (big homework)
![[automatic operation and maintenance] what is the use of the automatic operation and maintenance platform](/img/14/756d566744d6e4a988a284c5b30130.png)
[automatic operation and maintenance] what is the use of the automatic operation and maintenance platform

C语言课设工资管理系统(大作业)
![[wechat applet] how to build a building block development?](/img/69/edb02c88b52b474a797307b96de369.jpg)
[wechat applet] how to build a building block development?

C语言课设图书信息管理系统(大作业)

Grain Mall - environment (p1-p27)

Free trial of self-developed software noisecreater1.1

图解事件坐标screenX、clientX、pageX, offsetX的区别
随机推荐
Embedded system
Application of IT service management (ITSM) in Higher Education
Is the account opening of Huafu securities safe and reliable? How to open Huafu securities account
@Transactional的传播属性REQUIRES_NEW深入理解
软件工程领域的名词描述
HW(OD)岗面试题
How does the port scanning tool help enterprises?
Draw a directed graph based on input
Detailed steps for installing redis on Windows system
码力十足学量化|如何在财务报告寻找合适的财务公告
[wechat applet low code development] second, resolve the code composition of the applet in practice
数据库对象:视图学习记录
给逆序对数求原数组
SQL学习笔记九种连接2
[ITSM] what is ITSM and why does it department need ITSM
Stored procedure learning notes
SQL语言的学习记录一
idea 好用插件汇总!!!
解决The code generator has deoptimised the styling of xxxx.js as it exceeds the max of 500kb
清除过期缓存条目后可用空间仍不足 - 请考虑增加缓存的最大空间