news 2026/4/23 15:50:17

ESP32-S3(3) : 点亮WS2812 RGB

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
ESP32-S3(3) : 点亮WS2812 RGB

1.说明

开发框架 : ESP-IDF, 版本: 5.5.0

开发版图片(图中左边typec接口上面一点的白色小方块就是WS2812 RGB) :

2.代码

main/CMakeLists.txt

# 主程序组件 idf_component_register(SRCS "main.c" INCLUDE_DIRS ".")

CMakeLists.txt

# The following five lines of boilerplate have to be in your project's # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(0_kai_fa_ban)

main/main.c

rmt.h过时了, 但是不影响功能

#include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/rmt.h" #include "esp_log.h" static const char *TAG = "WS2812"; #define WS2812_GPIO_PIN GPIO_NUM_48 #define LED_NUM 1 #define RMT_TX_CHANNEL RMT_CHANNEL_0 // WS2812 timing parameters (in nanoseconds) #define T0H 350 // 0 bit high time #define T0L 900 // 0 bit low time #define T1H 900 // 1 bit high time #define T1L 350 // 1 bit low time #define RESET 50000 // Reset time // Convert time to RMT ticks (80MHz / 4 = 20MHz, 1 tick = 50ns) #define NS_TO_TICKS(ns) ((ns) / 50) void ws2812_init(void) { rmt_config_t config = RMT_DEFAULT_CONFIG_TX(WS2812_GPIO_PIN, RMT_TX_CHANNEL); config.clk_div = 4; // 80MHz / 4 = 20MHz config.mem_block_num = 1; config.tx_config.loop_en = false; config.tx_config.carrier_en = false; config.tx_config.idle_output_en = true; config.tx_config.idle_level = RMT_IDLE_LEVEL_LOW; ESP_ERROR_CHECK(rmt_config(&config)); ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0)); ESP_LOGI(TAG, "WS2812 initialized on GPIO %d", WS2812_GPIO_PIN); } void set_led_color(uint8_t red, uint8_t green, uint8_t blue) { uint8_t color[3] = {green, red, blue}; // WS2812 uses GRB order rmt_item32_t items[24]; // 3 bytes * 8 bits // Convert each bit to RMT items for (int i = 0; i < 3; i++) { for (int j = 0; j < 8; j++) { int bit_index = (i * 8) + j; if (color[i] & (1 << (7 - j))) { // Bit 1 items[bit_index].level0 = 1; items[bit_index].duration0 = NS_TO_TICKS(T1H); items[bit_index].level1 = 0; items[bit_index].duration1 = NS_TO_TICKS(T1L); } else { // Bit 0 items[bit_index].level0 = 1; items[bit_index].duration0 = NS_TO_TICKS(T0H); items[bit_index].level1 = 0; items[bit_index].duration1 = NS_TO_TICKS(T0L); } } } // Send data ESP_ERROR_CHECK(rmt_write_items(RMT_TX_CHANNEL, items, 24, true)); // Reset vTaskDelay(pdMS_TO_TICKS(1)); } void app_main(void) { ESP_LOGI(TAG, "Starting WS2812 Demo"); ws2812_init(); vTaskDelay(pdMS_TO_TICKS(1000)); while (1) { ESP_LOGI(TAG, "Red"); set_led_color(255, 0, 0); vTaskDelay(pdMS_TO_TICKS(1000)); ESP_LOGI(TAG, "Green"); set_led_color(0, 255, 0); vTaskDelay(pdMS_TO_TICKS(1000)); ESP_LOGI(TAG, "Blue"); set_led_color(0, 0, 255); vTaskDelay(pdMS_TO_TICKS(1000)); ESP_LOGI(TAG, "White"); set_led_color(255, 255, 255); vTaskDelay(pdMS_TO_TICKS(1000)); ESP_LOGI(TAG, "Off"); set_led_color(0, 0, 0); vTaskDelay(pdMS_TO_TICKS(1000)); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 10:49:50

5分钟掌握Grasscutter Tools:原神私服管理的智能化解决方案

5分钟掌握Grasscutter Tools&#xff1a;原神私服管理的智能化解决方案 【免费下载链接】grasscutter-tools A cross-platform client that combines launcher, command generation, and mod management to easily play Grasscutter; 一个结合了启动器、命令生成、MOD管理等功能…

作者头像 李华
网站建设 2026/4/22 12:50:30

基于hid单片机的自定义HID设备开发完整指南

打造你的专属外设&#xff1a;从零开始玩转基于HID单片机的自定义设备开发 你有没有想过&#xff0c;自己动手做一个能控制电脑剪辑软件的“一键快进”按钮&#xff1f;或者为工业设备设计一个带灯光反馈的紧急操作面板&#xff1f;又或者给VR游戏配一个独一无二的体感控制器&…

作者头像 李华
网站建设 2026/4/23 8:19:53

AUTOSAR架构图中的通信栈设计核心要点

深入AUTOSAR通信栈&#xff1a;从信号到总线的工程实践指南在一辆现代智能汽车中&#xff0c;ECU之间的数据流动远比我们想象的复杂。当你踩下刹车时&#xff0c;制动指令需要在不到10毫秒内传达到电机控制器&#xff1b;当OTA升级包从云端抵达T-Box时&#xff0c;数MB的数据要…

作者头像 李华
网站建设 2026/4/23 8:15:20

Venera漫画阅读器终极指南:快速上手免费阅读神器

想要在手机上畅享海量漫画资源吗&#xff1f;Venera漫画阅读器作为一款完全免费的跨平台应用&#xff0c;为你带来前所未有的漫画阅读体验。无论是本地文件还是在线资源&#xff0c;这款神器都能轻松驾驭。本文将带你从零开始&#xff0c;全面掌握Venera的各项强大功能。 【免费…

作者头像 李华
网站建设 2026/4/23 8:18:33

Noita多人联机模组:5步实现物理沙盒游戏全新社交体验

Noita多人联机模组&#xff1a;5步实现物理沙盒游戏全新社交体验 【免费下载链接】noita_entangled_worlds An experimental true coop multiplayer mod for Noita. 项目地址: https://gitcode.com/gh_mirrors/no/noita_entangled_worlds 厌倦了独自探索Noita的神秘世界…

作者头像 李华