找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 24976|回復(fù): 39
打印 上一主題 下一主題
收起左側(cè)

WS2812B漸變色流水算法+PWM DMA驅(qū)動(dòng)STM32源程序

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
最近在學(xué)習(xí)STM32,到定時(shí)器與DMA章節(jié),正巧手頭有先前買的WS2812B模塊,查找相關(guān)資料,于是成功點(diǎn)亮了,
又按照自己想法和思路寫了一個(gè)漸變色算法,和漸變色流水功能,

可自定義燈的數(shù)量和漸變色的深度

漸變色原理:
假如紅藍(lán)兩色變換
紅(亮度):10   8   6   4   2   0
藍(lán)(亮度):0     2   4   6   8   10


這樣就形成兩種顏色的過度
加大其分辨率,兩顏色之間顏色差越小,生成顏色數(shù)據(jù)越多


在流水過程中,顏色取到數(shù)組末尾時(shí),要將數(shù)組首端的數(shù)據(jù)加在末尾,使其顏色無差別連接

控制代碼:

生成三種顏色數(shù)據(jù)數(shù)組
  1. /**
  2. * color_length 每種顏色漸變最大長(zhǎng)度,總顏色長(zhǎng)度:color_length * 3
  3. * return 顏色數(shù)據(jù)總長(zhǎng)度
  4. */
  5. uint32_t set_Color_Loop(uint8_t color_length)
  6. {
  7.     RGB = (uint32_t*)malloc(color_length*3*sizeof(uint32_t));//分配數(shù)組大小,(所有漸變色顏色長(zhǎng)度)
  8.     color_length -= 1;
  9.     for(uint8_t i=0;i<=color_length;i++)
  10.     {
  11.         RGB[i] = (((0xff/color_length)*i)<<8)|((0xff/color_length)*(color_length-i));                                       //藍(lán)到綠
  12.         RGB[color_length + 1 +i] = (((0xff/color_length)*i)<<16)|((0xff/color_length)*(color_length-i))<<8; //綠到紅
  13.         RGB[((color_length+1)*2) +i] = ((0xff/color_length)*i)|((0xff/color_length)*(color_length-i))<<16;   //紅到藍(lán)
  14.     }
  15.     return color_length*3;
  16. }


  17. 無間斷位移獲取8位顏色數(shù)據(jù)
  18. /**
  19. * colorwidth 每次獲取多少位
  20. */

  21. void out_RGB(uint16_t data_Max_Length,uint16_t colorwidth)
  22. {
  23.     static uint32_t rgb_position = 0;
  24.     for(uint32_t i = 0;i < colorwidth; i++){
  25.         uint16_t c = (rgb_position + i) % data_Max_Length;//末尾顏色數(shù)據(jù)結(jié)束時(shí)將首位的收據(jù)填充到數(shù)組
  26.         send_Data(RGB[c]);
  27.     }
  28.     rgb_position ++;
  29.     rgb_position %= data_Max_Length;
  30. }
復(fù)制代碼


兩種圖片效果



