CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

another WS2812 library

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
guy



Joined: 21 Oct 2005
Posts: 297

View user's profile Send private message Visit poster's website

another WS2812 library
PostPosted: Mon Jul 06, 2015 2:34 am     Reply with quote

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();
   }
}
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

Great Thank-you
PostPosted: Tue Jan 21, 2020 11:38 am     Reply with quote

It work great and also at 32Mhz on a PIC18F14K42.
_________________
in médio virtus
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group