最近在学习STM32,到定时器与DMA章节,正巧手头有先前买的WS2812B模块,查找相关资料,于是成功点亮了,
又按照自己想法和思路写了一个渐变色算法,和渐变色流水功能,
可自定义灯的数量和渐变色的深度
渐变色原理:
假如红蓝两色变换
红(亮度):10 8 6 4 2 0
蓝(亮度):0 2 4 6 8 10
这样就形成两种颜色的过度
加大其分辨率,两颜色之间颜色差越小,生成颜色数据越多
在流水过程中,颜色取到数组末尾时,要将数组首端的数据加在末尾,使其颜色无差别连接
控制代码:
生成三种颜色数据数组
- /**
- * color_length 每种颜色渐变最大长度,总颜色长度:color_length * 3
- * return 颜色数据总长度
- */
- uint32_t set_Color_Loop(uint8_t color_length)
- {
- RGB = (uint32_t*)malloc(color_length*3*sizeof(uint32_t));//分配数组大小,(所有渐变色颜色长度)
- color_length -= 1;
- for(uint8_t i=0;i<=color_length;i++)
- {
- RGB[i] = (((0xff/color_length)*i)<<8)|((0xff/color_length)*(color_length-i)); //蓝到绿
- RGB[color_length + 1 +i] = (((0xff/color_length)*i)<<16)|((0xff/color_length)*(color_length-i))<<8; //绿到红
- RGB[((color_length+1)*2) +i] = ((0xff/color_length)*i)|((0xff/color_length)*(color_length-i))<<16; //红到蓝
- }
- return color_length*3;
- }
- 无间断位移获取8位颜色数据
- /**
- * colorwidth 每次获取多少位
- */
- void out_RGB(uint16_t data_Max_Length,uint16_t colorwidth)
- {
- static uint32_t rgb_position = 0;
- for(uint32_t i = 0;i < colorwidth; i++){
- uint16_t c = (rgb_position + i) % data_Max_Length;//末尾颜色数据结束时将首位的收据填充到数组
- send_Data(RGB[c]);
- }
- rgb_position ++;
- rgb_position %= data_Max_Length;
- }
复制代码
两种图片效果
手机拍不出灯光效果,实际肉眼看还是非常漂亮的
视频效果:
游客,本帖隐藏的内容需要积分高于 1 才可浏览,您当前积分为 0
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "WS2812B.h"
- #include <stdlib.h>
- uint32_t *RGB;//颜色列表数组
- void LED_Init(void);
- void PWM_Init(void);
- uint32_t set_Color_Loop(uint8_t color_length);
- void out_RGB(uint16_t data_Max_Length,uint16_t colorwidth);
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- uart_init(115200);
- delay_init();
- LED_Init();
-
- WS2812B_TIM_init();
- uint32_t len = set_Color_Loop(32);//每两种颜色之间的位数
-
- while(1)
- {
- out_RGB(len,8);//8个灯,每次输出8个数据
- delay_ms(80);
- }
- }
- /**
- * colorwidth 每次获取多少位
- */
- void out_RGB(uint16_t data_Max_Length,uint16_t colorwidth)
- {
- static uint32_t rgb_position = 0;
- for(uint32_t i = 0;i < colorwidth; i++){
- uint16_t c = (rgb_position + i) % data_Max_Length;//末尾颜色数据结束时将首位的收据填充到数组
- send_Data(RGB[c]);
- }
- rgb_position ++;
- rgb_position %= data_Max_Length;
- }
- /**
- * color_length 每种颜色渐变最大长度,总颜色长度:color_length * 3
- * return 颜色数据总长度
- */
- uint32_t set_Color_Loop(uint8_t color_length)
- {
- RGB = (uint32_t*)malloc(color_length*3*sizeof(uint32_t));//分配数组大小,(所有渐变色颜色长度)
- color_length -= 1;
- for(uint8_t i=0;i<=color_length;i++)
- {
- RGB[i] = (((0xff/color_length)*i)<<8)|((0xff/color_length)*(color_length-i)); //蓝到绿
- RGB[color_length + 1 +i] = (((0xff/color_length)*i)<<16)|((0xff/color_length)*(color_length-i))<<8; //绿到红
- RGB[((color_length+1)*2) +i] = ((0xff/color_length)*i)|((0xff/color_length)*(color_length-i))<<16; //红到蓝
- }
- return color_length*3;
- }
- void LED_Init()
- {
- GPIO_InitTypeDef GPIO_InitTypeStruct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOB,ENABLE);
- GPIO_InitTypeStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_5;
- GPIO_InitTypeStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOE,&GPIO_InitTypeStruct);
- GPIO_InitTypeStruct.GPIO_Pin = GPIO_Pin_5;
- GPIO_Init(GPIOB,&GPIO_InitTypeStruct);
- GPIOB->ODR ^= GPIO_Pin_5;
- GPIOE->ODR ^= GPIO_Pin_5;
- }
复制代码
PWM+DMA驱动WS2812的代码请查看附件
WS2812B.rar
(295.82 KB, 下载次数: 1132)
|