怀揣少年梦 发表于 2021-7-3 17:23

【ESP32-C3-DevKitM-1】创建工程+点亮RGB灯

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">上次搭好开发环境后,一直想点亮</font><font face="Calibri">RGB</font><font face="宋体">灯,但是平时时间也不多,今天就来给大家分享一下创建工程和点亮</font><font face="Calibri">RGB</font><font face="宋体">灯。</font></span></span></span></span></p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">1、首先把我们要用到的功能例子复制到自己的工程目录。我复制的是led_strip的例子</font></span></span></span></span></p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify"></p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify">&nbsp;</p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify">&nbsp;</p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">2、然后打开</font><font face="Calibri">VS code </font><font face="宋体">软件,在如图所示,添加文件夹,就是刚刚复制那个目录的文件夹。打开后,修改为自己的代码。</font></span></span></span></span></p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify"></p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify">打开之后就是这个样子</p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify">&nbsp;</p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify"></p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify">3、修改好代码后,编译。编译时间比较久。再选择串口,下载。</p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify">&nbsp;</p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify">4、最后的效果图</p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify"></p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify">&nbsp;</p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify">最后附代码图</p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify">&nbsp;</p>

<p style="margin-left:28px; text-indent:21.0000pt; text-align:justify">&nbsp;</p>

<pre>
<code>/* RMT example -- RGB LED Strip

   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 &lt;stdio.h&gt;
#include "esp_log.h"
#include "driver/rmt.h"
#include "led_strip.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

static led_strip_t *strip;

#define RMT_TX_NUM 8//发送口
#define RMT_TX_CHANNEL RMT_CHANNEL_0//发送频道
#define LED_STRIP_NUM 1//灯珠数量

static const char *TAG = "WS2812S";

struct WS2812_COLOR{
uint32_t red;
uint32_t green ;
uint32_t blue;
};

struct WS2812_COLOR WS2812_RGB;

void init_led();
void set_rgb(uint16_t Red, uint16_t Green, uint16_t Blue);

void app_main(void)
{
    init_led();
    while (true)
    {
    set_rgb(0xfe,0x00,0x25); //
      /* code */
    }
}

void init_led()
{
        rmt_config_t config = RMT_DEFAULT_CONFIG_TX(RMT_TX_NUM, RMT_TX_CHANNEL);//配置引脚及通道
        // set counter clock to 40MHz
        config.clk_div = 2;
        ESP_ERROR_CHECK(rmt_config(&amp;config));
        ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));

        // install ws2812 driver
        led_strip_config_t strip_config = LED_STRIP_DEFAULT_CONFIG(24, (led_strip_dev_t)config.channel);
        strip = led_strip_new_rmt_ws2812(&amp;strip_config);
        if (!strip)
        {
                ESP_LOGE(TAG, "install WS2812 driver failed");
        }
        // Clear LED strip (turn off all LEDs)
        ESP_ERROR_CHECK(strip-&gt;clear(strip, 100));
}


void set_rgb(uint16_t Red, uint16_t Green, uint16_t Blue)
{
        for (int i = 0; i &lt; LED_STRIP_NUM; i++)
        {
                strip-&gt;set_pixel(strip, i, Red, Green, Blue);//设置颜色
        }
        WS2812_RGB.red = Red;
        WS2812_RGB.green = Green;
        WS2812_RGB.blue = Blue;
        strip-&gt;refresh(strip, 10);
}



</code></pre>

<p>继续努力学习ESP32的使用</p>

Jacktang 发表于 2021-7-3 21:47

<p>亮灯效果很好</p>
页: [1]
查看完整版本: 【ESP32-C3-DevKitM-1】创建工程+点亮RGB灯