当前位置:网站首页>ESP32_ GPIO input, output and interrupt
ESP32_ GPIO input, output and interrupt
2022-04-22 02:59:00 【Fleeting years_ cth】
Catalog
2. Mode two : To configure gpio_config_t Structure way
Mode one : Front and back stage mode ( No operating system )
Mode two : Use FreeRTOS operating system
2. Mode two : To configure gpio_config_t Structure way
One 、GPIO Output
1. Mode one : The basic way
Programming logic :

To initialize LED For example , The code is as follows :
void LED_GPIO_Init(void)
{
gpio_pad_select_gpio(GPIO_LED); // choice GPIO mouth
gpio_set_direction(GPIO_LED, GPIO_MODE_OUTPUT); // GPIO As the output
gpio_set_level(GPIO_LED, 0); // Default low level
}
2. Mode two : To configure gpio_config_t Structure way
gpio_config_t Structure :
typedef struct {
uint64_t pin_bit_mask; /*!< GPIO pin: set with bit mask, each bit maps to a GPIO */
gpio_mode_t mode; /*!< GPIO mode: set input/output mode */
gpio_pullup_t pull_up_en; /*!< GPIO pull-up */
gpio_pulldown_t pull_down_en; /*!< GPIO pull-down */
gpio_int_type_t intr_type; /*!< GPIO interrupt type */
} gpio_config_t;
To initialize LED For example , The code is as follows :
void LED_GPIO_Init(void)
{
gpio_config_t io_config;
io_config.mode = GPIO_MODE_OUTPUT; // The input mode
io_config.pin_bit_mask = (1ull << 15); // Pin 15
io_config.pull_down_en = GPIO_PULLDOWN_DISABLE; // Pull down disable
io_config.pull_up_en = GPIO_PULLUP_DISABLE; // Pull up failure
io_config.intr_type = GPIO_INTR_DISABLE; // Interrupt disable
gpio_config(&io_config);
gpio_set_level(GPIO_LED, 1); // High level
}
3. Test code :
LED Blink every second :
Mode one : Front and back stage mode ( No operating system )
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "esp_spi_flash.h"
#include "freertos/queue.h"
#define GPIO_LED GPIO_NUM_2
//led initialization
void LED_GPIO_Init(void)
{
gpio_pad_select_gpio(GPIO_LED); // choice GPIO mouth
gpio_set_direction(GPIO_LED, GPIO_MODE_OUTPUT); // GPIO As the output
gpio_set_level(GPIO_LED, 0); // Default low level
}
// The main function
int app_main()
{
LED_GPIO_Init();
while(1)
{
gpio_set_level(GPIO_LED,1);// turn on the light
vTaskDelay(1000/portTICK_PERIOD_MS);// One second delay
gpio_set_level(GPIO_LED,0);// Turn off the lights
vTaskDelay(1000/portTICK_PERIOD_MS);// One second delay
}
}
Mode two : Use FreeRTOS operating system
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "esp_spi_flash.h"
#include "freertos/queue.h"
#define GPIO_LED GPIO_NUM_2
// LED Task function
void led_task(void *pvParam)
{
while (1)
{
gpio_set_level(GPIO_LED, 1); // High level
printf("LED OFF !\n");
vTaskDelay(50 / portTICK_PERIOD_MS); // Time delay
gpio_set_level(GPIO_LED, 0); // Low level
printf("LED ON !\n");
vTaskDelay(50 / portTICK_PERIOD_MS); // Time delay
}
}
//LED initialization
void LED_GPIO_Init(void)
{
gpio_pad_select_gpio(GPIO_LED); // choice GPIO mouth
gpio_set_direction(GPIO_LED, GPIO_MODE_OUTPUT); // GPIO As the output
gpio_set_level(GPIO_LED, 0); // Default low level
}
void app_main(void)
{
LED_GPIO_Init();
xTaskCreate(led_task,"ledtask",1024,NULL,4,NULL);
}
4. Experimental phenomena :
Two 、GPIO Input
1. Mode one : The basic way
Programming logic :
Take the initialization key as an example :
void KEY_GPIO_Init(void)
{
gpio_pad_select_gpio(GPIO_KEY);// choice keyIO mouth
gpio_set_direction(GPIO_KEY, GPIO_MODE_INPUT);// Set the port to the input mode
}
2. Mode two : To configure gpio_config_t Structure way
Take the initialization key as an example :
void KEY_GPIO_Init(void)
{
gpio_config_t io_config;
io_config.mode = GPIO_MODE_INPUT; // The input mode
io_config.pin_bit_mask = (1ull << 0); //key Pin
io_config.pull_down_en = GPIO_PULLDOWN_DISABLE; // Pull down disable
io_config.pull_up_en = GPIO_PULLUP_ENABLE; // Pull up enable
io_config.intr_type = GPIO_INTR_NEGEDGE; // Falling edge trigger interrupt
gpio_config(&io_config);
}
3. Test code :
Take button lighting as an example :
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "esp_spi_flash.h"
//---------- Macro definition -----------
#define GPIO_LED GPIO_NUM_2
#define GPIO_KEY GPIO_NUM_0
// LED initialization
void LED_GPIO_Init(void)
{
gpio_pad_select_gpio(GPIO_LED); // choice GPIO mouth
gpio_set_direction(GPIO_LED, GPIO_MODE_OUTPUT); // GPIO As the output
gpio_set_level(GPIO_LED, 0); // Default low level
}
//KEY initialization
void KEY_GPIO_Init(void)
{
gpio_pad_select_gpio(GPIO_KEY);
gpio_set_direction(GPIO_KEY, GPIO_MODE_INPUT);
}
// Key scan
uint8_t key_scan(void)
{
if(gpio_get_level(GPIO_KEY)==0)
{
while(gpio_get_level(GPIO_KEY)==0);
return 1;
}
return 0;
}
// The main function
void app_main(void)
{
uint8_t key_value;
uint8_t ledflag=0;
LED_GPIO_Init();
KEY_GPIO_Init();
while (1)
{
key_value = key_scan();
if (key_value == 1 && ledflag == 0)
{
ledflag = 1;
gpio_set_level(GPIO_LED,0);
}
else if (key_value == 1 && ledflag == 1)
{
ledflag = 0;
gpio_set_level(GPIO_LED,1);
}
vTaskDelay(10/portTICK_PERIOD_MS);// Prevent the watchdog from resetting
}
}
3、 ... and 、GPIO interrupt
Programming logic :
Example :
//key Interrupt initialization
void KEY_Intr_Init(void)
{
gpio_config_t io_config;
io_config.mode = GPIO_MODE_INPUT; // The input mode
io_config.pin_bit_mask = (1ull << 0); // key Pin
io_config.pull_down_en = GPIO_PULLDOWN_DISABLE; // Pull down disable
io_config.pull_up_en = GPIO_PULLUP_ENABLE; // Pull up enable
io_config.intr_type = GPIO_INTR_NEGEDGE; // Falling edge trigger interrupt
gpio_config(&io_config);
gpio_install_isr_service(1); // Register to interrupt service
gpio_isr_handler_add(GPIO_KEY, key_isr_handler, (void *)NULL); // Set interrupt callback function
}
Test code :
Take the button to interrupt lighting as an example :
Be careful : There cannot be any delay operation in the interrupt callback function , Include printf There can't be , If you want to print information, you can use esp_rom_printf(const char *fmt, ...) function .
#include <stdio.h> #include "sdkconfig.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" #include "driver/gpio.h" #include "esp_spi_flash.h" //---------- Macro definition ----------- #define GPIO_LED GPIO_NUM_2 #define GPIO_KEY GPIO_NUM_0 char LED_FLAG = 0; // led initialization void LED_GPIO_Init(void) { gpio_pad_select_gpio(GPIO_LED); // choice GPIO mouth gpio_set_direction(GPIO_LED, GPIO_MODE_OUTPUT); // GPIO As the output gpio_set_level(GPIO_LED, 0); // Default low level } // Interrupt handling function static void IRAM_ATTR key_isr_handler(void *arg) { if (LED_FLAG == 0) { LED_FLAG = 1; gpio_set_level(GPIO_LED, 0); esp_rom_printf("LED is ON!\n"); } else { LED_FLAG = 0; gpio_set_level(GPIO_LED, 1); esp_rom_printf("LED is OFF!\n"); }} //key Interrupt initialization void KEY_Intr_Init(void) { gpio_config_t io_config; io_config.mode = GPIO_MODE_INPUT; // The input mode io_config.pin_bit_mask = (1ull << 0); // key Pin io_config.pull_down_en = GPIO_PULLDOWN_DISABLE; // Pull down disable io_config.pull_up_en = GPIO_PULLUP_ENABLE; // Pull up enable io_config.intr_type = GPIO_INTR_NEGEDGE; // Falling edge trigger interrupt gpio_config(&io_config); gpio_install_isr_service(1); // Register to interrupt service gpio_isr_handler_add(GPIO_KEY, key_isr_handler, (void *)NULL); // Set interrupt callback function } void app_main(void) { LED_GPIO_Init(); KEY_Intr_Init(); while (1) { vTaskDelay(1000 / portTICK_PERIOD_MS); } }
Running results :
summary :
GPIO There are two ways to configure : The basic way 、 Configure the structure method .
GPIO interrupt :1. To configure IO Port is interrupt 2. Register to interrupt service 3. Set interrupt callback function
版权声明
本文为[Fleeting years_ cth]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220256304416.html
边栏推荐
- Day12 job
- 闭包是什么?闭包造成的内存泄漏又怎么解决?
- (Advanced) C function call
- 牛客网C语言入门刷题记录
- Kerberos authentication protocol
- Regression prediction | MATLAB realizes Bayes Gru (Bayes optimized gating cycle unit) multiple input and single output
- Authentication and access control
- 使用 DBT-3 对 OceanBase 和 MariaDB 进行性能测试对比
- C指针和数组深度汇总
- (进阶)C函数调用
猜你喜欢

