目录

    • 1、资料
    • 2、创建线程的LED闪烁
    • 3、创建线程的读取GPIO输入电平
    • 4、使用LEDC实现简单呼吸灯:

1、资料

ESP32 开发环境 Espressif-IDE:
https://blog.csdn.net/chuner0425/article/details/123466848
https://blog.csdn.net/bin_zhang1/article/details/129993820?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-0-129993820-blog-129054762.235v40pc_relevant_default_base&spm=1001.2101.3001.4242.1&utm_relevant_index=3

开发板资料:https://wiki.luatos.com/chips/esp32c3/hardware.html

API资料:https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32c3/api-reference/peripherals/gpio.html

头文件路径:components/esp_driver_gpio/include/driver/gpio.h

应用示例路径:GPIO 输出和输入中断示例:peripherals/gpio/generic_gpio。

想要在CSDN文章中生成目录,需要两步:在博文中(一般是文章顶部)加上:@[TOC](这里写目录标题)然后使用#对标题编号。----------------------------------程序调试的Log是可以在程序中选择:esp_log_level_set( TAG, ESP_LOG_INFO );ESP_LOG_NONE: 禁用所有日志输出ESP_LOG_ERROR: 仅输出错误信息ESP_LOG_WARN: 输出警告和错误信息ESP_LOG_INFO: 输出信息、警告和错误信息ESP_LOG_DEBUG: 输出调试、信息、警告和错误信息ESP_LOG_VERBOSE: 输出所有日志信息vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS); //延时1s时钟速率设为1000hz,时间片是1ms及portTICK_PERIOD_MS=1000/configTICK_RATE_HZ=1ms所以vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS);等价于vTaskDelay(1000/1000=vTaskDelay(1=1s————————————————#include #include "freertos/FreeRTOS.h"#include "freertos/task.h"#include "driver/gpio.h"#include "esp_log.h"#include "led_strip.h"#include "sdkconfig.h"static const char *TAG = "LED";#define LED1_GPIO 12#define LED2_GPIO 13static uint8_t s_led_state = 0;static led_strip_handle_t led_strip;static void blink_led(void){//设置某个GPIO的 输出电平gpio_set_level(LED1_GPIO, s_led_state);gpio_set_level(LED2_GPIO, s_led_state);}static void configure_led(void){/* Set the GPIO as a push/pull output */gpio_reset_pin(LED1_GPIO);gpio_set_direction(LED1_GPIO, GPIO_MODE_OUTPUT); //设置GPIO方向(输入 或者 输出)gpio_reset_pin(LED2_GPIO);gpio_set_direction(LED2_GPIO, GPIO_MODE_OUTPUT);}void app_main(void){configure_led();esp_log_level_set(TAG, ESP_LOG_VERBOSE);while (1){ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF");blink_led();/* Toggle the LED state */s_led_state = !s_led_state;vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS);}}运行后打印的:I (3927313) LED: Turning the LED ON!I (3928313) LED: Turning the LED OFF!

2、创建线程的LED闪烁

#include #include "freertos/FreeRTOS.h"#include "freertos/task.h"#include "driver/gpio.h"#include "esp_log.h"#include "led_strip.h"#include "sdkconfig.h"static const char *TAG = "LED";#define LED1_GPIO 12#define LED2_GPIO 13static uint8_t s_led_state = 0;static led_strip_handle_t led_strip;static void blink_led(void){/* Set the GPIO level according to the state (LOW or HIGH)*/gpio_set_level(LED1_GPIO, s_led_state);gpio_set_level(LED2_GPIO, s_led_state);}static void configure_led(void){/* Set the GPIO as a push/pull output */gpio_reset_pin(LED1_GPIO);gpio_set_direction(LED1_GPIO, GPIO_MODE_OUTPUT);gpio_reset_pin(LED2_GPIO);gpio_set_direction(LED2_GPIO, GPIO_MODE_OUTPUT);}static void LED_task(void *pvParameters){while (1){s_led_state = !s_led_state;blink_led();ESP_LOGI(TAG, "LED_task the LED %s!", s_led_state == true ? "ON" : "OFF");vTaskDelay(1000 / portTICK_PERIOD_MS); //1000ms}}void app_main(void){configure_led();esp_log_level_set(TAG, ESP_LOG_VERBOSE);xTaskCreate(&LED_task, "LED_task", 2048, NULL, 5, NULL); // 创建任务while (1){vTaskDelay(5000 / portTICK_PERIOD_MS); //5000ms}}

