Quantcast
Channel: MEL PICBASIC Forum
Viewing all articles
Browse latest Browse all 4787

NeoPixel (WS2812B) Demo

$
0
0
In a previous thread (http://www.picbasic.co.uk/forum/showthread.php?t=18431) I've posted an example how to use those addressable RGB leds (WS2812B) aka NeoPixel. I've optimized the code and made a set of include files.

The NeoPixels can be used with almost any PIC running at 20MHz. The only limit is the available RAM: you need 3 arrays, one for each colour (RGB). The size of each array is the number of leds to control. With a large number of pixels I suggest using a PIC18F. See PicBASIC manual for more information on arrays limits.

A PIC running @20MHz is more than enough to make almost any animation. The values are pre-computed and then streamed in one shot. You have plenty of time to compute the next sequence unless you want to build a video screen!

Two gamma correction files are supplied for better fades in or out. One is for PIC18F and one for the others.
Ref.: https://learn.adafruit.com/led-trick.../the-quick-fix
See NeoPixelDump.pbp for instructions.

The demo code show you how to run them on PIC12F683. Now the only limit is your imagination! Be creative!



Demo code:
Code:

'****************************************************************
'*  Name    : NeoPixelDemo.pbp                                  *
'*  Author  : Michel Jasmin                                    *
'*  Notice  : NO Copyright (c) 2014 Michel Jasmin  ;)          *
'*          : No Rights Reserved                                *
'*  Date    : 2014-11-29                                        *
'*  Version : 1.0                                              *
'*  Notes  : Demo how to use WS2812B (neoPixels) on            *
'*          : a PIC12F683 @20MHz                                *
'****************************************************************

#IF __PROCESSOR__ = "12F683"
    #CONFIG
        __config _HS_OSC & _WDT_OFF & _MCLRE_ON & _CP_OFF
    #ENDCONFIG

#ELSE
    #MSG "Wrong Processor selected!"
#ENDIF

DEFINE OSC 20
DEFINE NO_CLRWDT 1  ' Don't waste cycles clearing WDT

'------------ Const -------------------------

NEO_NUM    CON 18  'Number of pixels

'------------ Variables -------------------------

NeoGreen    VAR BYTE[NEO_NUM]
NeoBlue    VAR BYTE[NEO_NUM]
NeoRed      VAR BYTE[NEO_NUM]
NeoPixel    VAR BYTE            'Looping variable
NeoPixValue VAR BYTE            'Value to be bit-banged

'------------ ALIAS -------------------------

NeoPin      VAR GPIO.2

'-----------  Initialization -------------------------------

Clear  ' Clear RAM before entry

ANSEL = 0
CMCON0 = 7  'Comparator disabled
CCP1CON = 0
WPU = 0
TRISIO.2 = 0 'NeoPin

NeoPin = 0

GOSUB NeoInit

'-----------  Main program -------------------------

Main:

    GOSUB NeoPixelDump
    GOSUB NeoPixelRotate

    PAUSE 50

    GOTO Main

END

NeoInit:
    'Init values for 18 leds string.
    FOR NeoPixel = 0 TO (NEO_NUM - 1)

        IF NeoPixel < 3 THEN
            NeoGreen[NeoPixel] = 0
            NeoBlue[NeoPixel] = 0
            NeoRed[NeoPixel] = 64

        ELSEIF NeoPixel < 6 THEN
            NeoGreen[NeoPixel] = 0
            NeoBlue[NeoPixel] = 64
            NeoRed[NeoPixel] = 0

        ELSEIF NeoPixel < 9 THEN
            NeoGreen[NeoPixel] = 0
            NeoBlue[NeoPixel] = 64
            NeoRed[NeoPixel] = 64

        ELSEIF NeoPixel < 12 THEN
            NeoGreen[NeoPixel] = 64
            NeoBlue[NeoPixel] = 0
            NeoRed[NeoPixel] = 0

        ELSEIF NeoPixel < 15 THEN
            NeoGreen[NeoPixel] = 64
            NeoBlue[NeoPixel] = 0
            NeoRed[NeoPixel] = 64
        ELSE
            NeoGreen[NeoPixel] = 64
            NeoBlue[NeoPixel] = 64
            NeoRed[NeoPixel] = 0
        ENDIF

    NEXT

    RETURN

NeoPixelRotate:
    FOR NeoPixel = 0 TO (NEO_NUM - 1)
        NeoGreen[NeoPixel] = NeoGreen[NeoPixel + 1]
        NeoBlue[NeoPixel] = NeoBlue[NeoPixel + 1]
        NeoRed[NeoPixel] = NeoRed[NeoPixel + 1]
    NEXT

    NeoGreen[NEO_NUM - 1] = NeoGreen[0]
    NeoBlue[NEO_NUM - 1] = NeoBlue[0]
    NeoRed[NEO_NUM - 1] = NeoRed[0]

    RETURN


INCLUDE "..\_Include\NeoPixelDump.pbp"

Attachment 7497Attachment 7497

Attached Files

Viewing all articles
Browse latest Browse all 4787

Trending Articles