How to solve the deadlock problem in MySQL?

ESP32_GPIO输入、输出和中断

外包干了四年,废了

Or1k startup file analysis

What does defi need to become mainstream?

Will you "sell" SQL?

After four years of outsourcing, it was abandoned

Install and deploy phpstudy + DVWA vulnerability drill platform

使用Xamarin编写一个精美的APP登录注册界面

How to provide CPU branch prediction efficiency at the code level
随机推荐
Inline temporary variables of code refactoring to replace temporary variables with queries
Flink 细粒度资源管理新特性解读
Openshift enterprise test environment application deployment practice
Text processing - sed
【经验】tf1.x迁移到tf2.x教程
全网最全量化硬件设施比拼大会
Stackoverflow:IActionContextAccessor Is Null
Use ordinary modem to make dial-up response. It was originally written in millionaire and summarized
苹果表主题图片爬取!
How long can we rely on play to earn economic profits?
IIS prompt: the solution of "this website requires you to log in".
我要开始学canvas了
Redis event driven framework (Part 1): when to use select, poll and epoll?
UE4 lock camera screen
国何以立
Nocalhost for dapr remote debugging
Share price plummeted Robinhood acquired British encryption company for expansion
All primes - ladder training competition
Oracle的联合主键和复合主键创建
2022-4-21作业