手機(jī)拍不出燈光效果,實(shí)際肉眼看還是非常漂亮的
視頻效果:
游客,本帖隱藏的內(nèi)容需要積分高于 1 才可瀏覽,您當(dāng)前積分為 0


  1. #include "sys.h"
  2. #include "delay.h"
  3. #include "usart.h"
  4. #include "WS2812B.h"
  5. #include <stdlib.h>

  6. uint32_t *RGB;//顏色列表數(shù)組

  7. void LED_Init(void);
  8. void PWM_Init(void);
  9. uint32_t set_Color_Loop(uint8_t color_length);
  10. void out_RGB(uint16_t data_Max_Length,uint16_t colorwidth);

  11. int main(void)
  12. {
  13.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  14.     uart_init(115200);
  15.     delay_init();
  16.     LED_Init();
  17.        
  18.         WS2812B_TIM_init();
  19.         uint32_t len = set_Color_Loop(32);//每?jī)煞N顏色之間的位數(shù)
  20.        
  21.     while(1)
  22.     {
  23.                 out_RGB(len,8);//8個(gè)燈,每次輸出8個(gè)數(shù)據(jù)
  24.                 delay_ms(80);
  25.     }
  26. }

  27. /**
  28. * colorwidth 每次獲取多少位
  29. */

  30. void out_RGB(uint16_t data_Max_Length,uint16_t colorwidth)
  31. {
  32.         static uint32_t rgb_position = 0;
  33.         for(uint32_t i = 0;i < colorwidth; i++){
  34.                 uint16_t c = (rgb_position + i) % data_Max_Length;//末尾顏色數(shù)據(jù)結(jié)束時(shí)將首位的收據(jù)填充到數(shù)組
  35.                 send_Data(RGB[c]);
  36.         }
  37.         rgb_position ++;
  38.         rgb_position %= data_Max_Length;
  39. }

  40. /**
  41. * color_length 每種顏色漸變最大長(zhǎng)度,總顏色長(zhǎng)度:color_length * 3
  42. * return 顏色數(shù)據(jù)總長(zhǎng)度
  43. */
  44. uint32_t set_Color_Loop(uint8_t color_length)
  45. {
  46.         RGB = (uint32_t*)malloc(color_length*3*sizeof(uint32_t));//分配數(shù)組大小,(所有漸變色顏色長(zhǎng)度)
  47.         color_length -= 1;
  48.         for(uint8_t i=0;i<=color_length;i++)
  49.         {
  50.                 RGB[i] = (((0xff/color_length)*i)<<8)|((0xff/color_length)*(color_length-i));                                                //藍(lán)到綠
  51.                 RGB[color_length + 1 +i] = (((0xff/color_length)*i)<<16)|((0xff/color_length)*(color_length-i))<<8; //綠到紅
  52.                 RGB[((color_length+1)*2) +i] = ((0xff/color_length)*i)|((0xff/color_length)*(color_length-i))<<16;  //紅到藍(lán)
  53.         }
  54.         return color_length*3;
  55. }

  56. void LED_Init()
  57. {
  58.     GPIO_InitTypeDef GPIO_InitTypeStruct;
  59.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOB,ENABLE);
  60.     GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  61.     GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_5;
  62.     GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz;
  63.     GPIO_Init(GPIOE,&GPIO_InitTypeStruct);
  64.     GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_5;
  65.     GPIO_Init(GPIOB,&GPIO_InitTypeStruct);
  66.     GPIOB->ODR ^= GPIO_Pin_5;
  67.     GPIOE->ODR ^= GPIO_Pin_5;
  68. }
復(fù)制代碼

PWM+DMA驅(qū)動(dòng)WS2812的代碼請(qǐng)查看附件
WS2812B.rar (295.82 KB, 下載次數(shù): 1132)

評(píng)分

參與人數(shù) 3黑幣 +89 收起 理由
wpppmlah + 30 共享資料的黑幣獎(jiǎng)勵(lì)!
hzd530 + 9
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏32 分享淘帖 頂2 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:603929 發(fā)表于 2019-10-16 08:40 | 只看該作者
正好在做這個(gè),學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

板凳
ID:394041 發(fā)表于 2020-4-10 15:47 | 只看該作者
謝謝分享,學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

地板
ID:168165 發(fā)表于 2020-4-14 20:39 | 只看該作者
不閃動(dòng)呀
回復(fù)

使用道具 舉報(bào)

5#
ID:383785 發(fā)表于 2020-5-29 19:24 | 只看該作者
不錯(cuò) 學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

6#
ID:731155 發(fā)表于 2020-5-30 13:17 | 只看該作者
非常不錯(cuò),感謝分享�。。�!
回復(fù)

使用道具 舉報(bào)

7#
ID:471251 發(fā)表于 2020-6-9 20:00 | 只看該作者

謝謝分享,學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

8#
ID:219315 發(fā)表于 2020-6-12 15:04 | 只看該作者
實(shí)際燒到單片機(jī)里運(yùn)行,燈光效果很漂亮。
回復(fù)

