当前位置:网站首页>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#如何打印输出原版数组
- Summary of wechat official account embedded program to jump to wechat
- 产品学习(一)——结构图
- Design of sales management system for C language course (big homework)
- Mongodb: I. what is mongodb? Advantages and disadvantages of mongodb
- How did ManageEngine Zhuohao achieve the goal of being selected into Gartner Magic Quadrant for four consecutive years?
- 产品学习(二)——竞品分析
- 三分钟带你快速了解网站开发的整个流程
- Free trial of self-developed software noisecreater1.1
- C语言课设销售管理系统设计(大作业)
猜你喜欢

Software engineering review
![[enterprise data security] upgrade backup strategy to ensure enterprise data security](/img/59/e44c6533aa546e8854ef434aa64113.png)
[enterprise data security] upgrade backup strategy to ensure enterprise data security

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

Design of sales management system for C language course (big homework)

Student attendance system for C language course (big homework)

Idea easy to use plug-in summary!!!

C语言课设学生选修课程系统(大作业)

产品学习(一)——结构图

Some pits designed by NOC

Lxml module (data extraction)
随机推荐
8 张图 | 剖析 Eureka 的首次同步注册表
软件工程复习
图解事件坐标screenX、clientX、pageX, offsetX的区别
C language course design student information management system (big homework)
Resttemplate use
Comment imprimer le tableau original
JSON module
问题解决:OfficeException: failed to start and connect(一)
常用快捷键
SQL学习笔记2
清除过期缓存条目后可用空间仍不足 - 请考虑增加缓存的最大空间
【微信小程序】如何搭积木式开发?
脏读、幻读和不可重复读
Internet worm
考研目录链接
C语言课设职工信息管理系统(大作业)
Interview questions for HW (OD) post
Requests module (requests)
Detailed steps for installing redis on Windows system
Student attendance system for C language course (big homework)