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

another WS2812 library

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

Joined: 21 Oct 2005
Posts: 280

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.


#include <18LF46K22.h> 


#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];

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);

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++){

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.
   for(i=1;i<NUM_LEDS;i++) {

void main(){
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