采用STC15W204S最小系统板,采用P1.0口做输出接灯板DIN,编译时需要加入stc15.h头文件
制作出来的实物图如下:
视频演示:
单片机源码:
- #include<stc15.h>
- #include"intrins.h"
- sbit WS2812 = P1^0;
- #define numLEDs 22 //灯的个数
- unsigned char buf_R[numLEDs] = {0};//颜色缓存
- unsigned char buf_G[numLEDs] = {0};
- unsigned char buf_B[numLEDs] = {0};
- void RGB_Set_Up(); //送0码
- void RGB_Set_Down(); //送1码
- void HAL_Delay(unsigned int t)
- {
- unsigned int x,y;
- for(x=114;x>0;x--)
- for(y=t;y>0;y--);
- }
- //复位延时
- void Delay50us() //@22.1184MHz
- {
- unsigned char i, j;
- _nop_();
- _nop_();
- i = 2;
- j = 15;
- do
- {
- while (--j);
- } while (--i);
- }
- //1码,高电平850ns 低电平400ns 误差正负150ns
- void RGB_Set_Up()
- {
- WS2812 = 1;
- //经过逻辑分析仪调试的的延时
- _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
- _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
- WS2812 = 0;
- }
- //1码,高电平400ns 低电平850ns 误差正负150ns
- void RGB_Set_Down()
- {
- WS2812 = 1;
- //经过逻辑分析仪调试的的延时
- _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
- WS2812 = 0;
- }
- //发送24位数据
- void Send_2812_24bits(unsigned char G8,unsigned char R8,unsigned char B8)
- {
- unsigned int n = 0;
- //发送G8位
- for(n=0;n<8;n++)
- {
- G8<<=n;
- if(G8&0x80 == 0x80)
- {
- RGB_Set_Up();
- }
- else
- {
- RGB_Set_Down();
- }
- }
- //发送R8位
- for(n=0;n<8;n++)
- {
- R8<<=n;
- if(R8&0x80 == 0x80)
- {
- RGB_Set_Up();
- }
- else
- {
- RGB_Set_Down();
- }
- }
- //发送B8位
- for(n=0;n<8;n++)
- {
- B8<<=n;
- if(B8&0x80 == 0x80)
- {
- RGB_Set_Up();
- }
- else
- {
- RGB_Set_Down();
- }
- }
- }
- //复位码
- void RGB_Rst()
- {
- WS2812 = 0;
- Delay50us();
- }
- //把24位数据GRB码转RGB
- void Set_Colour(unsigned char r,unsigned char g,unsigned char b)
- {
- unsigned char i;
- for(i=0;i<numLEDs;i++)
- {
- buf_R[i] = r; //缓冲
- buf_G[i] = g;
- buf_B[i] = b;
- }
- for(i=0;i<numLEDs;i++)
- {
- Send_2812_24bits(buf_G[i],buf_R[i],buf_B[i]);//发送显示
- }
- }
- //某一个点显示的颜色
- void SetPointColour(unsigned int num,unsigned char r,unsigned char g,unsigned char b)
- {
- unsigned char i;
- for(i=0;i<numLEDs;i++)
- {
- buf_R[num] = r;//缓冲
- buf_G[num] = g;
- buf_B[num] = b;
- }
- for(i=0;i<numLEDs;i++)
- {
- Send_2812_24bits(buf_G[i],buf_R[i],buf_B[i]);//发送显示
- }
- }
- //颜色交换24位不拆分发
- void SetPixelColor(unsigned char num,unsigned long c)
- {
- unsigned char i;
- for(i=0;i<numLEDs;i++)
- {
- buf_R[num] = (unsigned char)(c>>16);
- buf_G[num] = (unsigned char)(c>>8);
- buf_B[num] = (unsigned char)(c);
- }
- for(i=0;i<numLEDs;i++)
- {
- Send_2812_24bits(buf_G[i],buf_R[i],buf_B[i]);
- }
- }
- //复位
- void PixelUpdate()
- {
- RGB_Rst();
- }
- // Fill the dots one after the other with a color
- //用一种颜色填充这些圆点
- void colorWipe(unsigned long c, unsigned int wait)
- {
- unsigned int i=0;
- for( i=0; i<numLEDs; i++)
- {
- SetPixelColor(i, c);
- PixelUpdate();
- HAL_Delay(wait);
- }
- }
- void main()
- {
- while(1)
- {
-
- colorWipe(255*32*1024,1000);//红色
- colorWipe(0,1000);
- colorWipe(255*16,1000); //绿色
- colorWipe(0,1000);
- colorWipe(255,1000);//蓝色
- colorWipe(0,1000);
- colorWipe(255*32*1024+255*16,1000);//红色+绿色
- colorWipe(0,1000);
- colorWipe(255*32*1024+255,1000);//红色+蓝色
- colorWipe(0,1000);
- colorWipe(255*16+255,1000); //绿色+蓝色
- colorWipe(0,1000);
- colorWipe(255*32*1024+255*16+255,1000); //红色+绿色+蓝色
- colorWipe(0,1000);
- colorWipe(255*32*1024,100); //红色
- HAL_Delay(10000);
- colorWipe(255*16,100); //绿色
- HAL_Delay(10000);
- colorWipe(255,100);//蓝色
- HAL_Delay(10000);
- colorWipe(255*32*1024+255*16,100);//红色+绿色
- HAL_Delay(10000);
- …………
- …………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
全部资料51hei下载地址:
ws2812三色切换.rar
(18.4 MB, 下载次数: 691)
|