LED使用的5050分装自带WS2811.只需要一个IO口控制,可以显示16777215 种颜色,附源代码。
单片机源程序如下:
- #include "main.h"
- #include
- //#define MAIN_Fosc 24000000L //定义主时钟
- #define WHITE 0xFFFFFF // 白色
- #define BLACK 0x000000 // 黑色
- #define RED 0x00ff00 // 红色
- #define GREEN 0xff0000 // 绿色
- #define BLUE 0x0000ff // 蓝色
- #define CHENG 0xff7f00 // 橙色
- #define DIAN 0xff00ff //
- #define ZI 0x00ffff
- #define LED_N 16 //LED的数量
- //extern unsigned long WsDat[];
- unsigned long WsDat[];
- void main ()//主函数
- {
- reset();
- //DisP_R_G_B(20);
- ColorToColor(0X000000,0XFF0000);
- ColorToColor(0XFF0000,0xFFFF00);
- ColorToColor(0xFFFF00,0Xffffff);
- ColorToColor(0Xffffff,0X00ffff);
- ColorToColor(0X00ffff,0X0000ff);
- ColorToColor(0X0000ff,0X000000);
- ColorToColor(0X000000,0X00FF00);
- ColorToColor(0X00FF00,0x0FFFF0);
- ColorToColor(0x0FFFF0,0Xffffff);
- ColorToColor(0Xffffff,0XFF00ff);
- ColorToColor(0XFF00ff,0XF0000f);
- ColorToColor(0XF0000f,0X000000);
- }
- void delay(unsigned int time) //延时
- { unsigned int i,j,k;
- for(j=0;j
- for(i=0;i
- for(k=0;k
- Delay();
-
- }
- void Delay() //@24.000MHz
- {
- _nop_();
- }
- void reset(void)
- {
- int i;
- LED=0;
- for(i=0;i<50;i++)
- {Delay();
- }
- }
- void T0(void)
- {
- LED=1;
- LED=0;
- Delay();
-
- }
- void T1(void)
- {
- LED=1;
- Delay();
- LED=0;
- }
- void DisP_Color(unsigned long dat) //控制一个灯
- { unsigned int i;
- for(i=0;i<24;i++)
- { if(0x800000 == (dat & 0x800000)) T1();
- else T0();
- dat<<=1; //左移一位
- }
- }
- void DisP_ALLColor(unsigned long dat) //控制所有灯
- {int j;
- for(j=0;j
- reset();
- }
- void DisP_R_G_B(unsigned int j) //RGB渐变
- {
- unsigned long i=0;
- while(i<0xffffff)
- {
- if(i<0xff) DisP_ALLColor(i++);
- else if(i<0xffff) { i=i+0x100;DisP_ALLColor(i); }
- else if(i<0xffffff) {i=i+0x10000;DisP_ALLColor(i); }
- delay(j);
- }
- while(i>0)
- {
- if(i>0xffff00) DisP_ALLColor(i--);
- else if(i>0xff0000) {i=i-0x100; DisP_ALLColor(i);}
- else if(i>0) {i=i-0x10000;DisP_ALLColor(i);}
- delay(j);
- }
- reset();
- }
- unsigned char abs0(int num)
- {
- if(num>0) return num;
-
- num = -num;
- return (unsigned char) num;
- }
-
- unsigned long ColorToColor(unsigned long color0, unsigned long color1)
- {
- unsigned char Red0, Green0, Blue0; // 起始三原色
- unsigned char Red1, Green1, Blue1; // 结果三原色
- int RedMinus, GreenMinus, BlueMinus; // 颜色差(color1 - color0)
- unsigned char NStep; // 需要几步
- float RedStep, GreenStep, BlueStep; // 各色步进值
- unsigned long color; // 结果色
- unsigned char i;
-
- // 绿 红 蓝 三原色分解
- Red0 = color0>>8;
- Green0 = color0>>16;
- Blue0 = color0;
-
- Red1 = color1>>8;
- Green1 = color1>>16;
- Blue1 = color1;
-
- // 计算需要多少步(取差值的最大值)
- RedMinus = Red1 - Red0;
- GreenMinus = Green1 - Green0;
- BlueMinus = Blue1 - Blue0;
-
- NStep = ( abs0(RedMinus) > abs0(GreenMinus) ) ? abs0(RedMinus):abs0(GreenMinus);
- NStep = ( NStep > abs0(BlueMinus) ) ? NStep:abs0(BlueMinus);
-
- // 计算出各色步进值
- RedStep = (float)RedMinus / NStep;
- GreenStep = (float)GreenMinus / NStep;
- ……………………
LED渐变.rar (30.65 KB, 下载次数: 293)
LED渐变
下载积分: 黑币 -5