1248 views|13 replies

109

Posts

3

Resources
The OP
 

[Renesas RA8D1 Review] Part 4: SPI driving ws2812 full-color LED [Copy link]

 
This post was last edited by eew_cT3H5d on 2024-7-23 23:58

First look at the effect:

WS2812B-5050 is an intelligent externally controlled LED light source that integrates control circuit and light-emitting circuit. Its appearance adopts the latest molding packaging technology, and the IC and light-emitting chip are packaged in a 5050 package size. Each component is a pixel. The pixel contains an intelligent digital interface data latch signal shaping and amplification drive circuit, a high-precision internal oscillator and a programmable constant current control part, which effectively ensures that the color of the pixel light is highly consistent.

It can display the color display light composed of the three primary colors of RGB. It is packaged as 5050 and has four pins, namely VCC, VSS, DIN, and DOUT. Its control protocol is very simple, and serial asynchronous signal transmission can be performed through a signal line


The cascade control protocol of WS2812 is very simple. Different high and low level pulses are used in serial communication to represent data 0,1 encoding. By sending three groups of 24-bit encoding, the colors of three cascaded WS2812 lights can be controlled. The GRB encoding corresponding to each group of 24 bits is shown below. The color sending order is GRB, with the high bit of the byte first.


The WS2812 driver chip uses a single-line return-to-zero code communication method. After the chip is powered on and reset, the DIN terminal receives the data transmitted from the controller. The first 24-bit data is extracted by the first chip and sent to the data latch inside the chip. The remaining data is shaped and amplified by the internal shaping processing circuit, and then forwarded to the next cascade chip through the DO port. After each chip is transmitted, the signal is reduced by 24 bits. The chip uses automatic shaping and forwarding technology, so that the number of cascades of the chip is not limited by signal transmission, but only by the signal transmission speed requirement.

Drive waveform logic analyzer test:

Logic analyzer waveform display:

Renesas RA8D1 controls ws2812 via SPI bus

Step 1: Continue to add SPI driver module in the previous project

Configure the SPI channel. By configuring channel 3, you can directly connect the SPI pins P309, P311, and P310 to the ws2812 directly through the lead pins of the development board.

After querying, the configuration speed of SPI is set to 7.5M, and the related clock can be 60Mhz or 120Mhz

Configure the system clock, use the internal clock, and configure the related clock to 120Mhz

Step 2: Add ws2812 underlying driver library

ws2812.c file

/*
 * ws2812.c
 *
 *  Created on: 2023年10月31日
 *      Author: Administrator
 */

#include <WS2812/ws2812.h>
#include "hal_data.h"
extern fsp_err_t err ;
extern volatile bool g_transfer_complete ;

//灯条显存SPI数据缓存
uint8_t gWs2812bDat_SPI[WS2812B_AMOUNT * 24+88] = {0};
//灯条显存
tWs2812bCache_TypeDef gWs2812bDat[WS2812B_AMOUNT] = {

//R    G      B
0XFF, 0X00, 0X00,   //0
0X00, 0XFF, 0X00,   //1
0X00, 0X00, 0XFF,   //2
0X00, 0XFF, 0XFF,   //3
0XFF, 0X00, 0XFF,   //4
0XFF, 0XFF, 0X00,   //5
0XFF, 0XFF, 0XFF,   //6
0X00, 0X00, 0X00,   //7
};

void WS2812b_Set(uint16_t Ws2b812b_NUM, uint8_t r,uint8_t g,uint8_t b)
{
    uint8_t *pR = &gWs2812bDat_SPI[88+(Ws2b812b_NUM) * 24 + 8];
    uint8_t *pG = &gWs2812bDat_SPI[88+(Ws2b812b_NUM) * 24];
    uint8_t *pB = &gWs2812bDat_SPI[88+(Ws2b812b_NUM) * 24 + 16];

    for(uint8_t i = 0; i <  8; i++) {
        if(g & 0x80) {
            *pG = CODE_1;
        }
        else {
            *pG = CODE_0;
        }
        if(r & 0x80) {
            *pR = CODE_1;
        }
        else {
            *pR = CODE_0;
        }
        if(b & 0x80) {
            *pB = CODE_1;
        }
        else {
            *pB = CODE_0;
        }
        r <<= 1;
        g <<= 1;
        b <<= 1;
        pR++;
        pG++;
        pB++;
    }
}
void WS2812B_Task(void)
{
    uint8_t dat = 0;
    for(int i=0;i<88;i++)
    {
        gWs2812bDat_SPI[i]=0;

    }
    //将gWs2812bDat数据解析成SPI数据
    for(uint8_t iLED = 0; iLED < WS2812B_AMOUNT; iLED++)
    {
        WS2812b_Set(iLED, gWs2812bDat[iLED].R, gWs2812bDat[iLED].G, gWs2812bDat[iLED].B);
    }
    //总线输出数据

    /* Send the reset command */
    g_transfer_complete = false;
    err = R_SCI_B_SPI_Write(&g_sci_spi3_ctrl, gWs2812bDat_SPI, sizeof(gWs2812bDat_SPI), SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }
//    //使总线输出低电平
//    g_transfer_complete = false;
//     err = R_SCI_SPI_Write(&g_spi0_ctrl, dat, 1, SPI_BIT_WIDTH_8_BITS);
//     assert(FSP_SUCCESS == err);
//     /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
//     while (  g_transfer_complete==false)
//     {
//         ;
//     }

    //帧信号:一个大于50us的低电平
     R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MILLISECONDS);
}