3、创建线程的读取GPIO输入电平

#include #include "freertos/FreeRTOS.h"#include "freertos/task.h"#include "driver/gpio.h"#include "esp_log.h"#include "led_strip.h"#include "sdkconfig.h"//----------------------LED_task begin--------------------------------------------static const char *TAG = "LED";#define LED1_GPIO 12#define LED2_GPIO 13static unsigned char s_led_state = 0;static void blink_led(void){gpio_set_level(LED1_GPIO, s_led_state); //设置某个GPIO的 输出电平gpio_set_level(LED2_GPIO, s_led_state);}static void configure_led(void){gpio_reset_pin(LED1_GPIO);//恢复 GPIO 默认值gpio_set_direction(LED1_GPIO, GPIO_MODE_OUTPUT); //设置GPIO方向(输入 或者 输出)gpio_reset_pin(LED2_GPIO);gpio_set_direction(LED2_GPIO, GPIO_MODE_OUTPUT);}static void LED_task(void *pvParameters){while (1){s_led_state = !s_led_state;blink_led();ESP_LOGI(TAG, "LED_task the LED %s!", s_led_state == true ? "ON" : "OFF");vTaskDelay(1000 / portTICK_PERIOD_MS); //1000ms}}//----------------------LED_task END--------------------------------------------//----------------------Btn_task begin--------------------------------------------#define Btn1_GPIO 0/* 配置GPIO内部上下拉电阻gpio_set_pull_mode(GPIO_PULLUP_ONLY) //仅上拉gpio_set_pull_mode(GPIO_PULLDOWN_ONLY) //仅下拉gpio_set_pull_mode(GPIO_PULLUP_PULLDOWN) // 全部启用gpio_set_pull_mode(GPIO_FLOATING) // 悬空这个 GPIO 配置GPIO使能上下拉电阻gpio_pullup_en()gpio_pullup_dis()gpio_pulldown_en()gpio_pulldown_dis()*/static void configure_Btn(void){gpio_set_direction(Btn1_GPIO, GPIO_MODE_INPUT);gpio_set_pull_mode(Btn1_GPIO, GPIO_PULLUP_ONLY);gpio_pullup_en(Btn1_GPIO);}static void Btn_task(void *pvParameters){unsigned char mLevel = 0;configure_Btn();mLevel = gpio_get_level(Btn1_GPIO);while (1){if (mLevel != gpio_get_level(Btn1_GPIO)){mLevel = gpio_get_level(Btn1_GPIO);if (mLevel){ESP_LOGI("Btn_task", "---HIGH \n");}else{ESP_LOGI("Btn_task", "---LOW \n");}}vTaskDelay(20 / portTICK_PERIOD_MS); //20ms}}//----------------------Btn_task END--------------------------------------------void app_main(void){configure_led();esp_log_level_set(TAG, ESP_LOG_VERBOSE);xTaskCreate(&LED_task, "LED_task", 2048, NULL, 5, NULL); // 创建显示任务xTaskCreate(&Btn_task, "Btn_task", 2048, NULL, 5, NULL); // 创建按键任务while (1){vTaskDelay(5000 / portTICK_PERIOD_MS); //5000ms}}运行后的打印:I (5313) LED: LED_task the LED OFF!I (6033) Btn_task: ---HIGH I (6313) LED: LED_task the LED ON!I (7313) LED: LED_task the LED OFF!I (7953) Btn_task: ---LOW 

4、使用LEDC实现简单呼吸灯:

