This repository provides an MPLAB® X project with an MPLAB® Code Configurator (MCC) generated code example for interfacing the CCL (Configurable Custom Logic) and SPI (Serial Peripheral Interface) peripherals with the WS2812 LED. The messages compatible with the WS2812 LED are formed from ones and zeros with a specific duration and form. The application translates the messages sent by SPI into messages intelligible by WS2812, using CCL. Furthermore, it creates a light game on a WS2812 LED band.
More details and code examples on the AVR128DA48 can be found at the following links:
- MPLAB X IDE v6.10 or newer
- MPLAB® XC8 v2.41 or newer
- AVR-Dx Series Device Pack v2.3.272 or newer
- AVR128DA48 Curiosity Nano Development Board is used as a test platform.
- WS2812 LED Stripe
- 12V Charger / Voltage Source for the LEDs
To program the Curiosity Nano board with this MPLAB X project, follow the steps provided in the How to Program the Curiosity Nano Board chapter.
The following peripheral and clock configurations are set up using MCC Melody for the AVR128DA48:
-
CLKCTRL:
- Clock Selection: Internal high-frequency oscillator
- Internal Oscillator Frequency: 1-32 MHz internal oscillator
- Oscillator Frequency Selection: 24 MHz system clock
-
SPI0:
- Enable SPI: Enabled
- Modes: Host
- SPI Mode: 1
- Desired Clock Speed (Hz): 750000 (750 kHz)
-
CCL:
- Enable CCL: Enabled
- LUT0:
- Enable LUT: Enabled
- Enable LUT Output: Enabled
- Input 0: TCA0 (WO0)
- Input 1: SPI0 (MOSI)
- Input 2: TCA0 (WO2)
- Gate Type: Custom
- Output Result: 0xE0
-
TCA0:
- Enable Timer: Enabled
- Clock Selection: System Clock
- Timer Mode: 16 Bit (Normal)
- Count Direction: UP
- Requested Timeout (s): 0.0001 (100 μs)
- Enable Count on Event Input B: Enabled
- Event Action B: Restart on Positive Event Edge
- Waveform Generation Mode: Single Slope PWM
- Enable Channel 0: Enabled
- Enable Channel 2: Enabled
- Duty Cycle 0 (%): 0.4%
- Duty Cycle 2 (%): 0.7%
-
EVSYS:
- Generators: SPI0_SCK -> CHANNEL0
- Users: CHANNEL0 -> TCA0CNTB
- Routing: SPI0_SCK -> CHANNEL0 -> TCA0CNTB
Pin | Configuration |
---|---|
PA0 (TCA0 WO0) | Digital Output |
PA2 (TCA0 WO2) | Digital Output |
PA3 (LUT0 OUT) | Digital Output |
PA4 (SPI0 MOSI) | Digital Output |
PA6 (SPI0 SCK) | Digital Output |
PA7 (SPI0 SS) | Digital Output |
Note: Only PA3 (LUT0 OUT) will be used to transmit data to the WS2812 LED. The rest of the pins are routed outside for intermediary signals visualization.
The output waveforms are shown in the figure below. SPI MOSI is the data sent through SPI, and the LUT0 OUT is the translated message, intelligible by WS2812 LED.
The light game is created on a LED strip which allows control of LEDs in groups of three. The LED groups are lit from one end to another continuously.
Note: The number of LEDs (groups of LEDs) must be configured in firmware. See NUMBER_OF_LEDS
macro define.
The demo shows how to interface WS2812 LED with the SPI peripheral using CCL and TCA.
This chapter shows how to use the MPLAB X IDE to program an AVR® device with an Example_Project.X. This can be applied for any other projects.
-
Connect the board to the PC.
-
Open the Example_Project.X project in MPLAB X IDE.
-
Set the Example_Project.X project as main project.
-
Clean and build the Example_Project.X project.
-
Select the AVRxxxxx Curiosity Nano in the Connected Hardware Tool section of the project settings:
-
Program the project to the board.