ws2812
需要开发的外设:ws2812
开发环境:MDK
开发板:stm32最小系统板
杜邦线无数
一、原理阐述
(一)这个ws2812有很多种,一个小灯、灯带,矩形像素屏等等。
这些都是DIN连接输入,DOUT连接输出,另外两个引脚是GNE和VCC
(二)数据传输原理:
第1个WS2812B灯珠接收到了第1个24bit的数据,做出响应(发光);
第N个WS2812B灯珠接收到了第N个24bit的数据,
再收到第二个24bit的数据后,直接转发给第二个WS2812B灯珠,由第二个WS2812B灯珠做出响应;依次类推。
每次给一个灯一个24位的数据,顺序是GRB-green red bule
(三)数据编码
编码时序是低高电平占位大小来区分的,分别是高比低比例位 1/2-->0 code
2/1-->1 code
(四)定时器配置
根据WS2812的最大传输速率800kbps,设置定时器不分频和计数周期为89+1,这样下来波形的频率为 72M /(89+1) = 800K ,并且一个波形的周期为 1 / 800 = 1.25us
我用的最大是72M,不同板子不同配置,自行选择。
二、代码展示
main.h
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "ws2812b.h"
uint8_t rgb0[][3] = {0,0,0};
uint8_t rgb1[212][3] = {{0,0,0},{10,0,0},{20,0,0},{30,0,0},{40,0,0},{50,0,0},{60,0,0},{70,0,0},{80,0,0},{90,0,0},
{100,0,0},{110,0,0},{120,0,0},{130,0,0},{140,0,0},{150,0,0},{160,0,0},{170,0,0},{180,0,0},{190,0,0},
{200,0,0},{210,0,0},{220,0,0},{230,0,0},{240,0,0},{250,0,0},{10,10,0},{20,20,0},{30,30,0},
{40,40,0},{50,50,0},{60,60,0},{70,70,0},{80,80,0},{90,90,0},{100,100,0},{110,110,0},{120,120,0},{130,130,0},{140,140,0},
{150,150,0},{160,160,0},{170,170,0},{180,180,0},{190,190,0},{200,200,0},{210,210,0},{220,220,0},{230,230,0},{240,240,0},
{250,250,0},{10,0,10},{20,0,20},{30,0,30},{40,0,40},{50,0,50},{60,0,60},{70,0,70},{80,0,80},{90,0,90},{100,0,100},{110,0,110},{120,0,120},
{130,0,130},{140,0,140},{150,0,150},{160,0,160},{170,0,170},{180,0,180},{190,0,190},{200,0,200},{210,0,210},{220,0,220},
{230,0,230},{240,0,240},{250,0,250},{0,10,10},{0,20,20},{0,30,30},{0,40,40},{0,50,50},{0,60,60},{0,70,70},
{0,80,80},{0,90,90},{0,100,100},{0,110,110},{0,120,120},{0,130,130},{0,140,140},{0,150,150},{0,160,160},{0,170,170},
{0,180,180},{0,190,190},{0,200,200},{0,210,210},{0,220,220},{0,230,230},{0,240,240},{0,250,250},{10,10,10},{30,30,30},
{90,90,90},{140,140,140},{200,200,200},{0,0,0},{10,0,0},{20,0,0},{30,0,0},{40,0,0},{50,0,0},{60,0,0},{70,0,0},{80,0,0},{90,0,0},
{100,0,0},{110,0,0},{120,0,0},{130,0,0},{140,0,0},{150,0,0},{160,0,0},{170,0,0},{180,0,0},{190,0,0},
{200,0,0},{210,0,0},{220,0,0},{230,0,0},{240,0,0},{250,0,0},{10,10,0},{20,20,0},{30,30,0},
{40,40,0},{50,50,0},{60,60,0},{70,70,0},{80,80,0},{90,90,0},{100,100,0},{110,110,0},{120,120,0},{130,130,0},{140,140,0},
{150,150,0},{160,160,0},{170,170,0},{180,180,0},{190,190,0},{200,200,0},{210,210,0},{220,220,0},{230,230,0},{240,240,0},
{250,250,0},{10,0,10},{20,0,20},{30,0,30},{40,0,40},{50,0,50},{60,0,60},{70,0,70},{80,0,80},{90,0,90},{100,0,100},{110,0,110},{120,0,120},
{130,0,130},{140,0,140},{150,0,150},{160,0,160},{170,0,170},{180,0,180},{190,0,190},{200,0,200},{210,0,210},{220,0,220},
{230,0,230},{240,0,240},{250,0,250},{0,10,10},{0,20,20},{0,30,30},{0,40,40},{0,50,50},{0,60,60},{0,70,70},
{0,80,80},{0,90,90},{0,100,100},{0,110,110},{0,120,120},{0,130,130},{0,140,140},{0,150,150},{0,160,160},{0,170,170},
{0,180,180},{0,190,190},{0,200,200},{0,210,210},{0,220,220},{0,230,230},{0,240,240},{0,250,250},{10,10,10},{30,30,30},
{90,90,90},{140,140,140},{200,200,200}};
uint8_t rgb2[109][3] = {{0,0,0},{0,5,0},{0,10,0},{0,15,0},{0,20,0},{0,25,0},{0,30,0},{0,35,0},{0,40,0},{0,45,0},{0,50,0},{0,55,0},{0,60,0},{0,65,0},{0,70,0},{0,75,0},{0,80,0},{0,85,0},{0,90,0},
{0,95,0},{0,100,0},{0,105,0},{0,110,0},{0,115,0},{0,120,0},{0,125,0},{0,130,0},{0,135,0},{0,140,0},{0,145,0},{0,150,0},{0,155,0},{0,160,0},{0,165,0},{0,170,0},{0,175,0},{0,180,0},{0,185,0},{0,190,0},
{0,195,0},{0,200,0},{0,205,0},{0,210,0},{0,215,0},{0,220,0},{0,225,0},{0,230,0},{0,235,0},{0,240,0},{0,245,0},{0,250,0},{0,255,0},{0,250,0},{0,245,0},{0,240,0},{0,235,0},{0,230,0},
{0,225,0},{0,220,0},{0,215,0},{0,210,0},{0,205,0},{0,200,0},{0,195,0},{0,190,0},{0,185,0},{0,180,0},{0,175,0},{0,170,0},{0,165,0},{0,160,0},{0,155,0},{0,150,0},{0,145,0},{0,140,0},{0,135,0},{0,130,0},
{0,125,0},{0,120,0},{0,115,0},{0,110,0},{0,105,0},{0,100,0},{0,95,0},{0,90,0},{0,85,0},{0,80,0},{0,75,0},{0,70,0},{0,65,0},{0,60,0},{0,55,0},{0,50,0},{0,45,0},{0,40,0},{0,35,0},{0,30,0},
{0,25,0},{0,20,0},{0,15,0},{0,10,0},{0,5,0},{0,0,0}};
uint8_t rgb3[53][3] = {{0,0,0},{0,0,10},{0,0,20},{0,0,30},{0,0,40},{0,0,50},{0,0,60},{0,0,70},{0,0,80},{0,0,90},
{0,0,100},{0,0,110},{0,0,120},{0,0,130},{0,0,140},{0,0,150},{0,0,160},{0,0,170},{0,0,180},{0,0,190},
{0,0,200},{0,0,210},{0,0,220},{0,0,230},{0,0,240},{0,0,250},{0,0,255},{0,0,250},{0,0,240},{0,0,230},
{0,0,220},{0,0,210},{0,0,200},{0,0,190},{0,0,180},{0,0,170},{0,0,160},{0,0,150},{0,0,140},{0,0,130},
{0,0,120},{0,0,110},{0,0,100},{0,0,90},{0,0,80},{0,0,70},{0,0,60},{0,0,50},{0,0,40},{0,0,30},
{0,0,20},{0,0,10},{0,0,0}};
uint8_t rgb5[200][3] = {{0,0,0},{100,0,0},
{100,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,100,0},{0,100,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,100},{0,0,100},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{10,100,0},{10,100,0},
{0,0,0},{0,0,0},{0,0,0},{0,0,0},{10,0,100},{10,0,100},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,10,100},{0,10,100},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{100,0,10},{100,0,10},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
{0,0,100},{0,0,100},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,100,0},{0,100,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{100,0,0},{100,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{100,0,100},{0,100,100},
{0,0,0},{0,0,0},{0,0,0},{0,0,0},{100,0,100},{100,100,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{90,0,100},{0,100,0}};
uint8_t rgb4[53][3] = {{0,0,0},{10,0,0},{20,0,0},{30,0,0},{40,0,0},{50,0,0},{60,0,0},{70,0,0},{80,0,0},{90,0,0},
{100,0,0},{110,0,0},{120,0,0},{130,0,0},{140,0,0},{150,0,0},{160,0,0},{170,0,0},{180,0,0},{190,0,0},
{200,0,0},{210,0,0},{220,0,0},{230,0,0},{240,0,0},{250,0,0},{255,0,0},{250,0,0},{240,0,0},{230,0,0},
{220,0,0},{210,0,0},{200,0,0},{190,0,0},{180,0,0},{170,0,0},{160,0,0},{150,0,0},{140,0,0},{130,0,0},
{120,0,0},{110,0,0},{100,0,0},{90,0,0},{80,0,0},{70,0,0},{60,0,0},{50,0,0},{40,0,0},{30,0,0},{20,0,0},{10,0,0},{0,0,0}};
//每个三位数,每一个位位一个不同颜色,数字大小表明亮度大小,最大256,通过将这个三个灯亮不同种类、大小程度进行调制不同量的颜色
u16 i,j;
int main(void)
{
delay_init();
LED_Init();
Timer2_init();
while(1)
{
for(i=0;i<110;i++)
{
WS2812_send(&rgb2[i],50,0);
delay_ms(50);
}
for(i=0;i<77;i++)
{
WS2812_send(&rgb5[i],50,0);
delay_ms(50);
}
for(i=0;i<212;i++)
{
WS2812_send(&rgb1[i],50,1);
delay_ms(50);
}
}
}
每个三位数,每一个位位一个不同颜色,数字大小表明亮度大小,最大256,通过将这个三个灯亮不同种类、大小程度进行调制不同量的颜色
ws2812.c
#include "WS2812B.h"
#define TIM2_CCR1_Address 0x40000034
#define TIMING_ONE 50
#define TIMING_ZERO 25
uint16_t LED_BYTE_Buffer[300];
void WS2812_send(uint8_t (*color)[3], uint16_t len,u8 flag)
{
static unsigned char iie = 0;
uint8_t i;
uint16_t memaddr;
uint16_t buffersize;
buffersize = (len*24);
memaddr = 0;
while (len)
{
for(i=0; i<8; i++) // G
{
LED_BYTE_Buffer[memaddr] = ((color[iie][1]<<i) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
memaddr++;
}
for(i=0; i<8; i++) // r
{
LED_BYTE_Buffer[memaddr] = ((color[iie][0]<<i) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
memaddr++;
}
for(i=0; i<8; i++) // B
{
LED_BYTE_Buffer[memaddr] = ((color[iie][2]<<i) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
memaddr++;
}
if(flag==1)
{
if(iie>=12)
iie = 0;
iie++;
}
len--;
}
ws2812.h
#ifndef __WS2812B_H
#define __WS2812B_H
#include "stm32f10x.h"
#include "delay.h"
void Timer2_init(void);
void WS2812_send(uint8_t (*color)[3], uint16_t len,u8 flag);
#endif
BUG:
如果使用象素萍的话我们只能成功使用50个,然后使用单一装置和小灯带可以成功,这个目前没有解决。文章来源:https://www.toymoban.com/news/detail-805621.html
完整工程代码:
(35条消息) 外设ws2812使用32驱动-单片机文档类资源-CSDN文库https://download.csdn.net/download/weixin_52451652/86399407?spm=1001.2014.3001.5503文章来源地址https://www.toymoban.com/news/detail-805621.html
到了这里,关于(STM32笔记5)ws2812驱动开发的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!