使用道具 舉報(bào)

9#
ID:394041 發(fā)表于 2020-7-1 22:26 來自觸屏版 | 只看該作者
謝謝,有空試一下。
回復(fù)

使用道具 舉報(bào)

10#
ID:610978 發(fā)表于 2020-9-11 11:02 | 只看該作者
感謝樓主分享,下載到板子里可以正常工作,我嘗試吧TIM2換成TIM1就不能亮了,這是為什么呢?
是我哪里漏修改了么?

void WS2812B_TIM_init(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    /* GPIOA Configuration: TIM2 Channel 1 as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    /* Compute the prescaler value */
    //PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = 89; // 800kHz
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
          TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channel1 */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 0;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);

    /* configure DMA */
    /* DMA clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    /* DMA1 Channel6 Config */
    DMA_DeInit(DMA1_Channel2);

    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM1->CCR1;        // physical address of Timer 3 CCR1
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)LED_BYTE_Buffer;                // this is the buffer memory
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;                                                // data shifted from memory to peripheral
    DMA_InitStructure.DMA_BufferSize = 24;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                                        // automatically increase buffer index
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;                                                        // stop DMA feed after buffer size is reached
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

    DMA_Init(DMA1_Channel2, &DMA_InitStructure);

    /* TIM3 CC1 DMA Request enable */
        /* 只能使用通道1 TIMx_UP */
    TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE);
}
回復(fù)

使用道具 舉報(bào)

11#
ID:791100 發(fā)表于 2020-11-10 16:39 | 只看該作者
非常感謝,值得學(xué)習(xí)一下,非常好
回復(fù)

使用道具 舉報(bào)

12#
ID:384489 發(fā)表于 2020-11-20 15:06 | 只看該作者
感謝分享,學(xué)習(xí)一下,非SPI方式。
回復(fù)

使用道具 舉報(bào)

13#
ID:235359 發(fā)表于 2020-12-28 16:40 | 只看該作者
正好在做這個(gè),學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

14#
ID:585349 發(fā)表于 2020-12-30 11:28 | 只看該作者
我有一條晚上2812燈帶是三根線的,引腳怎么去接
回復(fù)

使用道具 舉報(bào)

15#
ID:831068 發(fā)表于 2021-1-7 15:41 | 只看該作者
孫少 發(fā)表于 2020-12-30 11:28
我有一條晚上2812燈帶是三根線的,引腳怎么去接

vcc+gnd+signal=三根線
回復(fù)

使用道具 舉報(bào)

16#
ID:210471 發(fā)表于 2021-3-5 17:04 | 只看該作者
點(diǎn)亮了很好看
回復(fù)

使用道具 舉報(bào)

17#
ID:48216 發(fā)表于 2021-3-9 00:27 | 只看該作者

點(diǎn)亮了很好看
回復(fù)

使用道具 舉報(bào)

18#
ID:753687 發(fā)表于 2021-3-13 15:16 | 只看該作者
正好手里有一個(gè)ws2812,也想做這個(gè),學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

19#
ID:890487 發(fā)表于 2021-3-13 16:33 | 只看該作者
看看能不能移植到esp8266 謝謝樓主
回復(fù)

使用道具 舉報(bào)

20#
ID:202803 發(fā)表于 2021-5-20 14:01 | 只看該作者
請(qǐng)問使用PWM方式的話不使用DMA怎么實(shí)現(xiàn)呢,因?yàn)橛械膯纹瑱C(jī)是沒有DMA控制器的
回復(fù)

使用道具 舉報(bào)

21#
ID:423238 發(fā)表于 2021-5-21 15:46 來自觸屏版 | 只看該作者
有8266的驅(qū)動(dòng)程序沒
回復(fù)

使用道具 舉報(bào)

22#
ID:963713 發(fā)表于 2021-9-2 15:43 | 只看該作者
點(diǎn)亮了很好看
回復(fù)

使用道具 舉報(bào)

