LED驱动WS2801
WS2801单片机程序:
- #include<stc12c56.h>
- #include"WS2801.H"
- #define u8 unsigned char
- #define u16 unsigned int
- #define u32 unsigned long
- u8 num=0,MODE=0,GAME_NUM=0,R_PWM=0,G_PWM=0,B_PWM=0,WATEN_NUM=0;
- u8 bdata set=0;
- sbit F_CLOCK=set^0;
- sbit F_INC=set^1;
- sbit F_WATER=set^2;
- u8 status_mode=0;
- u16 mode_count=0;
- bit flag=0,StackerGame_flag=0;
- void T0_Init(void) //PWM初始化
- {
- TMOD=0x01;
- TH0=170;
- TL0=200;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void exter0() interrupt 1
- {
- TH0=200;
- TL0=200;
- mode_count++;
- if(mode_count>=3000)
- {
- mode_count=0;
- status_mode++;
- if(status_mode>38)
- {
- status_mode=0;
- }
- }
- WS2801_Write_data();
- CLK=0; //MT9221时钟信号
- DATA=0; //MT9221数据信号
- }
- void RGB_7_SE_MAN() //7单色慢闪
- {
- static u32 count=60000;
- u8 i;
- switch (count)
- {
- case 60000:
- {
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=255;
- RGB_DATA[i+1]=0;
- RGB_DATA[i+2]=0;
- i++;i++;
- }
- }break;
- case 120000:
- {
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=0;
- RGB_DATA[i+1]=255;
- RGB_DATA[i+2]=0;
- i++;i++;
- }
- }break;
- case 180000:
- {
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=0;
- RGB_DATA[i+1]=0;
- RGB_DATA[i+2]=255;
- i++;i++;
- }
- }break;
- case 240000:
- {
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=255;
- RGB_DATA[i+1]=255;
- RGB_DATA[i+2]=0;
- i++;i++;
- }
- }break;
- case 300000:
- {
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=0;
- RGB_DATA[i+1]=255;
- RGB_DATA[i+2]=255;
- i++;i++;
- }
- }break;
- case 360000:
- {
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=255;
- RGB_DATA[i+1]=0;
- RGB_DATA[i+2]=255;
- i++;i++;
- }
- }break;
- case 420000:
- {
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=255;
- RGB_DATA[i+1]=255;
- RGB_DATA[i+2]=255;
- i++;i++;
- }
- }
- count=0;
- break;
- }
- count++;
- }
- void RGB_7_SE_BIAN(u16 num) //7单色渐变
- {
- static u8 count=0;
- static u16 count_k=0;
- u8 i,k;
- switch(count)
- {
- case 0:
- {
- if(count_k>=num)
- {
- count_k=0;
- k++;
- if(k>=255)
- {
- count=1;
- }
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=k;
- RGB_DATA[i+1]=0;
- RGB_DATA[i+2]=0;
- i++;i++;
- }
- }
- }
- count_k++;
- break;
- case 1:
- {
- if(count_k>=num)
- {
- count_k=0;
- k++;
- if(k>=255)
- {
- count=2;
- }
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=255-k;
- RGB_DATA[i+1]=0;
- RGB_DATA[i+2]=0;
- i++;i++;
- }
- }
- }
- count_k++;
- break;
- case 2:
- {
- if(count_k>=num)
- {
- count_k=0;
- k++;
- if(k>=255)
- {
- count=3;
- }
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=0;
- RGB_DATA[i+1]=k;
- RGB_DATA[i+2]=0;
- i++;i++;
- }
- }
- }
- count_k++;
- break;
- case 3:
- {
- if(count_k>=num)
- {
- count_k=0;
- k++;
- if(k>=255)
- {
- count=4;
- }
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=0;
- RGB_DATA[i+1]=255-k;
- RGB_DATA[i+2]=0;
- i++;i++;
- }
- }
- }
- count_k++;
- break;
- case 4:
- {
- if(count_k>=num)
- {
- count_k=0;
- k++;
- if(k>=255)
- {
- count=5;
- }
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=0;
- RGB_DATA[i+1]=0;
- RGB_DATA[i+2]=k;
- i++;i++;
- }
- }
- }
- count_k++;
- break;
- case 5:
- {
- if(count_k>=num)
- {
- count_k=0;
- k++;
- if(k>=255)
- {
- count=6;
- }
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=0;
- RGB_DATA[i+1]=0;
- RGB_DATA[i+2]=255-k;
- i++;i++;
- }
- }
- }
- count_k++;
- break;
- case 6:
- {
- if(count_k>=num)
- {
- count_k=0;
- k++;
- if(k>=255)
- {
- count=7;
- }
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=k;
- RGB_DATA[i+1]=k;
- RGB_DATA[i+2]=0;
- i++;i++;
- }
- }
- }
- count_k++;
- break;
- case 7:
- {
- if(count_k>=num)
- {
- count_k=0;
- k++;
- if(k>=255)
- {
- count=8;
- }
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=255-k;
- RGB_DATA[i+1]=255-k;
- RGB_DATA[i+2]=0;
- i++;i++;
- }
- }
- }
- count_k++;
- break;
- case 8:
- {
- if(count_k>=num)
- {
- count_k=0;
- k++;
- if(k>=255)
- {
- count=9;
- }
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=0;
- RGB_DATA[i+1]=k;
- RGB_DATA[i+2]=k;
- i++;i++;
- }
- }
- }
- count_k++;
- break;
- case 9:
- {
- if(count_k>=num)
- {
- count_k=0;
- k++;
- if(k>=255)
- {
- count=10;
- }
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=0;
- RGB_DATA[i+1]=255-k;
- RGB_DATA[i+2]=255-k;
- i++;i++;
- }
- }
- }
- count_k++;
- break;
- case 10:
- {
- if(count_k>=num)
- {
- count_k=0;
- k++;
- if(k>=255)
- {
- count=11;
- }
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=k;
- RGB_DATA[i+1]=0;
- RGB_DATA[i+2]=k;
- i++;i++;
- }
- }
- }
- count_k++;
- break;
- case 11:
- {
- if(count_k>=num)
- {
- count_k=0;
- k++;
- if(k>=255)
- {
- count=0;
- }
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=255-k;
- RGB_DATA[i+1]=0;
- RGB_DATA[i+2]=255-k;
- i++;i++;
- }
- }
- }
- count_k++;
- break;
- }
- }
- void RGBZUO_YOU(u16 num) //7彩向左覆盖
- {
- static u8 count=0;
- static u16 count_k=0;
- static u8 i,k,beijing=0;
- if(count==0)
- {
- if(count_k>=num)
- {
- count_k=0;
-
- for(i=0;i<150;i++)
- {
- if(i<k)
- {
- RGB_DATA[i]=255;
- }
- else
- {
- RGB_DATA[i]=0;
- }
- RGB_DATA[i+1]=0;
- RGB_DATA[i+2]=beijing;
- i++;i++;
- }
- k++;k++;k++;
- if(k>=150)
- {
- count=1;
- k=0;i=0;count_k=0;
- beijing=255;
- }
- }
- count_k++;
- }
- if(count==1)
- {
- if(count_k>=num)
- {
- count_k=0;
-
- for(i=0;i<150;i++)
- {
- if(i<k)
- {
- RGB_DATA[i+2]=0;
- }
- else
- {
- RGB_DATA[i+2]=255;
- }
- RGB_DATA[i]=255;
- RGB_DATA[i+1]=0;
- i++;i++;
- }
- k++;k++;k++;
- if(k>=150)
- {
- count=2;
- k=0;i=0;count_k=0;
- beijing=255;
- }
- }
- count_k++;
- }
- if(count==2)
- {
- if(count_k>=num)
- {
- count_k=0;
-
- for(i=0;i<150;i++)
- {
- if(i<k)
- {
- RGB_DATA[i+1]=255;
- }
- else
- {
- RGB_DATA[i+1]=0;
- }
- RGB_DATA[i]=beijing;
- RGB_DATA[i+2]=0;
- i++;i++;
- }
- k++;k++;k++;
- if(k>=150)
- {
- count=3;
- k=0;i=0;count_k=0;
- beijing=255;
- }
- }
- count_k++;
- }
- if(count==3)
- {
- if(count_k>=num)
- {
- count_k=0;
-
- for(i=0;i<150;i++)
- {
- if(i<k)
- {
- RGB_DATA[i]=0;
- }
- else
- {
- RGB_DATA[i]=255;
- }
- RGB_DATA[i+1]=255;
- RGB_DATA[i+2]=0;
- i++;i++;
- }
- k++;k++;k++;
- if(k>=150)
- {
- count=4;
- k=0;i=0;count_k=0;
- beijing=255;
- }
- }
- count_k++;
- }
- if(count==4)
- {
- if(count_k>=num)
- {
- count_k=0;
-
- for(i=0;i<150;i++)
- {
- if(i<k)
- {
- RGB_DATA[i+2]=255;
- }
- else
- {
- RGB_DATA[i+2]=0;
- }
- RGB_DATA[i]=0;
- RGB_DATA[i+1]=beijing;
- i++;i++;
- }
- k++;k++;k++;
- if(k>=150)
- {
- count=5;
- k=0;i=0;count_k=0;
- beijing=255;
- }
- }
- count_k++;
- }
- if(count==5)
- {
- if(count_k>=num)
- {
- count_k=0;
-
- for(i=0;i<150;i++)
- {
- if(i<k)
- {
- RGB_DATA[i+1]=0;
- }
- else
- {
- RGB_DATA[i+1]=255;
- }
- RGB_DATA[i]=0;
- RGB_DATA[i+2]=beijing;
- i++;i++;
- }
- k++;k++;k++;
- if(k>=150)
- {
- count=0;
- k=0;i=0;count_k=0;
- beijing=255;
- }
- }
- count_k++;
- }
-
- }
- void CLOCK_SELECT(u8 byte)
- {
- switch(byte)
- {
- case 0: RGB_DATA[0]=0;RGB_DATA[1]=0;RGB_DATA[2]=0xff;break; //r
- case 1: RGB_DATA[0]=0;RGB_DATA[1]=0xff;RGB_DATA[2]=0;break; //r
- case 2: RGB_DATA[0]=0xff;RGB_DATA[1]=0;RGB_DATA[2]=0;break; //r
- case 3: RGB_DATA[0]=0;RGB_DATA[1]=0xff;RGB_DATA[2]=0xff;break; //r
- case 4: RGB_DATA[0]=0xff;RGB_DATA[1]=0xff;RGB_DATA[2]=0;break; //r
- case 5: RGB_DATA[0]=0xff;RGB_DATA[1]=0;RGB_DATA[2]=0xff;break; //r
- case 6: RGB_DATA[0]=0xff;RGB_DATA[1]=0xff;RGB_DATA[2]=0xff;break; //r
- }
- }
- void SERVER_CLOCK(u32 delay)
- {
- static u32 count=0;
- u8 i,j=147;
- if(count>=delay)
- {
- count=0;
- for(i=0;i<50;i++)
- {
- RGB_DATA[j+3]=RGB_DATA[j]; //B 23-24 22-23
- j-=1;
- RGB_DATA[j+3]=RGB_DATA[j]; //G
- j-=1;
- RGB_DATA[j+3]=RGB_DATA[j]; //R
- j-=1;
- }
- MODE+=1;
- if(MODE>=7)
- {
- MODE=0;
- }
- CLOCK_SELECT(MODE); //七彩颜色选择
- }
- count++;
- }
- void SERVER_3SE(u32 delay)
- {
- static u32 count=0;
- u8 i,j=120,k; //72颗灯 9=3*3 其中3代表要移的灯数 加1是指向他的前面一个数
- if(count>=delay)
- {
- count=0;
- for(k=0;k<9;k++) //8*9=72颗
- {
- for(i=0;i<15;i++)
- {
- RGB_DATA[i+j+15]=RGB_DATA[j+i]; //B 23-24 22-23
- }
- j-=15; //指向上一个数组的头
- }
- MODE+=1;
- if(MODE>=7)
- MODE=0;
- CLOCK_SELECT(MODE); //七彩颜色选择
- for(i=1;i<5;i++)
- {
- RGB_DATA[3*i+0]=RGB_DATA[0];
- RGB_DATA[3*i+1]=RGB_DATA[1];
- RGB_DATA[3*i+2]=RGB_DATA[2];
- }
- }
- count++;
- }
- void StackerGame(u32 delay)
- {
- u8 i;
- static u32 count=0;
- if(count>=delay)
- {
- count=0;
- for(i=0;i<3;i++)
- {
- if(num>=(150-GAME_NUM))
- {
- if(F_CLOCK==1)
- MODE+=1; //这部分是出七彩的效果
-
- if(MODE>=7) //七到了之后就要出七彩
- {
- MODE=0;
- F_CLOCK=1;
- }
- CLOCK_SELECT(MODE);
- num=0;
- GAME_NUM+=3; //执行这里说明转换一次完成 已经到尾
- if(GAME_NUM==150)
- {
- MODE+=1;
- if(F_CLOCK==1)
- {
- F_CLOCK=0;
- MODE=0;
- }
- GAME_NUM=0;
- for(i=0;i<150;i++)
- {
- RGB_DATA[i]=0; //清数据
- }
- break;
- }
- }
- RGB_DATA[num+3]=RGB_DATA[num];
- RGB_DATA[num]=0;
- num++; //交换数据
- }
- }count++;
- }
- void main()
- {
- status_mode=0;
- T0_Init();
- while(1)
- {
- switch (status_mode)
- {
- case 0:
- case 1:
- case 2:
- {
- RGB_7_SE_MAN();
- }
- break;
- case 3:
- case 4:
- case 5:
- case 6:
- {
- RGB_7_SE_BIAN(2000);//2000);
- }
- break;
- case 7:
- case 8:
- case 9:
- case 10:
- {
- SERVER_CLOCK(100000);
- }
- break;
- case 11:
- case 12:
- case 13:
- case 14:
- {
- RGBZUO_YOU(10000);
- }
- break;
- case 15:
- case 16:
- case 17:
- case 18:
- {
- SERVER_CLOCK(100000);
- }
- break;
- case 19:
- case 20:
- case 21:
- case 22:
- {
- SERVER_3SE(300000);
- }break;
- case 23:
- case 24:
- case 25:
- case 26:
- case 27:
- case 28:
- case 29:
- case 30:
- case 31:
- case 32:
- case 33:
- case 34:
- case 35:
- case 36:
- case 37:
- case 38:
- {
- StackerGame(4470);
- }
- break;
- }
- }
- }
复制代码
LED驱动WS2801.rar
(809.38 KB, 下载次数: 59)
|