ws2812.h file

/*
 * ws2812.h
 *
 *  Created on: 2023年10月31日
 *      Author: Administrator
 */

#ifndef _WS2812_H_
#define _WS2812_H_

#include <stdint.h>

//            编码 0 : 11000000
#define CODE_0      0xc0
//            编码 1 : 11111000
#define CODE_1      0xF8
/*ws2812b灯珠数量*/
#define WS2812B_AMOUNT      8

typedef struct
{
    uint8_t R;
    uint8_t G;
    uint8_t B;
} tWs2812bCache_TypeDef;

extern tWs2812bCache_TypeDef gWs2812bDat[WS2812B_AMOUNT];

void WS2812b_Set(uint16_t Ws2b812b_NUM, uint8_t r,uint8_t g,uint8_t b);
void WS2812B_Task(void);


#endif /* WS2812_H_ */

This is the main program running code, which allows ws2812 to display different colors

Step 3: Compile the program

Step 4: Program running effect: Successfully light up the ws2812 full-color LED, SPI drive is successful at one time

Video Demonstration:

ws2812

References:

1.https://blog.csdn.net/qq_24312945/article/details/134152211

2.https://en.eeworld.com/bbs/thread-1267092-1-1.html

This post is from Renesas Electronics MCUs

Latest reply

marvelous. This effect looks cool   Details Published on 2024-7-29 14:31

1w

Posts

204

Resources
2
 

This lamp looks really good!

This post is from Renesas Electronics MCUs
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle

Comments

Add a beautiful lampshade and it can be used as a bedside lamp  Details Published on 2024-7-24 23:26
 
Personal signature

玩板看这里:

http://en.eeworld.com/bbs/elecplay.html

EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!

 

9157

Posts

6

Resources
3
 

The effect is very cool

This post is from Renesas Electronics MCUs
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle

Comments

WS2812 is very playable and has a great display effect  Details Published on 2024-7-24 23:35
 
 
 

222

Posts

3

Resources
4
 

Full of useful information, full display effect

This post is from Renesas Electronics MCUs

Comments

WS2812 display consistency is still very good  Details Published on 2024-7-24 23:33
 
 
 

6818

Posts

11

Resources
5
 

It is indeed a good tutorial for learning. The result ADC acquisition can be used in new ways.

This post is from Renesas Electronics MCUs

Comments

Does ADC sample sound? Display different colors according to the rhythm of music  Details Published on 2024-7-24 23:32
 
 
 

109

Posts

3

Resources
6
 
okhxyyo posted on 2024-7-24 10:27 This looks really good! ! ! This lamp is really beautiful

Add a beautiful lampshade and it can be used as a bedside lamp

This post is from Renesas Electronics MCUs
 
 
 

109

Posts

3

Resources
7
 
lugl4313820 posted on 2024-7-24 16:43 It is indeed a good tutorial for learning. The result of ADC acquisition can be used to play new tricks.

Does ADC sample sound? Display different colors according to the rhythm of music

This post is from Renesas Electronics MCUs
 
 
 

109

Posts

3

Resources
8
 
Maker_kun posted on 2024-7-24 11:46 Full of useful information, full display effect

WS2812 display consistency is still very good

This post is from Renesas Electronics MCUs
 
 
 

109

Posts

3

Resources
9
 
eric_wang posted on 2024-7-24 10:27 The effect is very cool

WS2812 is very playable and has a great display effect

This post is from Renesas Electronics MCUs
 
 
 

5998

Posts

6

Resources
10
 

How to control the interval of WS2812B timing?

This post is from Renesas Electronics MCUs

Comments

Is the interval data time for each light?  Details Published on 2024-7-29 13:58
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

1573

Posts

0

Resources
11
 

Thanks for sharing. The principle of WS2812 is explained very clearly.

The logic analyzer is used, so advanced

In addition, why use the SPI protocol to drive the WS2812 module? Isn't it just a control signal DIN?

This post is from Renesas Electronics MCUs

Comments

Yes, you only need one data line to control it.  Details Published on 2024-7-29 13:56
 
 
 

109

Posts

3

Resources
12
 
se7ens posted on 2024-7-26 18:07 Thank you for sharing. The principle of WS2812 is explained very clearly. The logic analyzer is used. It's great. In addition, why use SPI protocol to drive WS281...

Yes, you only need one data line to control it.

This post is from Renesas Electronics MCUs
 
 
 

109

Posts

3

Resources
13
 
Qintianqintian0303 posted on 2024-7-26 13:46 How to control the interval of WS2812B timing sequence?

Is the interval data time for each light?

This post is from Renesas Electronics MCUs
 
 
 

623

Posts

0

Resources
14
 

marvelous.

This effect looks cool

This post is from Renesas Electronics MCUs
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list