23#
ID:226821 發(fā)表于 2021-9-6 08:56 | 只看該作者
Thankjs share nice colorful effect
回復(fù)

使用道具 舉報(bào)

24#
ID:89237 發(fā)表于 2021-9-6 09:03 | 只看該作者

點(diǎn)亮了很好看
回復(fù)

使用道具 舉報(bào)

25#
ID:436885 發(fā)表于 2021-9-14 18:19 | 只看該作者
大家用的是什么型號(hào)芯片?
回復(fù)

使用道具 舉報(bào)

26#
ID:88256 發(fā)表于 2021-9-17 01:49 | 只看該作者
改到ESP-12F上試了一下,發(fā)現(xiàn)只有8個(gè)燈數(shù)量太少變化就不明顯,等有空改長(zhǎng)了試試
回復(fù)

使用道具 舉報(bào)

27#
ID:973189 發(fā)表于 2021-10-19 07:39 | 只看該作者
先記錄下來,后面學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

28#
ID:986209 發(fā)表于 2021-11-30 22:48 | 只看該作者
牛呀,才學(xué)習(xí)就可以寫出這樣的程序不簡(jiǎn)單
回復(fù)

使用道具 舉報(bào)

29#
ID:384489 發(fā)表于 2022-1-10 16:47 | 只看該作者
用過SPI,試試PWM的。
回復(fù)

使用道具 舉報(bào)

30#
ID:837560 發(fā)表于 2022-1-25 17:14 | 只看該作者
不錯(cuò),不錯(cuò)有空研究一下
回復(fù)

使用道具 舉報(bào)

31#
ID:947605 發(fā)表于 2022-4-25 19:30 | 只看該作者
很漂亮,帥啊
回復(fù)

使用道具 舉報(bào)

32#
ID:375944 發(fā)表于 2022-8-12 08:52 | 只看該作者
正用到這款做機(jī)器人控制燈用,學(xué)習(xí)下!
回復(fù)

使用道具 舉報(bào)

33#
ID:1040201 發(fā)表于 2022-9-2 16:29 | 只看該作者
感謝樓主,我去抽點(diǎn)時(shí)間學(xué)習(xí)一下 。
回復(fù)

使用道具 舉報(bào)

34#
ID:651494 發(fā)表于 2022-9-3 15:30 | 只看該作者
主循環(huán)用個(gè)大DELAY,還讓不讓其他進(jìn)程活?改天給你們寫個(gè),估計(jì)都還是大學(xué)生。
回復(fù)

使用道具 舉報(bào)

35#
ID:46750 發(fā)表于 2023-5-11 10:43 | 只看該作者
漂亮,這種算法有乘除法運(yùn)算,運(yùn)行效率和儲(chǔ)存空間都是挑戰(zhàn),對(duì)于成本較低的MCU,就...
有沒有高手來完善一下
回復(fù)

使用道具 舉報(bào)

36#
ID:339520 發(fā)表于 2023-6-19 13:46 | 只看該作者
1毛錢的應(yīng)廣單片機(jī)PMS150G驅(qū)動(dòng)WS2812代碼寫完了。
回復(fù)

使用道具 舉報(bào)

37#
ID:356964 發(fā)表于 2023-8-18 08:01 | 只看該作者
謝謝分享,學(xué)習(xí)一下!學(xué)習(xí)這個(gè)有癮!
回復(fù)

使用道具 舉報(bào)

38#
ID:356964 發(fā)表于 2023-8-18 08:02 | 只看該作者
樂此不疲!樂此不疲!感謝分享!
回復(fù)

使用道具 舉報(bào)

39#
ID:31514 發(fā)表于 2024-1-5 22:45 | 只看該作者
請(qǐng)教一下樓主,我移植到51單片機(jī)上,只有白色,不知道是什么原因
回復(fù)

使用道具 舉報(bào)

40#
ID:349598 發(fā)表于 2024-1-12 21:49 | 只看該作者
不錯(cuò)!謝謝樓主分享。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表