<p><iframe name="ifd" src="https://mnifdv.cn/resource/cnblogs/LearnESP32" frameborder="0" scrolling="auto" width="100%" height="1500"></iframe></p>
<iframe frameborder="0" height="1500" name="ifd" scrolling="auto" src="https://mnifdv.cn/resource/cnblogs/LearnESP32" width="100%"></iframe>
#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" #define gpio_pin 25 void app_main(void) { //gpio配置结构体 gpio_config_t io_conf; //禁止中断 io_conf.intr_type = GPIO_PIN_INTR_DISABLE; //输出模式 io_conf.mode = GPIO_MODE_OUTPUT; //配置要设置的引脚 io_conf.pin_bit_mask = (unsigned long long)1<<gpio_pin; //禁止下拉 io_conf.pull_down_en = 0; //禁止上拉 io_conf.pull_up_en = 0; //配置gpio(不设置上下拉默认输出低电平) gpio_config(&io_conf); while(1) { gpio_set_level(gpio_pin, 0);//设置引脚输出低电平 vTaskDelay(3000 / portTICK_RATE_MS);//延时约3S gpio_set_level(gpio_pin, 1);//设置引脚输出高电平 vTaskDelay(3000 / portTICK_RATE_MS);//延时约3S } }
#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" #define gpio_pin 25 #define gpio_pin1 26 void app_main(void) { //gpio配置结构体 gpio_config_t io_conf; //禁止中断 io_conf.intr_type = GPIO_PIN_INTR_DISABLE; //输出模式 io_conf.mode = GPIO_MODE_OUTPUT; //配置要设置的引脚 io_conf.pin_bit_mask = (((unsigned long long)1<<gpio_pin) | ((unsigned long long)1<<gpio_pin1)); //禁止下拉 io_conf.pull_down_en = 0; //禁止上拉 io_conf.pull_up_en = 0; //配置gpio(不设置上下拉默认输出低电平) gpio_config(&io_conf); while(1) { gpio_set_level(gpio_pin, 0);//设置引脚输出低电平 gpio_set_level(gpio_pin1, 0);//设置引脚输出低电平 vTaskDelay(3000 / portTICK_RATE_MS);//延时约3S gpio_set_level(gpio_pin, 1);//设置引脚输出高电平 gpio_set_level(gpio_pin1, 1);//设置引脚输出高电平 vTaskDelay(3000 / portTICK_RATE_MS);//延时约3S } }
#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" #define gpio_pin 0 void app_main(void) { //gpio配置结构体 gpio_config_t io_conf; //禁止中断 io_conf.intr_type = GPIO_PIN_INTR_DISABLE; //输入输出模式 io_conf.mode = GPIO_MODE_INPUT_OUTPUT; //配置要设置的引脚 io_conf.pin_bit_mask = (unsigned long long)1<<gpio_pin; //禁止下拉 io_conf.pull_down_en = 0; //禁止上拉 io_conf.pull_up_en = 0; //配置gpio(不设置上下拉默认输出低电平) gpio_config(&io_conf); while(1) { gpio_set_level(gpio_pin, 0);//设置引脚输出低电平 printf("获取引脚状态=%d\r\n",gpio_get_level(gpio_pin)); vTaskDelay(3000 / portTICK_RATE_MS);//延时约3S gpio_set_level(gpio_pin, 1);//设置引脚输出高电平 printf("获取引脚状态=%d\r\n",gpio_get_level(gpio_pin)); vTaskDelay(3000 / portTICK_RATE_MS);//延时约3S } }
#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" #define gpio_pin 0 static xQueueHandle gpio_evt_queue = NULL; /*gpio中断回调函数*/ 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配置结构体 gpio_config_t io_conf; //下降沿中断 io_conf.intr_type = GPIO_INTR_NEGEDGE; //输入模式 io_conf.mode = GPIO_MODE_INPUT; //配置要设置的引脚 io_conf.pin_bit_mask = (unsigned long long)1<<gpio_pin; //禁止下拉 io_conf.pull_down_en = 0; //上拉 io_conf.pull_up_en = 1; //配置gpio gpio_config(&io_conf); /*中断里面不能写printf,写了会死机; 所以就用任务+消息队列打印*/ //创建消息队列 gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t)); //创建任务 xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL); //设置中断优先级(1-8级),如果参数写0,则内部自动从1-3级中分配一个优先级; 7级优先级为最高; //如果设置为8,则此中断是共享中断,即多个外设都可触发这个中断(处理起来应该会很麻烦,到时候真的使用到再说) gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1); //移除中断 gpio_isr_handler_remove(gpio_pin); //添加中断 gpio_isr_handler_add(gpio_pin, gpio_isr_handler, (void*) gpio_pin); while(1) { //必须加延时,任务不能没有延时,否则导致任务无法切换. vTaskDelay(1000 / portTICK_RATE_MS); } }