標題: WS2812B漸變色流水算法+PWM DMA驅(qū)動STM32源程序 [打印本頁]

作者: kaixuan520    時間: 2019-7-18 11:55
標題: WS2812B漸變色流水算法+PWM DMA驅(qū)動STM32源程序
最近在學(xué)習(xí)STM32,到定時器與DMA章節(jié),正巧手頭有先前買的WS2812B模塊,查找相關(guān)資料,于是成功點亮了,
又按照自己想法和思路寫了一個漸變色算法,和漸變色流水功能,

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

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


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


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

控制代碼:

生成三種顏色數(shù)據(jù)數(shù)組
  1. /**
  2. * color_length 每種顏色漸變最大長度,總顏色長度:color_length * 3
  3. * return 顏色數(shù)據(jù)總長度
  4. */
  5. uint32_t set_Color_Loop(uint8_t color_length)
  6. {
  7.     RGB = (uint32_t*)malloc(color_length*3*sizeof(uint32_t));//分配數(shù)組大小,(所有漸變色顏色長度)
  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));                                       //藍到綠
  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;   //紅到藍
  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é)束時將首位的收據(jù)填充到數(shù)組
  26.         send_Data(RGB[c]);
  27.     }
  28.     rgb_position ++;
  29.     rgb_position %= data_Max_Length;
  30. }
復(fù)制代碼


兩種圖片效果



手機拍不出燈光效果,實際肉眼看還是非常漂亮的
視頻效果:


  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);//每兩種顏色之間的位數(shù)
  20.        
  21.     while(1)
  22.     {
  23.                 out_RGB(len,8);//8個燈,每次輸出8個數(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é)束時將首位的收據(jù)填充到數(shù)組
  35.                 send_Data(RGB[c]);
  36.         }
  37.         rgb_position ++;
  38.         rgb_position %= data_Max_Length;
  39. }

  40. /**
  41. * color_length 每種顏色漸變最大長度,總顏色長度:color_length * 3
  42. * return 顏色數(shù)據(jù)總長度
  43. */
  44. uint32_t set_Color_Loop(uint8_t color_length)
  45. {
  46.         RGB = (uint32_t*)malloc(color_length*3*sizeof(uint32_t));//分配數(shù)組大小,(所有漸變色顏色長度)
  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));                                                //藍到綠
  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;  //紅到藍
  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ū)動WS2812的代碼請查看附件
WS2812B.rar (295.82 KB, 下載次數(shù): 1132)


作者: zhang1994    時間: 2019-10-16 08:40
正好在做這個,學(xué)習(xí)一下
作者: lxjmcuahz    時間: 2020-4-10 15:47
謝謝分享,學(xué)習(xí)一下
作者: 無窮小量    時間: 2020-4-14 20:39
不閃動呀
作者: ttkaixin    時間: 2020-5-29 19:24
不錯 學(xué)習(xí)一下
作者: hei51com    時間: 2020-5-30 13:17
非常不錯,感謝分享。。!
作者: aijinquan89    時間: 2020-6-9 20:00

謝謝分享,學(xué)習(xí)一下
作者: jach09    時間: 2020-6-12 15:04
實際燒到單片機里運行,燈光效果很漂亮。
作者: lxjmcuahz    時間: 2020-7-1 22:26
謝謝,有空試一下。
作者: daisyduxy    時間: 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);
}
作者: King23    時間: 2020-11-10 16:39
非常感謝,值得學(xué)習(xí)一下,非常好
作者: onono    時間: 2020-11-20 15:06
感謝分享,學(xué)習(xí)一下,非SPI方式。
作者: 我比黑點子更黑    時間: 2020-12-28 16:40
正好在做這個,學(xué)習(xí)一下
作者: 孫少    時間: 2020-12-30 11:28
我有一條晚上2812燈帶是三根線的,引腳怎么去接
作者: chaoge131    時間: 2021-1-7 15:41
孫少 發(fā)表于 2020-12-30 11:28
我有一條晚上2812燈帶是三根線的,引腳怎么去接

vcc+gnd+signal=三根線
作者: q6563008    時間: 2021-3-5 17:04
點亮了很好看
作者: Desc    時間: 2021-3-9 00:27

點亮了很好看
作者: 1956810010    時間: 2021-3-13 15:16
正好手里有一個ws2812,也想做這個,學(xué)習(xí)一下
作者: kidxpp    時間: 2021-3-13 16:33
看看能不能移植到esp8266 謝謝樓主
作者: 陳仕祥    時間: 2021-5-20 14:01
請問使用PWM方式的話不使用DMA怎么實現(xiàn)呢,因為有的單片機是沒有DMA控制器的
作者: 快樂a123    時間: 2021-5-21 15:46
有8266的驅(qū)動程序沒
作者: jouson    時間: 2021-9-2 15:43
點亮了很好看
作者: nkchen    時間: 2021-9-6 08:56
Thankjs share nice colorful effect
作者: myn0921    時間: 2021-9-6 09:03

點亮了很好看
作者: jsbhhqy    時間: 2021-9-14 18:19
大家用的是什么型號芯片?

作者: hhdsdy    時間: 2021-9-17 01:49
改到ESP-12F上試了一下,發(fā)現(xiàn)只有8個燈數(shù)量太少變化就不明顯,等有空改長了試試
作者: lff1802    時間: 2021-10-19 07:39
先記錄下來,后面學(xué)習(xí)一下
作者: buzhizml    時間: 2021-11-30 22:48
牛呀,才學(xué)習(xí)就可以寫出這樣的程序不簡單
作者: onono    時間: 2022-1-10 16:47
用過SPI,試試PWM的。
作者: 2823589028    時間: 2022-1-25 17:14
不錯,不錯有空研究一下
作者: 乾坤.    時間: 2022-4-25 19:30
很漂亮,帥啊
作者: qinglucky    時間: 2022-8-12 08:52
正用到這款做機器人控制燈用,學(xué)習(xí)下!
作者: liucan2020    時間: 2022-9-2 16:29
感謝樓主,我去抽點時間學(xué)習(xí)一下 。
作者: feeling1791    時間: 2022-9-3 15:30
主循環(huán)用個大DELAY,還讓不讓其他進程活?改天給你們寫個,估計都還是大學(xué)生。
作者: zenghl    時間: 2023-5-11 10:43
漂亮,這種算法有乘除法運算,運行效率和儲存空間都是挑戰(zhàn),對于成本較低的MCU,就...
有沒有高手來完善一下
作者: wuyb0512    時間: 2023-6-19 13:46
1毛錢的應(yīng)廣單片機PMS150G驅(qū)動WS2812代碼寫完了。
作者: bk6716    時間: 2023-8-18 08:01
謝謝分享,學(xué)習(xí)一下!學(xué)習(xí)這個有癮!
作者: bk6716    時間: 2023-8-18 08:02
樂此不疲!樂此不疲!感謝分享!
作者: tamadeji303    時間: 2024-1-5 22:45
請教一下樓主,我移植到51單片機上,只有白色,不知道是什么原因
作者: sjh7366    時間: 2024-1-12 21:49
不錯!謝謝樓主分享。




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1