<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[【模块第三弹】哪吒中的LEDC的驱动]]></title><description><![CDATA[<p dir="auto">实现了控制LEDC的接口，通过宏定义控制LEDC的个数，前提是需要在dts中打开这么多个LEDC设备。封装成内部接口，当然也可以自定义修改。</p>
<ul>
<li>集成颜色解码接口，使用宏定义声明颜色，调用颜色解码接口即可显示出对应的颜色。</li>
<li>目前有三种流水样式，其他流水样式可以参考这三种流水样式做修改。</li>
</ul>
<p dir="auto">代码<code>main.c</code></p>
<pre><code>#include "ws2812.h"
int main(int argc, char const *argv[])
{
    printf("demo 1\n");
    //LEDC_LiuShui();
    //LED_LiuShui2();
    LiuSui3();
    return 0;
}
</code></pre>
<p dir="auto">代码<code>ws2812.h</code></p>
<pre><code>#ifndef WS2812_H
#define WS2812_H


#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;    
#include &lt;sys/stat.h&gt;    
#include &lt;fcntl.h&gt;

typedef unsigned char uint8;
typedef unsigned int uint32;

#define LED_NUM 32

#define LEDC_RED_PATH "/sys/class/leds/sunxi_led%dr/brightness"
#define LEDC_GREEN_PATH "/sys/class/leds/sunxi_led%dg/brightness"
#define LEDC_BLUE_PATH "/sys/class/leds/sunxi_led%db/brightness"

#define delay_ms(x)  (usleep(x*1000))

#define MediumTurquoise 0x48D1CC
#define Gold            0xFFD700
#define DarkGray        0xA9A9A9
#define Magenta         0xFF00FF
#define Yellow          0xFFFF00

int LED_LiuShui2();
int LEDC_LiuShui();
int LED_Color(int led,uint8 R,uint8 G,uint8 B);
int ColorDecode(uint32 color ,uint8 *R,uint8 *G,uint8 *B);
int LiuSui3();




#endif


</code></pre>
<p dir="auto">代码<code>ws2812.c</code></p>
<pre><code>#include "ws2812.h"

static int export_fd;
static int color[3] = {0,0,255}; //RGB

int LED_LiuShui2(){
    int i;
    int mode = 0;
    int R= 0 ,G = 0,B = 0;
    while(1){
        if(i == 32)
        {
            i = 0;
            mode += 1;
        }
        if(mode == 0){
            R = 255; G = 0; B = 0;
        }
        if (mode == 1){
            R = 0; G = 255; B = 0;
        }
        if(mode == 2){
            R = 0; G = 0; B = 255;
        }
        if(mode == 3)
        {
            mode = 0;
            printf("delate 0\n");
        }
        LED_Color(i,R,G,B);
        i++;
        delay_ms(20);
    }
    return 1;
}

int LEDC_LiuShui(){
    int i = 0,len;
    char ledPath[45] = {0};
    char led_write[3] = {0};
    printf("demo 1 \n");
    while(1){
        for(i = 0 ; i &lt; LED_NUM ; i++){
        printf("LED %d \n",i);
        sprintf(ledPath,LEDC_BLUE_PATH,i);
        export_fd = open(ledPath, O_WRONLY);
        if(-1 == export_fd)
        {
            printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
            return -1;
        }
        len = sprintf(led_write,"%d",color[2]);
        if(-1 == write(export_fd, led_write, len))
        {
            printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
            close(export_fd);
            return -1;
        }
        close(export_fd);
        if(i&gt;3){
            sprintf(ledPath,LEDC_BLUE_PATH,i-3);
            export_fd = open(ledPath, O_WRONLY);
            if(-1 == export_fd)
            {
                printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
                return -1;
            }
            len = sprintf(led_write,"%d",0);
            if(-1 == write(export_fd, led_write, len))
            {
                printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
                close(export_fd);
                return -1;
            }
            close(export_fd);
        }
        else {
            sprintf(ledPath,LEDC_BLUE_PATH,i+29);
            export_fd = open(ledPath, O_WRONLY);
            if(-1 == export_fd)
            {
                printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
                return -1;
            }
            len = sprintf(led_write,"%d",0);
            if(-1 == write(export_fd, led_write, len))
            {
                printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
                close(export_fd);
                return -1;
            }
            close(export_fd);
        }
    
        
        delay_ms(50);
    }
    
    }
    printf("demo 1 end \n");
    return 1;
}



int LED_Color(int led,uint8 R,uint8 G,uint8 B)
{
    int len;
    char ledPath[45] = {0};
    char led_write[3] = {0};

    sprintf(ledPath,LEDC_BLUE_PATH,led);
    export_fd = open(ledPath, O_WRONLY);
    if(-1 == export_fd)
    {
        printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
        return -1;
    }
    len = sprintf(led_write,"%d",B);
    if(-1 == write(export_fd, led_write, len))
    {
        printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
        close(export_fd);
        return -1;
    }
    close(export_fd);

    sprintf(ledPath,LEDC_GREEN_PATH,led);
    export_fd = open(ledPath, O_WRONLY);
    if(-1 == export_fd)
    {
        printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
        return -1;
    }
    len = sprintf(led_write,"%d",G);
    if(-1 == write(export_fd, led_write, len))
    {
        printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
        close(export_fd);
        return -1;
    }
    close(export_fd);

    sprintf(ledPath,LEDC_RED_PATH,led);
    export_fd = open(ledPath, O_WRONLY);
    if(-1 == export_fd)
    {
        printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
        return -1;
    }
    len = sprintf(led_write,"%d",R);
    if(-1 == write(export_fd, led_write, len))
    {
        printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
        close(export_fd);
        return -1;
    }
    close(export_fd);
}


int ColorDecode(uint32 color ,uint8 *R,uint8 *G,uint8 *B){
    *R = (color &gt;&gt; 16) &amp; 0x000011;
    *G = (color &gt;&gt; 8 ) &amp; 0x000011;
    *B = (color &gt;&gt; 0 ) &amp; 0x000011;
}

int LiuSui3(){
    int i;
    int mode = 0;
    uint8 R= 0 ,G = 0,B = 0;
    while(1){
        if(i == 32)
        {
            i = 0;
            mode += 1;
        }
        if(mode == 0){
            ColorDecode(MediumTurquoise,&amp;R,&amp;G,&amp;B);
        }
        if (mode == 1){
            ColorDecode(Gold,&amp;R,&amp;G,&amp;B);
        }
        if(mode == 2){
            ColorDecode(DarkGray,&amp;R,&amp;G,&amp;B);
        }
        if(mode == 3)
        {
            mode = 0;
            printf("delate 0\n");
        }
        LED_Color(i,R,G,B);
        i++;
        delay_ms(20);
    }
    return 1;
}
</code></pre>
<p dir="auto"><code>Makefile</code></p>
<pre><code>CC := riscv64-unknown-linux-gnu-gcc

target = ws2812

LDFLAGS ?= -lm
CFLAGS ?= -I .



CSRCS += ws2812.c
MAINSRC  = main.c


all : default


OBJEXT ?= .o

COBJS = $(CSRCS:.c=$(OBJEXT))
MAINOBJ = $(MAINSRC:.c=$(OBJEXT))

# SRCS =  $(CSRCS) $(MAINSRC)s
%.o: %.c
	@$(CC)  $(CFLAGS) -c $&lt; -o $@
	@echo "CC $&lt;"

default:  $(COBJS) $(MAINOBJ)
	$(CC) -o $(target) $(MAINOBJ)  $(COBJS) $(LDFLAGS)
	

clean: 
	rm -f $(target) $(AOBJS) $(COBJS) $(MAINOBJ)
</code></pre>
]]></description><link>https://bbs.aw-ol.com/topic/424/模块第三弹-哪吒中的ledc的驱动</link><generator>RSS for Node</generator><lastBuildDate>Thu, 11 Jun 2026 17:07:03 GMT</lastBuildDate><atom:link href="https://bbs.aw-ol.com/topic/424.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 14 Sep 2021 11:27:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to 【模块第三弹】哪吒中的LEDC的驱动 on Wed, 10 Dec 2025 08:55:30 GMT]]></title><description><![CDATA[<p dir="auto">测试200个灯没问题；大于32个灯，驱动程序自动DMA传输，小于32，CPU传输</p>
]]></description><link>https://bbs.aw-ol.com/post/27727</link><guid isPermaLink="true">https://bbs.aw-ol.com/post/27727</guid><dc:creator><![CDATA[huali]]></dc:creator><pubDate>Wed, 10 Dec 2025 08:55:30 GMT</pubDate></item><item><title><![CDATA[Reply to 【模块第三弹】哪吒中的LEDC的驱动 on Tue, 16 May 2023 02:29:35 GMT]]></title><description><![CDATA[<p dir="auto">LEDC在一个机型上要真正用起来，的确是个比较吸引视觉的部件，但32还是少了点</p>
]]></description><link>https://bbs.aw-ol.com/post/15766</link><guid isPermaLink="true">https://bbs.aw-ol.com/post/15766</guid><dc:creator><![CDATA[HQEmbed]]></dc:creator><pubDate>Tue, 16 May 2023 02:29:35 GMT</pubDate></item><item><title><![CDATA[Reply to 【模块第三弹】哪吒中的LEDC的驱动 on Tue, 16 May 2023 02:24:10 GMT]]></title><description><![CDATA[<p dir="auto">不错，买了根灯条回来，效果是挺漂亮的。不过FIFO最大32的深度，如果更多颗灯要怎么编程呢？</p>
]]></description><link>https://bbs.aw-ol.com/post/15765</link><guid isPermaLink="true">https://bbs.aw-ol.com/post/15765</guid><dc:creator><![CDATA[HQEmbed]]></dc:creator><pubDate>Tue, 16 May 2023 02:24:10 GMT</pubDate></item><item><title><![CDATA[Reply to 【模块第三弹】哪吒中的LEDC的驱动 on Tue, 14 Sep 2021 12:40:30 GMT]]></title><description><![CDATA[<p dir="auto"><img src="/assets/uploads/files/1631623200840-2212845286340231.jpg" alt="2212845286340231.jpg" class=" img-responsive img-markdown" width="1200" height="901" /></p>
<p dir="auto"><img src="/assets/uploads/files/1631623206373-2212934496874572.jpg" alt="2212934496874572.jpg" class=" img-responsive img-markdown" width="1200" height="674" /></p>
<p dir="auto">闲鱼上面看到的，显示屏是WS2812</p>
]]></description><link>https://bbs.aw-ol.com/post/1885</link><guid isPermaLink="true">https://bbs.aw-ol.com/post/1885</guid><dc:creator><![CDATA[tigger]]></dc:creator><pubDate>Tue, 14 Sep 2021 12:40:30 GMT</pubDate></item><item><title><![CDATA[Reply to 【模块第三弹】哪吒中的LEDC的驱动 on Tue, 14 Sep 2021 12:22:55 GMT]]></title><description><![CDATA[<p dir="auto">Holihi~看看实际效果</p>
]]></description><link>https://bbs.aw-ol.com/post/1884</link><guid isPermaLink="true">https://bbs.aw-ol.com/post/1884</guid><dc:creator><![CDATA[xiaowenge]]></dc:creator><pubDate>Tue, 14 Sep 2021 12:22:55 GMT</pubDate></item><item><title><![CDATA[Reply to 【模块第三弹】哪吒中的LEDC的驱动 on Tue, 14 Sep 2021 11:30:05 GMT]]></title><description><![CDATA[<p dir="auto">代码文件：<a href="/assets/uploads/files/1631619003012-ws2812.tar.gz">WS2812.tar.gz</a></p>
]]></description><link>https://bbs.aw-ol.com/post/1883</link><guid isPermaLink="true">https://bbs.aw-ol.com/post/1883</guid><dc:creator><![CDATA[BedRock]]></dc:creator><pubDate>Tue, 14 Sep 2021 11:30:05 GMT</pubDate></item></channel></rss>