WS2812 Communication

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
gritt
Posts: 11
Joined: Tue May 25, 2010 12:06 pm
Been thanked: 1 time
Contact:

WS2812 Communication

Post by gritt »

Has anyone tried controlling a WS2812 RGB led driver from a PIC using flowcode V4?

Datasheet is here http://www.adafruit.com/datasheets/WS2812.pdf
It uses a single data input line that has it's own specifically timed data format (different length of pulse for 1 and 0, at a rate of 800khz with a delay between each group of bytes).

I assume the best way to control it is using bit banging and have had a look at the spi_byte flowcide macro but couldn't work out how to make it run at a precise rate. Would I be best to use an interrupt?

Can anyone suggest the best way to go?

Thanks

James

gritt
Posts: 11
Joined: Tue May 25, 2010 12:06 pm
Been thanked: 1 time
Contact:

Re: WS2812 Communication

Post by gritt »

If anyone is interested then the following code seems to work. This was borrowed from http://www.vectric.com/forum/viewtopic.php?f=3&t=17937 then modified slightly to work with my PIC.
Pin B0 is connected to the data line of the WS2812

Code: Select all

void OutputAZeroBit ()
{
portb = 0x01;
nop();
nop();
portb = 0x00;
nop();
nop();
nop();
}
//
void OutputAOneBit ()
{
portb = 0x01;
nop();
nop();
nop();
nop();
nop();
portb = 0x00;
}

//
void OutputByte (char Byte)
{
char i;
// data sent to the LEDs must be sent MSB first
for (i=1; i<=8 ; i++)
{
if ( Byte > 127 ) // Is MSB on?
OutputAOneBit();
else
OutputAZeroBit();

Byte = Byte * 2; // shift left one bit
}
}
This assumes that the code runs at 8M instructions / sec.

James

Post Reply