WS2812 LED demo code

The WS2812 is an SMD RGB LED controlled by SPI. Only four Pins are needed. Operating voltage, ground, data-in (DIN) and data-out (DOUT). In the LED next to the LED chips an LED controller is housed, which takes over the control of the LEDs.

This demonstration contains three operational demonstrations, and two methods of driving the WS2812 LEDs.
The demos have a speed control using a pot, this pots adjusts the timing. A switch change the demos in operation.

Demos are:
1. Chaser - LED chaser
2. Flash All LEDs
3. Xylon Chaser, also known as Knight Rider

Methods of driving the WS2812:
1. CLC approach using the Microchip Configurable Logic Cells (CLC) to create a specialised SPI signal in terms of timing
2. Bit Banging a SPI signal, again in terms of timing.

 

Youtube video see here

 

 

 

Operation:
By default the Chaser (#1) will be operational when applying power to the microcontroller and the WS2812 LEDs.
Pressing the Switch will change the operational to the Flash All LEDs (#2), pressing the Switch again with return to Chaser operation.
To select the Xylon Chase press the switch when applying power to the microcontroller and the WS2812 LEDs.

Selecting the the specialised SPI signal method:
Using the Microchip CLC method as detailed in AN1606.
This method can be selected by defining the constants as follows.

        #define UseWS2812PWMSendData
        #define WS2812SendData WS2812PWMSendData

Using the bit Banging method.
This method can be selected by defining the constants as follows.

    #define UseWS2812BitBangSendData
    #define WS2812SendData WS2812BitBangSendData

These methods are mutually exclusive - choose either CLC or Bit Banging, define the TWO constants. The constants ensure the code is configured correctly.

Configuration:
WS2812 DIN is connected to PORTC.7.
Pot is connected to PORTA.4
Switch is pulled high and connected to PORTA.5

General commentary:
The use of CLC is detailed in AN1606. Please review this document.

The timing of the WS2812 is absolutely critical. Please review the WS2812 datasheet for the specific timing that are required.

The Chaser demonstration simply writes to the WS2812 (for the number of LEDs) and indexes through the LEDS enabling the select color on each pass through the sequence.
The Flash ALL LEDS simply writes to all the LEDS the same color.
The Xylon Chaser uses an ARRAY to hold the pattern. When a bit in 1 then the LEDs with be turn on.

To be continued

The whole Story how the Development evolves is here in the Forum.