Simplistic code to test a couple WS2803 in series. WS2803 are 18 channel PWM constant current sources (sinks) normally used to drive 18 LEDs or 6 RGB LEDs.
main.cpp@0:0bf629429936, 2013-02-14 (annotated)
- Committer:
- tolson
- Date:
- Thu Feb 14 04:59:29 2013 +0000
- Revision:
- 0:0bf629429936
Simplistic code to test WS2803 18 channel PWM constant current source
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tolson | 0:0bf629429936 | 1 | // WS2803_Testing_MBED |
tolson | 0:0bf629429936 | 2 | // By Thomas Olson |
tolson | 0:0bf629429936 | 3 | // teo20130210.01 |
tolson | 0:0bf629429936 | 4 | // MBED LPC1768 |
tolson | 0:0bf629429936 | 5 | // WS2803 pin4 CKI -> ws2803_clockPin(p21) |
tolson | 0:0bf629429936 | 6 | // WS2803 pin5 SDI -> ws2803_dataPin(p22) |
tolson | 0:0bf629429936 | 7 | |
tolson | 0:0bf629429936 | 8 | |
tolson | 0:0bf629429936 | 9 | #include "mbed.h" |
tolson | 0:0bf629429936 | 10 | |
tolson | 0:0bf629429936 | 11 | DigitalOut ws2803_clockPin(p21); |
tolson | 0:0bf629429936 | 12 | DigitalOut ws2803_dataPin(p22); |
tolson | 0:0bf629429936 | 13 | |
tolson | 0:0bf629429936 | 14 | #define nLEDs 18 |
tolson | 0:0bf629429936 | 15 | #define nBARs 2 // Number of WS2803 chained together |
tolson | 0:0bf629429936 | 16 | |
tolson | 0:0bf629429936 | 17 | uint8_t ledBar[nBARs][nLEDs]; |
tolson | 0:0bf629429936 | 18 | |
tolson | 0:0bf629429936 | 19 | void shiftOut(uint8_t sodata) { |
tolson | 0:0bf629429936 | 20 | for(int i = 7; i >= 0; i--) { |
tolson | 0:0bf629429936 | 21 | ws2803_clockPin = 0; |
tolson | 0:0bf629429936 | 22 | if(sodata & (0x01 << i)){ |
tolson | 0:0bf629429936 | 23 | ws2803_dataPin = 1; |
tolson | 0:0bf629429936 | 24 | } else { |
tolson | 0:0bf629429936 | 25 | ws2803_dataPin = 0; |
tolson | 0:0bf629429936 | 26 | } |
tolson | 0:0bf629429936 | 27 | wait_us(1); |
tolson | 0:0bf629429936 | 28 | ws2803_clockPin = 1; |
tolson | 0:0bf629429936 | 29 | wait_us(1); |
tolson | 0:0bf629429936 | 30 | ws2803_clockPin = 0; |
tolson | 0:0bf629429936 | 31 | } |
tolson | 0:0bf629429936 | 32 | } |
tolson | 0:0bf629429936 | 33 | |
tolson | 0:0bf629429936 | 34 | void loadWS2803(){ |
tolson | 0:0bf629429936 | 35 | for(int wsBar = 0; wsBar < nBARs; wsBar++){ |
tolson | 0:0bf629429936 | 36 | for (int wsOut = 0; wsOut < nLEDs; wsOut++){ |
tolson | 0:0bf629429936 | 37 | shiftOut(ledBar[nBARs-(wsBar+1)][wsOut]); |
tolson | 0:0bf629429936 | 38 | } |
tolson | 0:0bf629429936 | 39 | } |
tolson | 0:0bf629429936 | 40 | ws2803_clockPin = 0; |
tolson | 0:0bf629429936 | 41 | wait_us(600); // 500us-600us needed to reset WS2803s |
tolson | 0:0bf629429936 | 42 | } |
tolson | 0:0bf629429936 | 43 | |
tolson | 0:0bf629429936 | 44 | void clearWS2803(){ |
tolson | 0:0bf629429936 | 45 | for(int wsOut = 0; wsOut < nLEDs; wsOut++){ |
tolson | 0:0bf629429936 | 46 | ledBar[0][wsOut] = ledBar[1][wsOut] = 0x00; |
tolson | 0:0bf629429936 | 47 | loadWS2803(); |
tolson | 0:0bf629429936 | 48 | } |
tolson | 0:0bf629429936 | 49 | } |
tolson | 0:0bf629429936 | 50 | |
tolson | 0:0bf629429936 | 51 | int main() { |
tolson | 0:0bf629429936 | 52 | // initialize WS2803s |
tolson | 0:0bf629429936 | 53 | ws2803_clockPin = 0; |
tolson | 0:0bf629429936 | 54 | wait_us(600); |
tolson | 0:0bf629429936 | 55 | |
tolson | 0:0bf629429936 | 56 | while(1) { |
tolson | 0:0bf629429936 | 57 | clearWS2803(); |
tolson | 0:0bf629429936 | 58 | wait(2.0); |
tolson | 0:0bf629429936 | 59 | // Chase mode |
tolson | 0:0bf629429936 | 60 | for(int iOut=7; iOut < 256; iOut*=2){ |
tolson | 0:0bf629429936 | 61 | for(int wsOut = 0; wsOut < nLEDs; wsOut++){ |
tolson | 0:0bf629429936 | 62 | ledBar[0][wsOut] = ledBar[1][wsOut] = (uint8_t)(0xFF & iOut); |
tolson | 0:0bf629429936 | 63 | loadWS2803(); |
tolson | 0:0bf629429936 | 64 | clearWS2803(); |
tolson | 0:0bf629429936 | 65 | } |
tolson | 0:0bf629429936 | 66 | } |
tolson | 0:0bf629429936 | 67 | |
tolson | 0:0bf629429936 | 68 | wait(2.0); |
tolson | 0:0bf629429936 | 69 | //parallel mode |
tolson | 0:0bf629429936 | 70 | for(int iOut = 7; iOut < 64; iOut*=4){ |
tolson | 0:0bf629429936 | 71 | for(int wsOut = 0; wsOut < nLEDs; wsOut++){ |
tolson | 0:0bf629429936 | 72 | ledBar[0][wsOut] = ledBar[1][wsOut] = (uint8_t)(0xFF & iOut); |
tolson | 0:0bf629429936 | 73 | loadWS2803(); |
tolson | 0:0bf629429936 | 74 | wait_us(1000000); |
tolson | 0:0bf629429936 | 75 | } |
tolson | 0:0bf629429936 | 76 | wait_us(10000); |
tolson | 0:0bf629429936 | 77 | } |
tolson | 0:0bf629429936 | 78 | }// while(1) |
tolson | 0:0bf629429936 | 79 | } |