Skip to content

Instantly share code, notes, and snippets.

@terryspitz
Last active May 1, 2022 10:47
Show Gist options
  • Save terryspitz/970b801030fc6008e65956263c2f6500 to your computer and use it in GitHub Desktop.
Save terryspitz/970b801030fc6008e65956263c2f6500 to your computer and use it in GitHub Desktop.
Using PIC SPI to control WS2812b neopixel
void WS281x_Send() {
//[My] WS2812b seems sensitive only to timing of high, signal can be low for any period less than 50us reset.
//Using PIC at 32Mhz, SPI at FOSC/4 = 8Mhz
//Write one SPI byte per WS2812 bit,
//for WS2812 1 bit use pattern 0b01111110 which is high for 6x125=750ns
//for WS2812 0 bit use pattern 0b00111000 which is high for 3x125=375ns
//(keep first SPI bit low as this affects timings - SPI SDO goes high early before the clock starts)
//and ignore delays between SPI bytes as it stays low
for (char b=0; b < 3 * LED_COUNT; ++b) {
char colour = WS281x_buffer[b];
for (char i = 7; i < 8; --i) {
SPI1_WriteByte((colour & 1U << i) ? 0b01111110 : 0b00111000);
}
}
}
@terryspitz
Copy link
Author

v1 tested on PIC16F15214

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment