Les's WS2812 code


+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Les's WS2812 code835 days old

  1. #1
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,350
    Thumbs Up
    Received: 29
    Given: 7
    Total Downloaded
    1.09 GB

    0 Not allowed!

    Default Les's WS2812 code

    Hi Les,
    I have tried your code http://www.protonbasic.co.uk/showthr...roller-library! on a neopixel ring https://coolcomponents.co.uk/product...grated-drivers and I have very unpredictable results.
    I was just wondering if you tried the code on actual hardware?
    I modified it (as I was powering it from a pin) to this which I assumed I would get 12 very dim red LED's but I get incredibly bright LEDS even though I have only a value of 1. I get 2 bright red LED's, a dimmer one and then a bright blue, a gap and another bright blue one even though they should only be red?
    Code:
    Device = 18F25K20
    Declare Xtal = 64
    
    $define WS2812B_Pin PORTB.0 ' Assign the pin used for the WS2812B devices
    $define WS2812B_Amount 12 ' The amount of WS2812B devices on the line
    
    Include "WS2812B.inc" ' Load the RGB WS2812B routines into the program
    
    Dim ChipNumber As Byte
    Dim Red[WS2812B_Amount] As Byte
    Dim Green[WS2812B_Amount] As Byte
    Dim Blue[WS2812B_Amount] As Byte
    
    '---------------------------------------------------------------------------------------------
    ' The main program starts here
    '
    Main:
    lOOP:
    Clear Green
    Clear Blue
    Red = 1
    
    For ChipNumber = 0 To WS2812B_Amount - 1
    WS2812B_Colour(ChipNumber, Red[ChipNumber], Green[ChipNumber], Blue[ChipNumber])
    DelayMS 5
    Next
    
    GoTo lOOP

Attention

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

The advertisements we display are relevant to this web site and your browsing history

Please consider supporting us by disabling your ad blocker.


Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues


