WS2801 – 3 Channel PWM

The WS2801 is a 3-channel constant current LED drivers that is designed for controlling chains of RGB LEDs. The IC is controlled by a 2-wire serial control scheme that allows multiple WS2801 to be chained together. You can find the WS2801 in the link below. This demo code basically shifts out 24 bits (8bits per color) using a 2-wire protocol. This code only works for one WS2801. There is a object designed for multiple WS2801 already in the propeller OBEX. That design uses too many resources for use on my pinball machine so I wrote this to use as little resources as possible (no new cog).

The idea would be to have the WS2801 PWM a set of mosfets which would in turn be connected to the string of LEDs. This way you could light up a bunch of RGB LEDs without allot of CPU time taken from the Propeller. The Propeller only has to mess with the WS2801 when it needs to change the PWM signals.

Where to buy the WS2801.
Demo Driver


'WS2801_Driver.spin
'Version: 1.0
'Parker Dillmann
'The Longhorn Engineer (c) 2013 
'www.longhornengineer.com

'See end of file for License.

'WS2801 PWM Driver

'This driver is designed to control 1 WS2801 chip
'Usually setup the WS2801 so that it drives 3 mosfets which can control any number of RGB LEDs
'The purpose of this driver is to minimize cog time on the propeller and not waste a cog doing PWM work

'Call Init in your main program to setup the driver.
'Pass the CLK pin as an arguement.
'The driver assumes that the data line will be the next pin. 
'
CON
    
  _CLKMODE      = XTAL1 + PLL16X                        
  _XINFREQ      = 5_000_000

  Pin_Low  = 0
  Pin_High = 1

VAR

  byte CLK                

PUB Init(CLK_T)

  CLK := CLK_T

  DIRA[CLK] ~~
  DIRA[CLK+1] ~~

  OUTA[CLK] := Pin_Low
  OUTA[CLK+1] := Pin_Low
  
return

PUB Color(RED,GREEN,BLUE)

  repeat 8
    OUTA[CLK] := Pin_Low
    OUTA[CLK+1] := RED
    OUTA[CLK] := Pin_High
    RED := RED >> 1

  repeat 8
    OUTA[CLK] := Pin_Low
    OUTA[CLK+1] := GREEN
    OUTA[CLK] := Pin_High
    GREEN := GREEN >> 1

  repeat 8
    OUTA[CLK] := Pin_Low
    OUTA[CLK+1] := BLUE
    OUTA[CLK] := Pin_High
    BLUE := BLUE >> 1

  OUTA[CLK] := Pin_Low

return

DAT
{{
┌──────────────────────────────────────────────────────────────────────────────────────┐
│                           TERMS OF USE: MIT License                                  │
├──────────────────────────────────────────────────────────────────────────────────────┤
│Permission is hereby granted, free of charge, to any person obtaining a copy of this  │
│software and associated documentation files (the "Software"), to deal in the Software │
│without restriction, including without limitation the rights to use, copy, modify,    │
│merge, publish, distribute, sublicense, and/or sell copies of the Software, and to    │
│permit persons to whom the Software is furnished to do so, subject to the following   │
│conditions:                                                                           │
│                                                                                      │
│The above copyright notice and this permission notice shall be included in all copies │
│or substantial portions of the Software.                                              │
│                                                                                      │
│THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,   │
│INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A         │
│PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT    │
│HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION     │
│OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE        │
│SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                │
└──────────────────────────────────────────────────────────────────────────────────────┘
}}

One thought on “WS2801 – 3 Channel PWM

  1. Pingback: Propeller: WS2801 – 3 Channel PWM | The Longhorn Engineer