//----------------------PWM_LED_task begin------------------------------------------#include #include "driver/ledc.h"// 导入LEDC驱动库#include "esp_err.h" // 导入错误处理库// 定义LEDC定时器、模式、输出IO口、通道、占空比分辨率、初始占空比和频率#define LEDC_TIMERLEDC_TIMER_0#define LEDC_MODE LEDC_LOW_SPEED_MODE#define LEDC_OUTPUT_IO(12) // 定义输出的GPIO号#define LEDC_CHANNELLEDC_CHANNEL_0#define LEDC_DUTY_RES LEDC_TIMER_13_BIT // 设置占空比分辨率为13位#define LEDC_DUTY (4096) // 设置50%的占空比. (2 ** 13) * 50% = 4096#define LEDC_FREQUENCY(4000) // 设置频率为4000赫兹。void my_ledc_pwm_init(void){ledc_fade_func_install(0);//安装LEDC渐变功能// 准备并应用LEDC PWM定时器配置ledc_timer_config_t ledc_timer ={.speed_mode = LEDC_MODE,.duty_resolution= LEDC_DUTY_RES,.timer_num= LEDC_TIMER,.freq_hz= LEDC_FREQUENCY,// 设置输出频率为4000 Hz.clk_cfg= LEDC_AUTO_CLK// 自动选择时钟源};// 检测函数调用结果,确认无错误发生ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));// 准备并应用LEDC PWM通道配置ledc_channel_config_t ledc_channel ={.speed_mode = LEDC_MODE,.channel= LEDC_CHANNEL,.timer_sel= LEDC_TIMER,.intr_type= LEDC_INTR_DISABLE,// 禁用中断.gpio_num = LEDC_OUTPUT_IO,.duty = 0,// 初始占空比设为0%.hpoint = 0 // 高电平时间点设置为0};// 检测函数调用结果,确认无错误发生ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));}// 增加或减少LED亮度的函数void ledc_fade(int start_duty, int end_duty, int step, int delay, bool fade_up){int duty;if (fade_up){// 渐亮for (duty = start_duty; duty <= end_duty; duty += step){ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, duty);ledc_update_duty(LEDC_MODE, LEDC_CHANNEL);vTaskDelay(delay / portTICK_PERIOD_MS);}}else{// 渐暗for (duty = start_duty; duty >= end_duty; duty -= step){ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, duty);ledc_update_duty(LEDC_MODE, LEDC_CHANNEL);vTaskDelay(delay / portTICK_PERIOD_MS);}}}static void PWM_LED_task(void *pvParameters){//configure_led();// 设置LEDC外设配置my_ledc_pwm_init();// 设置占空比为50%ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY));// 更新占空比以应用新的值ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));printf("PWM-->ok\n");while (1){ESP_LOGI(TAG, "PWM_LED_task the LED up!");// 从0%占空比渐变到指定的亮度ledc_fade(0, LEDC_DUTY, 80, 30, true); // 慢慢亮起vTaskDelay(100 / portTICK_PERIOD_MS); // 在高亮度下停0.1秒ESP_LOGI(TAG, "PWM_LED_task the LED down!");// 从指定亮度渐变到0%占空比ledc_fade(LEDC_DUTY, 0, 80, 30, false); // 慢慢熄灭vTaskDelay(200 / portTICK_PERIOD_MS); // 在低亮度下停0.1秒}}//----------------------PWM_LED_task END--------------------------------------------void app_main(void){configure_led();esp_log_level_set(TAG, ESP_LOG_VERBOSE);//xTaskCreate(&LED_task, "LED_task", 2048, NULL, 5, NULL); // 创建显示任务xTaskCreate(&PWM_LED_task, "PWM_LED_task", 2048, NULL, 5, NULL); // 创建显示任务xTaskCreate(&Btn_task, "Btn_task", 2048, NULL, 5, NULL); // 创建按键任务while (1){vTaskDelay(5000 / portTICK_PERIOD_MS); //5000ms}}