Here is the code I use in C for WS2812B. This is in XC16 on PIC24 or PIC33 with 8 MHz instruction cycle.
Handcrafted assembly code? I think not. I think it is pretty straightforward. The rgb function is called for each LED in the strinp, followed by a delay of 50usec or more to cause the strip to latch the new data.
And here is the code for the "one" function in bobl, a language I am developing whose father is Ada and mother is Java. This on is for a 16MHz instruction cycle, so, twice as many instructions.
Bob
void one()
{
DATAOUT = 1;
__asm("NOP");
__asm("NOP");
__asm("NOP");
__asm("NOP");
__asm("NOP");
__asm("NOP");
DATAOUT = 0;
__asm("NOP");
__asm("NOP");
__asm("NOP");
}
void zero()
{
DATAOUT = 1;
__asm("NOP");
__asm("NOP");
DATAOUT = 0;
__asm("NOP");
__asm("NOP");
__asm("NOP");
__asm("NOP");
__asm("NOP");
__asm("NOP");
__asm("NOP");
}
void shift_out(unsigned char c)
{
if (c & 0x80) one(); else zero();
if (c & 0x40) one(); else zero();
if (c & 0x20) one(); else zero();
if (c & 0x10) one(); else zero();
if (c & 0x08) one(); else zero();
if (c & 0x04) one(); else zero();
if (c & 0x02) one(); else zero();
if (c & 0x01) one(); else zero();
}
void rgb(unsigned char r, unsigned char g, unsigned char b)
{
shift_out(g);
shift_out(r);
shift_out(b);
}
Handcrafted assembly code? I think not. I think it is pretty straightforward. The rgb function is called for each LED in the strinp, followed by a delay of 50usec or more to cause the strip to latch the new data.
And here is the code for the "one" function in bobl, a language I am developing whose father is Ada and mother is Java. This on is for a 16MHz instruction cycle, so, twice as many instructions.
-- this is tricky. It all depends on timing. I count on the fact
-- the compiler will generate exactly 1 instruction taking 1
-- cycle to execute. Thus this routine will keep the line high for
-- 14 cycles and low for 6 cycles. Which, with a 16 MHz instruction
-- clock translates to 875 usec high and 375 usec low. This is
-- within the WS2812B specs of 850 and 400 += 150 for the high
-- and low times.
static procedure one is
begin
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 1;
portb.lat.b12 := 0;
portb.lat.b12 := 0;
portb.lat.b12 := 0;
portb.lat.b12 := 0;
portb.lat.b12 := 0;
portb.lat.b12 := 0;
end one;
Bob