Thank you for your attention.

  • #2
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,350
    Thumbs Up
    Received: 29
    Given: 7
    Total Downloaded
    1.09 GB

    0 Not allowed!

    Default Re: Les's WS2812 code

    Oops,
    (as I was powering it from a pin)
    Meant to say USB.

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #3
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,350
    Thumbs Up
    Received: 29
    Given: 7
    Total Downloaded
    1.09 GB

    0 Not allowed!

    Default Re: Les's WS2812 code

    Hi,
    I have a bit more information, the Neopixel ring I have needs this line
    Code:
     '   WS2812B_RGB(WS2812B_pRed, WS2812B_pGreen,WS2812B_pBlue)             ' Yes. So access a single WS2812B
    'Change the byte order as my Neopixel ring shows different colours. 
        WS2812B_RGB(WS2812B_pBlue,WS2812B_pRed, WS2812B_pGreen)            ' Yes. So access a single WS2812B
    and this one
    Code:
      '      WS2812B_RGB(WS2812B_Red[WS2812B_bIndex], WS2812B_Green[WS2812B_bIndex], WS2812B_Blue[WS2812B_bIndex])
    'Change the byte order as my Neopixel ring shows different colours.   
            WS2812B_RGB(WS2812B_Blue[WS2812B_bIndex],WS2812B_Red[WS2812B_bIndex],WS2812B_Green[WS2812B_bIndex])
    changed as the colours are different.

    When I have 12 for the number of LED's it only illuminates 8.
    They show Red, Blue, Red, Blue, Red, Blue, Red, Blue
    but if I slow it down (the loop) the blue ones go green first and on the second loop they change to blue so it appears there is a timing issue somewhere?

    Any ideas would be greatly appreciated, I have orders from my sister they must be working for christmas or I don't get Christmas in Majorca

    Regards,
    Bob

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #4
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,350
    Thumbs Up
    Received: 29
    Given: 7
    Total Downloaded
    1.09 GB

    0 Not allowed!

    Default Re: Les's WS2812 code

    Hi,
    Getting somewhere, clutching at straws I decided to play around with the timing values and am a lot closer to it working properly.
    Changed the lines in post 3 back and then altered the timing values by trial and error and found that changing the zero bit from 4 to 8 is nearly there, I get all 12 lit, they all go to the correct colour first time, the only issue is that the brightness is not quite right.
    A value of 20 gives a dim LED for the first one but all the others are very bright.
    Code:
    $if _xtal = 64                                      ' Are we using a 64MHz oscillator?
        $define cWS2812B_Zero  8                        ' Amount of cycles for a zero delay (approx 350ns)
    Hopefully will play a bit more later, not really happy with the less than scientific approach but if it work I will take it.

    Bob

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #5
    Member Dompie's Avatar
    Join Date
    Apr 2012
    Posts
    129
    Thumbs Up
    Received: 16
    Given: 13
    Total Downloaded
    5.18 GB

    1 Not allowed!

    Default Re: Les's WS2812 code

    I have tried the the code too and the result is OK. So Les his code works perfect for my Chinese strip (1 meter=144 WS2812B's). The only change I have made is amount=144 in stead of 64. See enclosed video

    Last edited by Dompie; 6th November 2017 at 21:54.

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #6
    Junior Member Maxi's Avatar
    Join Date
    Mar 2010
    Posts
    93
    Thumbs Up
    Received: 1
    Given: 25
    Total Downloaded
    5.30 GB

    0 Not allowed!

    Default Re: Les's WS2812 code

    sorry guys, I want to ask little questions
    are you use 64mhz with external crystal?
    I trying use it with int osc and pll (16x4) , dont work

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #7
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,478
    Thumbs Up
    Received: 313
    Given: 146
    Total Downloaded
    1.99 GB

    0 Not allowed!

    Default Re: Les's WS2812 code

    Thanks for the video Dompie.

    I tried my code on actual devices and simulation and it worked without any changes required. The only thing I can think of Bob, is that you have either slightly different WS2812 devices, or you are not running at 40 or 64MHz. The WS2812 devices do use very tight pulses for coms, but they are only an approximation, and pulses shorter, or even, slightly larger still work.

    Maxi. Make sure your device is running at 64MHz. Sometimes, just altering the configs is not enough, some SFRs also need manipulating. To test your device, the simplest thing to do is send a string of "Hello World" out via either Rsout or Hrsout and see if it received at the correct Baud. If not, the internal oscillator is not running right.

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #8
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,350
    Thumbs Up
    Received: 29
    Given: 7
    Total Downloaded
    1.09 GB

    0 Not allowed!

    Default Re: Les's WS2812 code

    Hi Les,
    Thanks for replying, I have just noticed on the site where I bought them that it says WS2812 and then in other places SK6812.
    Googling it they are supposed to be direct replacements but looking at the datasheet the timing is slightly different. I have played around with the timing and the best I can get is the first one appears correct but all the others are too bright. The number is right and they are all the right colour so I cannot see what the issue is with the brightness.

    I have just tried Red = 255 and all appear to be the correct colour and brightness, I then did Red = 0 and the first one is off but all the others appear full brightness?

    Bob
    Last edited by RGV250; 7th November 2017 at 12:03.

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #9
    Member Dompie's Avatar
    Join Date
    Apr 2012
    Posts
    129
    Thumbs Up
    Received: 16
    Given: 13
    Total Downloaded
    5.18 GB

    1 Not allowed!

    Default Re: Les's WS2812 code

    @Maxi
    The demo video was made with a 18F26K22 - 64Mhz Internal.
    My standard include for 18F2xK22/K20 64Mhz is:

    OSCCONbits_IRCF2 = 1 : OSCCONbits_IRCF1 = 1 : OSCCONbits_IRCF0 = 1 ;16Mhz internal clock
    OSCTUNEbits_PLLEN = 1 ;Enable PLL
    DelayMS 10 ;Instabiele XTAL afwachten
    Declare Xtal = 64 ;Vertel de compiler dat het 64Mhz is

    And my Fuses include file is:
    ;-------------------------------------------------------------------------------
    ;-------------------------------------------------------------------------------
    ;**** Added by Fuse Configurator ****
    ; Use the Fuse Configurator plug-in to change these settings


    ;Device = 18F26K2x ;K20=3.3V versie en K22=5V versie


    Config_Start
    ;Zie tabel 24.1 blz 344 voor overzicht
    ;CONFIG1L = leeg
    ;CONFIG1H = 24.2 = blz 345
    ; FOSC = HSMP ;HS oscillator (Medium Power 4-16Mhz)
    FOSC = INTIO67
    ; PLLCFG = On ;4 x PLL always enabled, Oscillator multiplied by 4 (ext xTal)
    PLLCFG = Off ;PLL is under Software control OSCTUNE.6 (OSCTUNE_PLLEN)
    PRICLKEN = On ;Primary clock enabled
    FCMEN = OFF ;Fail-Safe Clock Monitor disabled
    IESO = OFF ;Oscillator Switchover mode disabled
    ;CONFIG2L
    PWRTEN = OFF ;Power up timer disabled
    BOREN = SBORDIS ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
    BORV = 190 ;VBOR set to 1.90 V nominal
    ;CONFIG2H
    WDTEN = OFF ;Watch dog timer is always disabled. SWDTEN has no effect.
    WDTPS = 32768 ;1:32768
    ;CONFIG3L = Leeg
    ;CONFIG3H
    CCP2MX = PORTC1 ;CCP2 input/output is multiplexed with RC1
    PBADEN = OFF ;PORTB<5:0> pins are configured as digital I/O on Reset
    CCP3MX = PORTB5 ;P3A/CCP3 input/output is multiplexed with RB5
    HFOFST = On ;HFINTOSC output and ready status are not delayed by the oscillator stable status
    T3CMX = PORTC0 ;T3CKI is on RC0
    P2BMX = PORTB5 ;P2B is on RB5
    MCLRE = INTMCLR ;RE3 input pin enabled; MCLR disabled
    ;CONFIG4L
    STVREN = On ;Stack full/underflow will cause Reset
    LVP = OFF ;Single-Supply ICSP disabled
    XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    Debug = OFF ;Disabled
    ;CONFIG4H = leeg
    ;CONFIG5L
    Cp0 = OFF ;Block 0 (000800-003FFFh) not code-protected
    CP1 = OFF ;Block 1 (004000-007FFFh) not code-protected
    CP2 = OFF ;Block 2 (008000-00BFFFh) not code-protected
    CP3 = OFF ;Block 3 (00C000-00FFFFh) not code-protected
    ;CONFIG5H
    CPB = OFF ;Boot block (000000-0007FFh) not code-protected
    CPD = OFF ;Data EEPROM not code-protected
    ;CONFIG6L
    WRT0 = OFF ;Block 0 (000800-003FFFh) not write-protected
    WRT1 = OFF ;Block 1 (004000-007FFFh) not write-protected
    WRT2 = OFF ;Block 2 (008000-00BFFFh) not write-protected
    WRT3 = OFF ;Block 3 (00C000-00FFFFh) not write-protected
    ;CONFIG6H
    WRTC = OFF ;Configuration registers (300000-3000FFh) not write-protected
    WRTB = OFF ;Boot Block (000000-0007FFh) not write-protected
    WRTD = OFF ;Data EEPROM not write-protected
    ;CONFIG7L
    EBTR0 = OFF ;Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
    EBTR1 = OFF ;Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
    EBTR2 = OFF ;Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
    EBTR3 = OFF ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
    ;CONFIG7H
    EBTRB = OFF ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
    Config_End


    ;**** End of Fuse Configurator Settings ****
    ;-------------------------------------------------------------------------------
    ;-------------------------------------------------------------------------------

    <Edit> Sorry some Dutch words included</Edit>

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #10
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,350
    Thumbs Up
    Received: 29
    Given: 7
    Total Downloaded
    1.09 GB

    1 Not allowed!

    Default Re: Les's WS2812 code

    Hi,
    I think I have found the issue, apart from the timing alteration (changing cWS2812B_Zero to 8 from 4) which is needed for my setup the demo works as it should.
    The problem comes when I tried to have a lower intensity so instead of
    Code:
    Clear Green
    Clear Blue
    Set Red
    I changed it to
    Code:
    Clear Green
    Clear Blue
    Red = 10
    which appears to cause the issue, I can only assume "Set" is faster than "Red = 10"

    If I change it to this so the array is loaded first it works as expected.
    Code:
    For ChipNumber = 0 To WS2812B_Amount - 1
    Red[ChipNumber] = 10
    Next
    @ Dompie,
    Can you try altering the intensity of your LED's as Colour = xx to see if the issue is the same on yours.

    Regards,
    Bob

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #11
    Junior Member Maxi's Avatar
    Join Date
    Mar 2010
    Posts
    93
    Thumbs Up
    Received: 1
    Given: 25
    Total Downloaded
    5.30 GB

    0 Not allowed!

    Default Re: Les's WS2812 code

    thank you its working now
    but colors red, light yellow ,green and white (its very bright)
    (chinese led strip, 11 leds)

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #12
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,350
    Thumbs Up
    Received: 29
    Given: 7
    Total Downloaded
    1.09 GB

    1 Not allowed!

    Default Re: Les's WS2812 code

    Hi Maxi,
    Try using this, you can alter the brightness and speed, currently set for 5 and 100.

    Bob

    Code:
    ' WS2812B RGB Interface
    '
    Device = 18F25K20
    Declare Xtal = 64
    
    $define WS2812B_Pin PORTB.0 ' Assign the pin used for the WS2812B devices
    $define WS2812B_Amount 11 ' The amount of WS2812B devices on the line
    
    Include "WS2812B.inc" ' Load the RGB WS2812B routines into the program
    
    Dim ChipNumber As Byte
    Dim Red[WS2812B_Amount] As Byte
    Dim Green[WS2812B_Amount] As Byte
    Dim Blue[WS2812B_Amount] As Byte
    
    Dim Brightness As Byte = 5
    Dim Delay As Word = 100
    
    Main:
    
    'Red  ---------------------------------------
    
    Clear Green
    Clear Blue
    'Load the Red array with a value of 5
    For ChipNumber = 0 To WS2812B_Amount - 1
    Red[ChipNumber] = Brightness
    Next
    
    'Set Red
    For ChipNumber = 0 To WS2812B_Amount - 1
    WS2812B_Colour(ChipNumber, Red[ChipNumber], Green[ChipNumber], Blue[ChipNumber])
    DelayMS Delay
    Next
    
    'Green --------------------------------------
    
    Clear Red
    Clear Blue
    'Load the Green array with a value of 5
    For ChipNumber = 0 To WS2812B_Amount - 1
    Green[ChipNumber] = Brightness
    Next
    
    'Set Green
    For ChipNumber = 0 To WS2812B_Amount - 1
    WS2812B_Colour(ChipNumber, Red[ChipNumber], Green[ChipNumber], Blue[ChipNumber])
    DelayMS Delay
    Next
    
    'Blue ---------------------------------------
    
    Clear Red
    Clear Green
    'Load the Blue array with a value of 5
    For ChipNumber = 0 To WS2812B_Amount - 1
    Blue[ChipNumber] = Brightness
    Next
    
    'Set Blue
    For ChipNumber = 0 To WS2812B_Amount - 1
    WS2812B_Colour(ChipNumber, Red[ChipNumber], Green[ChipNumber], Blue[ChipNumber])
    DelayMS Delay
    Next
    
    'Clear --------------------------------------
    
    Clear Red
    Clear Green
    Clear Blue
    For ChipNumber = 0 To WS2812B_Amount - 1
    WS2812B_Colour(ChipNumber, Red[ChipNumber], Green[ChipNumber], Blue[ChipNumber])
    DelayMS Delay
    Next
    
    GoTo Main
    Last edited by RGV250; 7th November 2017 at 19:29.

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #13
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,478
    Thumbs Up
    Received: 313
    Given: 146
    Total Downloaded
    1.99 GB

    0 Not allowed!

    Default Re: Les's WS2812 code

    If they are SK6812 devices Bob, they use a 32-bit protocol for Red, Green, Blue and White (RGBW), whereas the WS2812 devices use a 24-bit protocol for Red, Green, and Blue (RGB). This would cause all sorts of problems, as the device would be seeing the next device's byte. There's also a slight timing difference in the WS2812 and WS2812B devices.

    I have written a similar library for the SK6812 devices and I'll post it asap. It uses the same commands as the WS2812 library, but they are named SK6812_xxxx instead of WS2812_xxxx.
    Last edited by top204; 7th November 2017 at 20:01.

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #14
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,478
    Thumbs Up
    Received: 313
    Given: 146
    Total Downloaded
    1.99 GB

    0 Not allowed!

    Default Proton SK6812 RGBW LED library

    Here's the library include file for the SK6812. It hasn't been as extensively tested as the WS code, but it works on real devices.

    Place the code below in an include file named "SK6812.inc"

    Code:
    $ifndef  _SK6812_INC_
    $define _SK6812_INC_
    '
    ' SK6812 RGBW LED interface
    ' Because of the very tight pulse requirements of the SK6812 device, this code is for a PICmicro device operating at 40MHz or 64MHz
    '
    ' Written by Les Johnson for the Proton BASIC compiler.
    '
    ' Create variables
    '
        Dim SK6812_bIndex    As Byte System             ' Used to access the individual SK6812 devices on the line
        Dim SK6812_bBitIndex As Byte System             ' Used to access each bit in the SK6812 interface
        Dim SK6812_dRGBW     As Dword System            ' Used to hold the Red, Green, Blue, and White bytes
    '
    ' The colour of each LED is encoded as four LED brightness values, which must be sent in GRBW (green, red, blue, white) order.
    '
        Dim SK6812_bGreen As SK6812_dRGBW.Byte3         ' Alias the green byte
        Dim SK6812_bRed   As SK6812_dRGBW.Byte2         ' Alias the red byte
        Dim SK6812_bBlue  As SK6812_dRGBW.Byte1         ' Alias the blue byte
        Dim SK6812_bWhite As SK6812_dRGBW.Byte0         ' Alias the white byte
    
        Dim SK6812_bChipNumber As Byte System           ' The SK6812 to access on a line of them
        Dim SK6812_pGreen As Byte System                ' The Green value for the SK6812 to System on a line of them
        Dim SK6812_pRed   As Byte System                ' The Red value for the SK6812 to System on a line of them
        Dim SK6812_pBlue  As Byte System                ' The Blue value for the SK6812 to System on a line of them
        Dim SK6812_pWhite As Byte System                ' The White value for the SK6812 to System on a line of them
    '
    ' The amount of SK6812 chips on the line
    '
    $ifndef SK6812_Amount
        $define SK6812_Amount 1                         ' The amount of SK6812 chips on the line (default to 1 if not given in the main program)
    $endif
    $define cSK6812_IndexAmount $eval (SK6812_Amount - 1)
    
    $if SK6812_Amount <> 1                              ' Is there only 1 SK6812 on the line?
        Dim SK6812_Red[SK6812_Amount]   As Byte         ' Holds the red value for a particular SK6812 device
        Dim SK6812_Green[SK6812_Amount] As Byte         ' Holds the green value for a particular SK6812 device
        Dim SK6812_Blue[SK6812_Amount]  As Byte         ' Holds the blue value for a particular SK6812 device
        Dim SK6812_White[SK6812_Amount] As Byte         ' Holds the white value for a particular SK6812 device
        Dim SK6812_dColours[SK6812_Amount] As Dword At SK6812_Red
    $endif
    '
    ' SK6812 pulse timing constants for 40MHz and 64MHz oscillators
    '
    $if _xtal = 64                                      ' Are we using a 64MHz oscillator?
        $define cSK6812_Zero  4                         ' Amount of cycles for a zero delay (approx 350ns)
        $define cSK6812_One   14                        ' Amount of cycles for a one delay (approx 900ns)
    $elseif _xtal = 40                                  ' Are we using a 40MHz oscillator?
        $define cSK6812_Zero  2                         ' Amount of cycles for a zero delay (approx 350ns)
        $define cSK6812_One   8                         ' Amount of cycles for a one delay (approx 900ns)
    $else
        $error "Only 40MHz or 64MHz oscillators suitable for this code because of the tight timing on a SK6812 device"
    $endif
    $define cSK6812_Reset 80                            ' Amount of microseconds for a reset delay
    '
    ' The pin that the SK6812 chip is connected too
    '
    $ifndef SK6812_Pin
        $define SK6812_Pin PORTB.0
    $endif
    '---------------------------------------------------------------------------------------------
        GoTo _SK6812_Main                               ' Jump over the routines
    
    '---------------------------------------------------------------------------------------------
    ' Send a 0 (approx 0.35us) to the SK6812 device
    '
    $define SK6812_SendZero() '
        Set SK6812_Pin        '
        DelayCS cSK6812_Zero  '
        Clear SK6812_Pin
    
    '---------------------------------------------------------------------------------------------
    ' Send a 1 (approx 0.9us) to the SK6812 device
    '
    $define SK6812_SendOne() '
        Set SK6812_Pin       '
        DelayCS cSK6812_One  '
        Clear SK6812_Pin
    
    '---------------------------------------------------------------------------------------------
    ' Send a reset (80us) to the SK6812 device
    '
    $define SK6812_Finish() '
        Clear SK6812_Pin    '
        DelayUS cSK6812_Reset
    
    '---------------------------------------------------------------------------------------------
    ' Interface to a single SK6812 RGBW controller chip
    ' Input     : pRed holds the red value (0 to 255)
    '           : pGreen holds the green value (0 to 255)
    '           : pBlue holds the blue value (0 to 255)
    '           : pWhite holds the white value (0 to 255)
    ' Output    : None
    ' Notes     : Sends the 32-bits MSB.
    '           : A zero bit is a high pulse for approx 350ns
    '           : A one bit is a high pulse for approx 900ns
    '
    $define SK6812_RGBW(pRed, pGreen, pBlue, pWhite) '
        SK6812_bRed = pRed                  '
        SK6812_bGreen = pGreen              '
        SK6812_bBlue = pBlue                '
        SK6812_bWhite = pWhite              '
        _SK6812_RGBW
    
    _SK6812_RGBW Macro-
        GoSub __SK6812_RGBW
    Endm
    
    #ifMacro- _SK6812_RGBW, _SK6812_Colour
    __SK6812_RGBW:
        For SK6812_bBitIndex = 31 To 0 Step -1      ' Create a loop for the 32-bits of data to send to the SK6812
            If SK6812_dRGBW.31 = 1 Then             ' Is the bit a 1?
                SK6812_SendOne()                    ' Yes. So send a 1 bit to the SK6812 chip
            Else                                    ' Otherwise... We have a zero bit. So...
                SK6812_SendZero()                   ' Send a 0 bit to the SK6812 chip
            EndIf
            Rol SK6812_dRGBW                        ' Left Rotate the 32-bits (MSB first)
        Next
        Return
    #endIfMacro-
    
    '---------------------------------------------------------------------------------------------
    ' Illuminate an SK6812 based upon its position in a string of them
    ' Input     : pChipNumber holds the SK6812 in the line to alter (0 to 255)
    '           : pRed holds the red value (0 To 255)
    '           : pGreen holds the green value (0 to 255)
    '           : pBlue holds the blue value (0 to 255)
    '           : pWhite holds the white value (0 to 255)
    ' Output    : None
    ' Notes     : None
    '
    $define SK6812_Colour(pChipNumber, pRed, pGreen, pBlue, pWhite)  '
        SK6812_bChipNumber = pChipNumber    '
        SK6812_pRed = pRed                  '
        SK6812_pGreen = pGreen              '
        SK6812_pBlue = pBlue                '
        SK6812_pWhite = pWhite              '
        _SK6812_Colour
    
    _SK6812_Colour Macro-
        GoSub __SK6812_Colour
    Endm
    
    #ifMacro- _SK6812_Colour
    __SK6812_Colour:
    $if SK6812_Amount = 1                                                       ' Is there only 1 SK6812 on the line?
        SK6812_RGBW(SK6812_pRed, SK6812_pGreen, SK6812_pBlue, SK6812_pWhite)    ' Yes. So System a single SK6812
        SK6812_Finish()                                                         ' Bring the pin low for approx 50us to reset the SK6812
        Return
    $else                                                                       ' Otherwise... There's more than 1 SK6812 on the line. So...
        If SK6812_bChipNumber > SK6812_Amount - 1 Then Return                   ' Return early if the chip number is more than the amount on the line
        SK6812_bIndex = 0                                                       ' \ Create a loop for the amount of SK6812 devices on the line
        Repeat                                                                  ' /
            If SK6812_bIndex = SK6812_bChipNumber Then                          ' Is it the chip we want to alter?
                SK6812_Red[SK6812_bIndex]   = SK6812_pRed                       ' \
                SK6812_Green[SK6812_bIndex] = SK6812_pGreen                     ' | Yes. So save the colour in the colour array
                SK6812_Blue[SK6812_bIndex]  = SK6812_pBlue                      ' |
                SK6812_White[SK6812_bIndex] = SK6812_pWhite                     ' /
            EndIf
            SK6812_RGBW(SK6812_Red[SK6812_bIndex], SK6812_Green[SK6812_bIndex], SK6812_Blue[SK6812_bIndex], SK6812_White[SK6812_bIndex])
            Inc SK6812_bIndex
        Until SK6812_bIndex >= SK6812_Amount                                    ' Loop for all the devices attached to the line
        SK6812_Finish()                                                         ' Bring the pin low for approx 80us to reset the SK6812 devices
        Return
    $endif
    #endIfMacro-
    
    '---------------------------------------------------------------------------------------------
    ' Setup the interface with SK6812 RGB controller chips
    ' Input     : None
    ' Output    : None
    ' Notes     : None
    '
    $define SK6812_Setup() _SK6812_Setup
    
    _SK6812_Setup Macro-
        GoSub __SK6812_Setup
    Endm
    
    #ifMacro- _SK6812_Setup
    __SK6812_Setup:
        Low SK6812_Pin                  ' Make the pin that connects to the SK6812 chip a low output
        DelayMS 10                      ' And reset the SK6812
    $if SK6812_Amount <> 1              ' Is there only 1 SK6812 on the line?
        Clear SK6812_Red                ' \
        Clear SK6812_Green              ' | No. So clear the arrays holding the SK6812 colours
        Clear SK6812_Blue               ' |
        Clear SK6812_White              ' /
    $endif
        Return
    #endIfMacro-
    
    '---------------------------------------------------------------------------------------------
    ' The main library routine starts here
    '
    _SK6812_Main:
        SK6812_Setup()     ' Setup the interface to the SK6812 RGBW controller chip
    
    $endif  ' _SK6812_INC_
    A simple demo is:
    Code:
    ' SK6812 RGBW LED controller Interface
    '
        Device = 18F25K20
        Declare Xtal = 64
        
    $define SK6812_Pin PORTB.0
    $define SK6812_Amount 2
        
        Include "SK6812.inc"                       ' Load the RGBW SK6812 routines into the program
       
        Dim Red As Byte
        Dim Green As Byte
        Dim Blue As Byte
        Dim White As Byte
        
        GoTo Main
                
    '---------------------------------------------------------------------------------------------
    ' The main program starts here
    '   
    Main:
    
        Red = 255
        Green = 0
        Blue = 0
        White = 0
        Do
            SK6812_RGBW(Red, Green, Blue, White)
            Inc White
            If White > 127 Then White = 0
            'Inc Blue
            DelayMS 100
        Loop

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #15
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,350
    Thumbs Up
    Received: 29
    Given: 7
    Total Downloaded
    1.09 GB

    0 Not allowed!

    Default Re: Les's WS2812 code

    Hi Les,
    I tried that but not a lot happened, I am pretty sure they are WS2812, on the cool components web site the title says SK6812 but all the other documentation says WS2812.
    I have it working fine now apart from having to change the zero value from 4 to 8, that makes the demo work as planned but when I tried to dim them using Red = 10 etc it caused all sorts of problems.
    If I pre-load the arrays rather than using Red = 10 etc it works as it should.

    Regards,
    Bob

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

    Thread Information

    Users Browsing this Thread

    There are currently 1 users browsing this thread. (0 members and 1 guests)

       

    Similar Threads

    1. Les's ILI9325 Wiki code
      By charliecoultas in forum LCD module
      Replies: 136
      Last Post: 1st February 2020, 11:57
    2. Replies: 4
      Last Post: 17th September 2015, 09:04
    3. Start with WS2812
      By basparky in forum Proton Plus Compiler v3
      Replies: 2
      Last Post: 2nd September 2015, 09:21
    4. Help: Les's RGB Dimmer code.
      By CharlieBrown in forum Proton Plus Compiler v3
      Replies: 2
      Last Post: 9th July 2009, 18:47
    5. Les's MANCHESTER code
      By mikedownunder in forum Proton Plus Compiler v3
      Replies: 3
      Last Post: 14th May 2006, 13:01

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts