#include "mcc_generated_files/system/system.h"
#include <util/delay.h>
#include <string.h>
#define NUMBER_OF_LEDS ( 20 )
#define RGB_COUNT ( 3 )
#define LED_MIN_BRIGHT ( 0 )
#define RX_MAX_BUFFER ( 255 )
typedef struct COLOR
{
uint8_t redChannel;
uint8_t greenChannel;
uint8_t blueChannel;
} color_t;
void ws2812_send(unsigned char x)
{
for(int i=0;i<8;i++)
{
x<<=i;
if(x&0x80)
{
SPI0_ByteExchange(0xF8);
}
else
{
SPI0_ByteExchange(0xC0);
}
}
}
static void WriteNeopixel(color_t const color)
{
ws2812_send(color.greenChannel);
ws2812_send(color.redChannel);
ws2812_send(color.blueChannel);
}
static void WriteLEDsString(color_t const * const frame)
{
for (uint8_t idx = 0; idx < NUMBER_OF_LEDS; ++idx)
{
WriteNeopixel(frame[idx]);
}
}
static void ClearLEDsString(void)
{
static color_t off = { LED_MIN_BRIGHT, LED_MIN_BRIGHT, LED_MIN_BRIGHT };
for (uint8_t idx = 0; idx < NUMBER_OF_LEDS; ++idx)
{
WriteNeopixel(off);
}
}
void main(void)
{
color_t neopixelsBuffer[NUMBER_OF_LEDS] = {
{ 64, 100, 64 }, { 64, 64, 0 }, { 0, 64, 64 }, { 0, 64, 0 },
{ 64, 0, 64 }, { 64, 0, 0 }, { 0, 0, 64 }, { 30, 10, 2 },
{ 96, 96, 96 }, { 48, 48, 48 }, { 24, 24, 24 }, { 12, 24, 12 },
{ 36, 36, 6 }, { 3, 33, 33 }, { 16, 16, 1 }, { 50, 0, 0 },
{ 6, 60, 6 }, { 3, 3, 30 }, { 10, 1, 10 }, { 0, 10, 0 }
};
SYSTEM_Initialize();
// Initialize LEDs
ClearLEDsString();
_delay_ms(1000);
WriteLEDsString(neopixelsBuffer);
while (true)
{
color_t temp;
temp.blueChannel =neopixelsBuffer[0].blueChannel;
temp.greenChannel=neopixelsBuffer[0].greenChannel;
temp.redChannel =neopixelsBuffer[0].redChannel;
for(int i=0;i<19;i++)
{
neopixelsBuffer[i].blueChannel =neopixelsBuffer[i+1].blueChannel;
neopixelsBuffer[i].greenChannel=neopixelsBuffer[i+1].greenChannel;
neopixelsBuffer[i].redChannel =neopixelsBuffer[i+1].redChannel;
}
neopixelsBuffer[19].blueChannel =temp.blueChannel;
neopixelsBuffer[19].greenChannel=temp.greenChannel;
neopixelsBuffer[19].redChannel =temp.redChannel;
_delay_ms(250);
WriteLEDsString(neopixelsBuffer);
}
}
最后上效果