[STM32F1] WS2812B渐变色流水算法+PWM DMA驱动

[复制链接]
1704|4
 楼主| biechedan 发表于 2024-2-25 23:03 | 显示全部楼层 |阅读模式


  1. uint32_t *RGB;//颜色列表数组

  2. void LED_Init(void);
  3. void PWM_Init(void);
  4. uint32_t set_Color_Loop(uint8_t color_length);
  5. void out_RGB(uint16_t data_Max_Length,uint16_t colorwidth);

  6. int main(void)
  7. {
  8.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  9.     uart_init(115200);
  10.     delay_init();
  11.     LED_Init();
  12.       
  13.         WS2812B_TIM_init();
  14.         uint32_t len = set_Color_Loop(32);//每两种颜色之间的位数
  15.       
  16.     while(1)
  17.     {
  18.                 out_RGB(len,8);//8个灯,每次输出8个数据
  19.                 delay_ms(80);
  20.     }
  21. }

  22. /**
  23. * colorwidth 每次获取多少位
  24. */

  25. void out_RGB(uint16_t data_Max_Length,uint16_t colorwidth)
  26. {
  27.         static uint32_t rgb_position = 0;
  28.         for(uint32_t i = 0;i < colorwidth; i++){
  29.                 uint16_t c = (rgb_position + i) % data_Max_Length;//末尾颜色数据结束时将首位的收据填充到数组
  30.                 send_Data(RGB[c]);
  31.         }
  32.         rgb_position ++;
  33.         rgb_position %= data_Max_Length;
  34. }

  35. /**
  36. * color_length 每种颜色渐变最大长度,总颜色长度:color_length * 3
  37. * return 颜色数据总长度
  38. */
  39. uint32_t set_Color_Loop(uint8_t color_length)
  40. {
  41.         RGB = (uint32_t*)malloc(color_length*3*sizeof(uint32_t));//分配数组大小,(所有渐变色颜色长度)
  42.         color_length -= 1;
  43.         for(uint8_t i=0;i<=color_length;i++)
  44.         {
  45.                 RGB[i] = (((0xff/color_length)*i)<<8)|((0xff/color_length)*(color_length-i));                                                //蓝到绿
  46.                 RGB[color_length + 1 +i] = (((0xff/color_length)*i)<<16)|((0xff/color_length)*(color_length-i))<<8; //绿到红
  47.                 RGB[((color_length+1)*2) +i] = ((0xff/color_length)*i)|((0xff/color_length)*(color_length-i))<<16;  //红到蓝
  48.         }
  49.         return color_length*3;
  50. }


呐咯密密 发表于 2024-2-26 10:04 | 显示全部楼层
可以尝试FsatLED库。效果更棒
ZYQFBW 发表于 2024-3-16 08:54 | 显示全部楼层
呐咯密密 发表于 2024-2-26 10:04
可以尝试FsatLED库。效果更棒

这哈库函数?在哪下载?
CarterERO 发表于 2024-3-18 10:17 来自手机 | 显示全部楼层
ZYQFBW 发表于 2024-3-16 08:54
这哈库函数?在哪下载?

Arduino第三方库
小夏天的大西瓜 发表于 2024-3-18 17:48 | 显示全部楼层
外部硬件还是比较重要的,一般就是PWM进行调压改变颜色的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

296

主题

8226

帖子

12

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