跳到主要内容

点灯的案例说明 GPIO

代码整体功能

这是一个 LED 闪烁 的例子,类似硬件版的 "Hello World"。

/* Blink Example

This example code is in the Public Domain (or CC0 licensed, at your option.)

Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#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 = "example";

/* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
or you can edit the following line and set a number here.
*/
#define BLINK_GPIO CONFIG_BLINK_GPIO

static uint8_t s_led_state = 0;

#ifdef CONFIG_BLINK_LED_STRIP

static led_strip_handle_t led_strip;

static void blink_led(void)
{
/* If the addressable LED is enabled */
if (s_led_state) {
/* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */
led_strip_set_pixel(led_strip, 0, 16, 16, 16);
/* Refresh the strip to send data */
led_strip_refresh(led_strip);
} else {
/* Set all LED off to clear all pixels */
led_strip_clear(led_strip);
}
}

static void configure_led(void)
{
ESP_LOGI(TAG, "Example configured to blink addressable LED!");
/* LED strip initialization with the GPIO and pixels number*/
led_strip_config_t strip_config = {
.strip_gpio_num = BLINK_GPIO,
.max_leds = 1, // at least one LED on board
};
#if CONFIG_BLINK_LED_STRIP_BACKEND_RMT
led_strip_rmt_config_t rmt_config = {
.resolution_hz = 10 * 1000 * 1000, // 10MHz
.flags.with_dma = false,
};
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
#elif CONFIG_BLINK_LED_STRIP_BACKEND_SPI
led_strip_spi_config_t spi_config = {
.spi_bus = SPI2_HOST,
.flags.with_dma = true,
};
ESP_ERROR_CHECK(led_strip_new_spi_device(&strip_config, &spi_config, &led_strip));
#else
#error "unsupported LED strip backend"
#endif
/* Set all LED off to clear all pixels */
led_strip_clear(led_strip);
}

#elif CONFIG_BLINK_LED_GPIO

static void blink_led(void)
{
/* Set the GPIO level according to the state (LOW or HIGH)*/
gpio_set_level(BLINK_GPIO, s_led_state);
}

static void configure_led(void)
{
ESP_LOGI(TAG, "Example configured to blink GPIO LED!");
gpio_reset_pin(BLINK_GPIO);
/* Set the GPIO as a push/pull output */
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
}

#else
#error "unsupported LED type"
#endif

void app_main(void)
{

/* Configure the peripheral according to the LED type */
configure_led();

gpio_config_t io_conf = {
.intr_type = GPIO_INTR_DISABLE,
.mode = GPIO_MODE_INPUT,
.pin_bit_mask = (1ULL << CONFIG_BLINK_INPUT_GPIO),
.pull_down_en = 0,
.pull_up_en = 1,
};

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

1. LED 输出配置(两种方式)

方式一:普通 GPIO LED

static void configure_led(void)
{
ESP_LOGI(TAG, "Example configured to blink GPIO LED!");
gpio_reset_pin(BLINK_GPIO); // 重置引脚到默认状态
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT); // 设置为输出模式
}

方式二:可编程 LED 灯带

led_strip_config_t strip_config = {
.strip_gpio_num = BLINK_GPIO, // 指定 GPIO 引脚号
.max_leds = 1, // LED 数量
};

两种 LED 控制方式对比

普通 GPIO LED

// 简单的开关控制,类似布尔值操作
gpio_set_level(BLINK_GPIO, s_led_state); // 设置高电平(1)或低电平(0)

可编程 LED(如 WS2812)

// 可以设置颜色和亮度
led_strip_set_pixel(led_strip, 0, 16, 16, 16); // RGB 值
led_strip_refresh(led_strip); // 刷新显示

通信后端配置

代码支持两种通信方式:

RMT(Remote Control)

led_strip_rmt_config_t rmt_config = {
.resolution_hz = 10 * 1000 * 1000, // 10MHz 时钟频率
.flags.with_dma = false, // 不使用 DMA
};

SPI

led_strip_spi_config_t spi_config = {
.spi_bus = SPI2_HOST, // 使用 SPI2 总线
.flags.with_dma = true, // 使用 DMA(更高效)
};

主循环逻辑

while (1) {
ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF");
blink_led(); // 控制 LED
s_led_state = !s_led_state; // 切换状态
vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS); // 延时
}

这就像一个无限循环,每隔一段时间切换 LED 的开关状态,实现闪烁效果。

总结: GPIO 就是你控制硬件的"接口",这段代码展示了如何配置和使用这些接口来控制 LED 灯的闪烁。