[PIC®/AVR®/dsPIC®产品] AVR64DD32使用SPI驱动WS2812B方法

[复制链接]
813|0
 楼主| gaoyang9992006 发表于 2024-7-30 20:52 | 显示全部楼层 |阅读模式
根据朴素的SPI驱动WS2812B方法,要让SPI工作在8MHz,然后发送0xF8表示1,发送0xC0表示0.为了轻松分频出8MHz时钟信号,可设置主时钟为16MHz,这样轻松的通过二分频就得到了8MHz。


  1. #include "mcc_generated_files/system/system.h"

  2. #include <util/delay.h>

  3. #include <string.h>

  4. #define NUMBER_OF_LEDS    ( 20 )
  5. #define RGB_COUNT         ( 3 )
  6. #define LED_MIN_BRIGHT    ( 0 )
  7. #define RX_MAX_BUFFER     ( 255 )

  8. typedef struct COLOR
  9. {
  10.     uint8_t redChannel;
  11.     uint8_t greenChannel;
  12.     uint8_t blueChannel;
  13. } color_t;


  14. void ws2812_send(unsigned char x)
  15. {
  16.     for(int i=0;i<8;i++)
  17.     {
  18.         x<<=i;
  19.         if(x&0x80)
  20.         {
  21.             SPI0_ByteExchange(0xF8);
  22.         }
  23.         else
  24.         {
  25.             SPI0_ByteExchange(0xC0);
  26.         }
  27.     }
  28. }

  29. static void WriteNeopixel(color_t const color)
  30. {
  31.     ws2812_send(color.greenChannel);   
  32.     ws2812_send(color.redChannel);   
  33.     ws2812_send(color.blueChannel);
  34. }

  35. static void WriteLEDsString(color_t const * const frame)
  36. {
  37.     for (uint8_t idx = 0; idx < NUMBER_OF_LEDS; ++idx)
  38.     {
  39.         WriteNeopixel(frame[idx]);
  40.     }
  41. }



  42. static void ClearLEDsString(void)
  43. {
  44.     static color_t off = { LED_MIN_BRIGHT, LED_MIN_BRIGHT, LED_MIN_BRIGHT };
  45.      
  46.     for (uint8_t idx = 0; idx < NUMBER_OF_LEDS; ++idx)
  47.     {
  48.         WriteNeopixel(off);
  49.     }
  50. }



  51. void main(void)
  52. {
  53.     color_t neopixelsBuffer[NUMBER_OF_LEDS] = {
  54.         { 64, 100, 64 }, { 64, 64,  0 }, {  0, 64, 64 }, {  0, 64,  0 },
  55.         { 64,  0, 64 }, { 64,  0,  0 }, {  0,  0, 64 }, {  30,  10,  2 },
  56.         { 96, 96, 96 }, { 48, 48, 48 }, { 24, 24, 24 }, { 12, 24, 12 },
  57.         {  36,  36,  6 }, {  3,  33,  33 }, {  16,  16,  1 }, {  50,  0,  0 },
  58.         {  6,  60,  6 }, {  3,  3,  30 }, {  10,  1,  10 }, {  0,  10,  0 }
  59.     };
  60.    
  61.     SYSTEM_Initialize();

  62.    
  63.     // Initialize  LEDs
  64.     ClearLEDsString();
  65.     _delay_ms(1000);
  66.     WriteLEDsString(neopixelsBuffer);
  67.     while (true)
  68.     {
  69.         color_t temp;
  70.         temp.blueChannel =neopixelsBuffer[0].blueChannel;
  71.         temp.greenChannel=neopixelsBuffer[0].greenChannel;
  72.         temp.redChannel  =neopixelsBuffer[0].redChannel;
  73.         
  74.         for(int i=0;i<19;i++)
  75.         {
  76.             neopixelsBuffer[i].blueChannel =neopixelsBuffer[i+1].blueChannel;
  77.             neopixelsBuffer[i].greenChannel=neopixelsBuffer[i+1].greenChannel;
  78.             neopixelsBuffer[i].redChannel  =neopixelsBuffer[i+1].redChannel;
  79.         }
  80.         neopixelsBuffer[19].blueChannel =temp.blueChannel;
  81.         neopixelsBuffer[19].greenChannel=temp.greenChannel;
  82.         neopixelsBuffer[19].redChannel  =temp.redChannel;
  83.         _delay_ms(250);
  84.         WriteLEDsString(neopixelsBuffer);
  85.     }   
  86. }
最后上效果

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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:如果你觉得我的分享或者答复还可以,请给我点赞,谢谢。

2052

主题

16403

帖子

222

粉丝
快速回复 在线客服 返回列表 返回顶部