| guy 
 
 
 Joined: 21 Oct 2005
 Posts: 297
 
 
 
			      
 
 | 
			
				| another WS2812 library |  
				|  Posted: Mon Jul 06, 2015 2:34 am |   |  
				| 
 |  
				| My code uses the UART to output the time-critical waveforms required to control WS2812 LEDs. It will not work on any PIC since it requires: * INVERTED UART OUTPUT (could also be done in hardware)
 * 64 MHz clock (should also work on 32MHz, not tested)
 
 Here is an example of the waveform:
 
   
 Here is the code for the library with a small demo.
 
 
  	  | Code: |  	  | #include <18LF46K22.h>
 
 #fuses INTRC_IO,NOPROTECT,NOPLLEN,NOBROWNOUT,NOLVP,NOWDT
 
 #use delay(clock=64000000)
 #use rs232(stream=LED_STREAM, baud=8000000, xmit=PIN_C6, rcv=PIN_C7,bits=8,parity=N,stop=0,ERRORS,INVERT)
 
 #define NUM_LEDS 24
 
 #define HI_BIT   192   // 0b11000000
 #define LOW_BIT   252   // 0b11111100
 
 
 //==========global  variables:===========
 
 byte grn [NUM_LEDS];
 byte blu [NUM_LEDS];
 byte red [NUM_LEDS];
 
 ///////////////////////////////////////////////
 #INLINE
 void SendByte (byte b) {
 byte i;
 
 // send 8 bits, MSB first.
 // for each bit send a CHAR that creates the bit's waveform
 for(i=0;i<8;i++) {
 if(bit_test(b,7)) fputc(HI_BIT,LED_STREAM);
 else fputc(LOW_BIT,LED_STREAM);
 b<<=1;
 }
 }
 ////////////////////////////////////
 
 void Draw (){
 unsigned int8 i;
 
 // send all data to LEDs
 // data order to transmit is G,R,B, 8 bits each.
 
 for (i=0;i<NUM_LEDS;i++){
 SendByte(grn[i]);
 SendByte(red[i]);
 SendByte(blu[i]);
 }
 }
 
 /////////////////////////////////////
 void initArray() {
 byte i;
 
 // assuming 24 LEDs:
 for(i=0;i<NUM_LEDS;i++) {
 // red channel:
 if(i<6) red[i]=i*51;
 else if(i<12) red[i]=(11-i)*51;
 else red[i]=0;
 // grn channel:
 if(i<8) grn[i]=0;
 else if(i<14) grn[i]=(i-8)*51;
 else if(i<20) grn[i]=(19-i)*51;
 else grn[i]=0;
 // blu channel:
 if(i<4) blu[i]=(3-i)*51;
 else if(i<16) blu[i]=0;
 else if(i<22) blu[i]=(i-16)*51;
 else blu[i]=(27-i)*51;
 }
 }
 ///////////////////////////////////////
 void Rotate() {
 byte r,g,b,i;
 
 // rotate colors in array. This is only for demo.
 r=red[0];
 g=grn[0];
 b=blu[0];
 for(i=1;i<NUM_LEDS;i++) {
 red[i-1]=red[i];
 grn[i-1]=grn[i];
 blu[i-1]=blu[i];
 }
 red[NUM_LEDS-1]=r;
 grn[NUM_LEDS-1]=g;
 blu[NUM_LEDS-1]=b;
 }
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 
 
 void main(){
 
 setup_oscillator(OSC_64MHZ);
 initArray();
 delay_ms(100);
 
 while(true){
 Draw();
 delay_ms(60);
 Rotate();
 }
 }
 
 | 
 |  |