当前位置:网站首页>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.
边栏推荐
- C语言课设图书信息管理系统(大作业)
- sci-hub如何使用
- C language course is provided with employee information management system (large operation)
- 2022 Jiangsu Vocational College skills competition (secondary vocational school) network construction and application open competition volume
- [ITSM] what is ITSM and why does it department need ITSM
- C语言课设销售管理系统设计(大作业)
- 【#Unity Shader#自定义材质面板_第二篇】
- SQL learning notes 2
- C language course set up salary management system (big homework)
- 基金定投是高风险产品吗?
猜你喜欢

下载外文期刊的方法

了解ESP32睡眠模式及其功耗

How did ManageEngine Zhuohao achieve the goal of being selected into Gartner Magic Quadrant for four consecutive years?

问题:OfficeException: failed to start and connect(三)

C语言课设学生考勤系统(大作业)

ESP32 ESP-IDF GPIO按键中断响应
![[wechat applet] view container and basic content components](/img/25/181986ab4bf048854d1d1ca87de637.jpg)
[wechat applet] view container and basic content components

概率论学习笔记
![[ManageEngine] how to realize network automatic operation and maintenance](/img/8a/75332d3180f92c6a6482d881032bbf.png)
[ManageEngine] how to realize network automatic operation and maintenance

8 张图 | 剖析 Eureka 的首次同步注册表
随机推荐
C语言课设物业费管理系统(大作业)
SQL语言的学习记录一
[unity shader ablation effect _ case sharing]
8 张图 | 剖析 Eureka 的首次同步注册表
PAT (Advanced Level) Practice 1057 Stack
C语言课设学生信息管理系统(大作业)
[network security tool] what is the use of USB control software
[wechat applet] how to build a building block development?
考研目录链接
Three minutes to quickly understand the whole process of website development
[ManageEngine Zhuohao] helps Huangshi Aikang hospital realize intelligent batch network equipment configuration management
C语言课设学生考勤系统(大作业)
第五章 輸入/輸出(I/O)管理
ESP32 ESP-IDF ADC监测电池电压(带校正)
【Unity Shader 消融效果_案例分享】
[ManageEngine] terminal management system helps Huasheng securities' digital transformation
概率论学习笔记
SQL statement
MySQL learning
ESP32在电池供电时用ULP监测电池电压