esp32-c-简单应用笔记

发布时间:2024年01月18日

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。

程序调试的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 <stdio.h>
#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 13

static 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);
}


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 <stdio.h>
#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 13

static 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
    }
}
文章来源:https://blog.csdn.net/u012579721/article/details/135639968
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。