MrXploder
Joined: 14 Mar 2013 Posts: 6
|
WS2812B Library for PIC18F2550 (Work in Progress) |
Posted: Wed Nov 05, 2014 9:02 am |
|
|
This library only works on PIC18F2550 Chips (for now). I would like to receive some comments on how the code would be much better..
I base on the work from Adafruit NeoPixel Library for the examples and all the stuff you can find on the internet
Currently you can only use WS2812b with H/W SPI due to my poor acknowledge of ASM
/*PIC18F2550 MUST be running at 48MHZ in order to this library works,
The output is in pin SDO(C7) from H/W Spi, CLK(B1) and SDI(B1) could not be used.
In order to reduce led burnout risk, add 1000 uF capacitor across strip
power leads, add 300 - 500 Ohm resistor on first led's data input and minimize
distance between SDO and first Led. Avoid connecting on a live circuit,
if you must, connect GND first.*/
WS2812.h
Code: |
#define WS2812_ZERO 0b10000000
#define WS2812_ONE 0b11111000
#define STRIP_SIZE 25
volatile unsigned int8 Strip_RGBData[STRIP_SIZE][3]={0};
//takes a 24bit color and stores it in the array split it in 3 8bit variables
void Strip_setPixelColor(int8 pixel, int32 color){
Strip_RGBData[pixel][0] = (int8)(color >> 16);
Strip_RGBData[pixel][1] = (int8)(color >> 8);
Strip_RGBData[pixel][2] = (int8)(color);
}
//The same above, but does take separated values, "OVERLOADED FUNCTION"
void Strip_setPixelColor(int8 pixel, int8 r, int8 g, int8 b){
Strip_RGBData[pixel][0] = r;
Strip_RGBData[pixel][1] = g;
Strip_RGBData[pixel][2] = b;
}
//Makes a 24bit color from 3 8bit color variables
int32 Strip_Color(int8 r, int8 g, int8 b){
return ((int32)r << 16) | ((int32)g << 8) | b;
}
//Output the Strip_RGBData array to the chips
void Strip_Show(){
unsigned int8 x,y,z;
for(x=0;x<STRIP_SIZE;x++){
for(y=0;y<3;y++){
for(z=0;z<8;z++){
if(bit_test(Strip_RGBData[x][y],7-z)){
spi_write(ws2812_one);
}
else{
spi_write(ws2812_zero);
}
}
}
}
delay_us(40);
}
void Strip_ClearAll(){
memset(Strip_RGBData,0,STRIP_SIZE);
Strip_Show();
}
void Strip_SetAll(int8 value){
memset(Strip_RGBData,value,STRIP_SIZE);
Strip_Show();
}
void Strip_Init(){
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
Strip_Show();
}
|
WS2812_Effects.c
Code: |
int32 Wheel(int8 WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85){
return Strip_Color(255 - (WheelPos * 3), 0, WheelPos * 3);
}
else if(WheelPos < 170){
WheelPos -= 85;
return Strip_Color(0, (WheelPos * 3), 255 - WheelPos * 3);
}
else{
WheelPos -= 170;
return Strip_Color(WheelPos * 3, 255 - (WheelPos * 3), 0);
}
}
void Rainbow(int8 wait) {
unsigned int16 i,j;
for(j=0;j<256;j++) {
for(i=0;i<STRIP_SIZE;i++) {
Strip_setPixelColor(i, Wheel((i+j) & 255));
}
Strip_Show();
delay_ms(wait);
}
}
void RainbowCycle(int8 wait) {
int16 i,j;
for(j=0;j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0;i<STRIP_SIZE;i++) {
Strip_setPixelColor(i, Wheel(((i * 256 / STRIP_SIZE) + j) & 255));
}
Strip_Show();
delay_ms(wait);
}
}
void ColorWipe(int32 c, int8 wait) {
int16 i;
for(i=0;i<STRIP_SIZE;i++) {
Strip_setPixelColor(i,c);
Strip_Show();
delay_ms(wait);
}
}
void TheaterChase(int32 c, int8 wait){
int16 j,q,i;
for(j=0;j<10;j++){
for(q=0;q<3;q++){
for(i=0;i<STRIP_SIZE;i=i+3){
Strip_setPixelColor(i+q, c);
}
Strip_show();
delay_ms(wait);
for(i=0;i<STRIP_SIZE;i=i+3){
Strip_setPixelColor(i+q, 0);
}
}
}
}
void TheaterChaseRainbow(int8 wait){
int16 j,q,i;
for(j=0;j<256;j++){
for(q=0;q<3;q++){
for(i=0;i<STRIP_SIZE;i=i+3){
Strip_setPixelColor(i+q, Wheel((i+j) % 255));
}
Strip_show();
delay_ms(wait);
for(i=0;i<STRIP_SIZE;i=i+3){
Strip_setPixelColor(i+q,0);
}
}
}
}
|
"Example of use"
Code: |
#include <18f2550.h>
#device ADC=10
#fuses NOMCLR
#use delay(clock=48M, crystal=16M)
#use FAST_IO(ALL)
#include <WS2812.h>
#include <WS2812_Effects.c>
void main(){
int16 x,y,z;
set_tris_b(0b00000000);
set_tris_c(0b00000000);
Strip_Init();
while(true){
ColorWipe(strip_Color(255, 0, 0), 50);
ColorWipe(strip_Color(0, 255, 0), 50);
ColorWipe(strip_Color(0, 0, 255), 50);
TheaterChase(Strip_Color(127,127,127), 50);
TheaterChase(Strip_Color(127,0,0), 50);
TheaterChase(Strip_Color(0,127,0), 50);
TheaterChase(Strip_Color(0,0,127), 50);
Rainbow(20);
RainbowCycle(20);
TheaterChaseRainbow(50);
}
}
|
Remember that this is a work in progress, I would like to receive some feedback and support... sorry for my poor english.. Im from Chile... :D |
|