如何我要從中間往兩邊跑,以及多種效果,請教下算法思路,現在就三種流法,也可以說是兩種
單片機源程序如下:
- #include<STC8H1K28.H>
- #include"intrins.h"
- sbit WS2812 = P1^0;
- sbit on = P2^4;
- #define numLEDs 36 //燈的個數
- 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 long 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;
- for(n=0;n<8;n++) //發送G8位
- {
- G8<<=n;
- if(G8&0x80 == 0x80)
- {
- RGB_Set_Up();
- }
- else
- {
- RGB_Set_Down();
- }
- }
-
- for(n=0;n<8;n++)//發送R8位
- {
- R8<<=n;
- if(R8&0x80 == 0x80)
- {
- RGB_Set_Up();
- }
- else
- {
- RGB_Set_Down();
- }
- }
-
- for(n=0;n<8;n++)//發送B8位
- {
- B8<<=n;
- if(B8&0x80 == 0x80)
- {
- RGB_Set_Up();
- }
- else
- {
- RGB_Set_Down();
- }
- }
- }
- //復位碼
- void RGB_Rst()
- {
- WS2812 = 0;
- Delay50us();
- }
- //顏色交換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 char i=0;
- for( i=0; i<numLEDs; i++)
- {
- SetPixelColor(i, c);
- PixelUpdate();
- HAL_Delay(wait*5);
- }
- }
- void Watercolor(unsigned long c, unsigned int wait)
- {
- unsigned char i=0;
- for( i=0; i<numLEDs; i++)
- {
- SetPixelColor(i, c);
- PixelUpdate();
- HAL_Delay(wait*5);
- SetPixelColor(i, 0);
- PixelUpdate();
- HAL_Delay(wait);
- }
- }
- void main()
- {
- P1M0=0xFF;
- P1M1=0x00;
- P2M0=0xFF;
- P2M1=0x00;
- while(1)
- {
- on=0;
- /*colorWipe(0xff0000,1); //紅色
- HAL_Delay(10000);
- colorWipe(0xff00,1); //綠色
- HAL_Delay(10000);
- colorWipe(0xff,1);//藍色
- HAL_Delay(10000);
- colorWipe(0xffff00,1); //黃色
- HAL_Delay(10000);
- colorWipe(0xffff,1); //青色
- HAL_Delay(10000);
- colorWipe(0xff00ff,1); //紫色
- HAL_Delay(10000);
- colorWipe(0xffffff,1); //白色
- HAL_Delay(10000);
- colorWipe(0x00,1); //off
- HAL_Delay(10000);*/
- Watercolor(0xff0000, 100);//紅色
- HAL_Delay(1000);
- Watercolor(0xff00, 100);//綠色
- HAL_Delay(1000);
- Watercolor(0xff, 100);//藍色
- HAL_Delay(1000);
- Watercolor(0xffff00, 100); //黃色
- HAL_Delay(1000);
- Watercolor(0xffff, 100);//青色
- HAL_Delay(1000);
- Watercolor(0xff00ff, 100); //紫色
- HAL_Delay(1000);
- Watercolor(0xffffff, 100); //白色
- HAL_Delay(1000);
-
- colorWipe(0x00,1); //off
- HAL_Delay(10000);
- /*colorWipe(0xff0000,100); //紅色
- HAL_Delay(1000);
- colorWipe(0x00,100); //0ff
- HAL_Delay(1000);
- colorWipe(0xff00,100); //綠色
- HAL_Delay(1000);
- colorWipe(0x00,100); //0ff
- HAL_Delay(1000);
- colorWipe(0xff,100);//藍色
- HAL_Delay(1000);
- colorWipe(0x00,100); //0ff
- HAL_Delay(1000);
- colorWipe(0xffff00,100); //黃色
- HAL_Delay(1000);
- colorWipe(0x00,100); //0ff
- HAL_Delay(1000);
- colorWipe(0xffff,100); //青色
- HAL_Delay(1000);
- colorWipe(0x00,100); //0ff
- HAL_Delay(1000);
- colorWipe(0xff00ff,100); //紫色
- HAL_Delay(1000);
- colorWipe(0x00,100); //0ff
- HAL_Delay(1000);
- colorWipe(0xffffff,100); //白色
- HAL_Delay(1000);
- colorWipe(0x00,100); //0ff
- HAL_Delay(1000);*/
- }
- }
復制代碼
|