打印
[PIC®/AVR®/dsPIC®产品]

AVR64DD32使用SPI驱动WS2812B方法

[复制链接]
282|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
根据朴素的SPI驱动WS2812B方法,要让SPI工作在8MHz,然后发送0xF8表示1,发送0xC0表示0.为了轻松分频出8MHz时钟信号,可设置主时钟为16MHz,这样轻松的通过二分频就得到了8MHz。


#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);
    }   
}
最后上效果

至于其中细节和更多不为人知的秘密下个月有空再细说。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:西安公路研究院南京院
简介:主要工作从事监控网络与通信网络设计,以及从事基于嵌入式的通信与控制设备研发。擅长单片机嵌入式系统物联网设备开发,音频功放电路开发。

1935

主题

15824

帖子

204

粉丝