当前位置:网站首页>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 language course set up student elective course system (big homework)
- RestTemplate使用
- How did ManageEngine Zhuohao achieve the goal of being selected into Gartner Magic Quadrant for four consecutive years?
- sql中TCL语句(事务控制语句)
- 问题:OfficeException: failed to start and connect(三)
- [unity shader amplify shader editor (ASE) Chapter 9]
- Postgraduate entrance examination directory link
- node中引入模块的原理
- [unity shader ablation effect _ case sharing]
- 【Unity Shader 消融效果_案例分享】
猜你喜欢
问题:OfficeException: failed to start and connect(二)
【#Unity Shader#自定义材质面板_第二篇】
嵌入式系统
ESP32 - ULP 协处理器在低功耗模式下读片内霍尔传感器HALL SENSOR
SQL语句
[ManageEngine Zhuohao] helps Julia college, the world's top Conservatory of music, improve terminal security
C语言课设职工信息管理系统(大作业)
产品学习(一)——结构图
[wechat applet low code development] second, resolve the code composition of the applet in practice
VS2019如何永久配置本地OpenCV4.5.5使用
随机推荐
软件工程领域的名词描述
2022 年江苏省职业院校技能大赛(中职) 网络搭建与应用赛项公开赛卷
解决The code generator has deoptimised the styling of xxxx.js as it exceeds the max of 500kb
mysql数据类型学习笔记
Gson的@JsonAdater注解的几种方式
【微信小程序】一文解决button、input、image组件
C language course set up student elective course system (big homework)
产品学习(二)——竞品分析
C language course is provided with employee information management system (large operation)
Student attendance system for C language course (big homework)
Record MySQL troubleshooting caused by disk sector damage
Some pits designed by NOC
SQL语言的学习记录一
Terminology description in the field of software engineering
Interview questions for HW (OD) post
C language course design student information management system (big homework)
ESP32 ESP-IDF GPIO按键中断响应
What are the functions of LAN monitoring software
网络爬虫
码力十足学量化|如何在财务报告寻找合适的财务公告