Xmas tree serial LED blinking example code
Fork of JBB_SerialLED_test by
WS2812B/LEDStrip_WS2812.cpp@0:57df396a2de8, 2014-05-12 (annotated)
- Committer:
- jksoft
- Date:
- Mon May 12 11:01:44 2014 +0000
- Revision:
- 0:57df396a2de8
- Child:
- 1:832ea8c566a8
First edition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:57df396a2de8 | 1 | /* |
jksoft | 0:57df396a2de8 | 2 | * WS2812 tape led IC |
jksoft | 0:57df396a2de8 | 3 | * |
jksoft | 0:57df396a2de8 | 4 | * 0.35us 0.8us (+-150ns) |
jksoft | 0:57df396a2de8 | 5 | * 0: |^^^^^|__________| |
jksoft | 0:57df396a2de8 | 6 | * |
jksoft | 0:57df396a2de8 | 7 | * 0.7us 0.6us (+-150ns) |
jksoft | 0:57df396a2de8 | 8 | * 1: |^^^^^^^^^^|_____| |
jksoft | 0:57df396a2de8 | 9 | * |
jksoft | 0:57df396a2de8 | 10 | * >50us |
jksoft | 0:57df396a2de8 | 11 | * RESET: |________________| |
jksoft | 0:57df396a2de8 | 12 | */ |
jksoft | 0:57df396a2de8 | 13 | #include "mbed.h" |
jksoft | 0:57df396a2de8 | 14 | #include "LEDStrip.h" |
jksoft | 0:57df396a2de8 | 15 | |
jksoft | 0:57df396a2de8 | 16 | SPI tape(p11, NC, NC); |
jksoft | 0:57df396a2de8 | 17 | |
jksoft | 0:57df396a2de8 | 18 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
jksoft | 0:57df396a2de8 | 19 | LPC_SSP_TypeDef *_ssp = LPC_SSP0; |
jksoft | 0:57df396a2de8 | 20 | #elif defined(TARGET_LPC11U24) |
jksoft | 0:57df396a2de8 | 21 | LPC_SSPx_Type *_ssp = LPC_SSP1; |
jksoft | 0:57df396a2de8 | 22 | #endif |
jksoft | 0:57df396a2de8 | 23 | |
jksoft | 0:57df396a2de8 | 24 | int num = 100; |
jksoft | 0:57df396a2de8 | 25 | int *data; |
jksoft | 0:57df396a2de8 | 26 | volatile int busy = 0, wakeup = 0; |
jksoft | 0:57df396a2de8 | 27 | |
jksoft | 0:57df396a2de8 | 28 | |
jksoft | 0:57df396a2de8 | 29 | extern "C" |
jksoft | 0:57df396a2de8 | 30 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
jksoft | 0:57df396a2de8 | 31 | void SSP0_IRQHandler() { |
jksoft | 0:57df396a2de8 | 32 | #elif defined(TARGET_LPC11U24) |
jksoft | 0:57df396a2de8 | 33 | void SSP1_IRQHandler() { |
jksoft | 0:57df396a2de8 | 34 | #endif |
jksoft | 0:57df396a2de8 | 35 | static int addr = 0, bit = 0x800000; |
jksoft | 0:57df396a2de8 | 36 | repeat: |
jksoft | 0:57df396a2de8 | 37 | if (busy) { |
jksoft | 0:57df396a2de8 | 38 | // led data |
jksoft | 0:57df396a2de8 | 39 | while (_ssp->SR & (1<<1)) { // TNF |
jksoft | 0:57df396a2de8 | 40 | if (data[addr] & bit) { |
jksoft | 0:57df396a2de8 | 41 | // 1 |
jksoft | 0:57df396a2de8 | 42 | //_ssp->DR = 0x01f; |
jksoft | 0:57df396a2de8 | 43 | _ssp->DR = 0x007; |
jksoft | 0:57df396a2de8 | 44 | } else { |
jksoft | 0:57df396a2de8 | 45 | // 0 |
jksoft | 0:57df396a2de8 | 46 | _ssp->DR = 0x07f; |
jksoft | 0:57df396a2de8 | 47 | } |
jksoft | 0:57df396a2de8 | 48 | bit = bit >> 1; |
jksoft | 0:57df396a2de8 | 49 | if (bit == 0) { |
jksoft | 0:57df396a2de8 | 50 | bit = 0x800000; |
jksoft | 0:57df396a2de8 | 51 | addr ++; |
jksoft | 0:57df396a2de8 | 52 | if (addr >= num) { |
jksoft | 0:57df396a2de8 | 53 | addr = 0; |
jksoft | 0:57df396a2de8 | 54 | busy = 0; |
jksoft | 0:57df396a2de8 | 55 | goto repeat; |
jksoft | 0:57df396a2de8 | 56 | } |
jksoft | 0:57df396a2de8 | 57 | } |
jksoft | 0:57df396a2de8 | 58 | } |
jksoft | 0:57df396a2de8 | 59 | } else { |
jksoft | 0:57df396a2de8 | 60 | // blank |
jksoft | 0:57df396a2de8 | 61 | while (_ssp->SR & (1<<1)) { // TNF |
jksoft | 0:57df396a2de8 | 62 | _ssp->DR = 0xfff; |
jksoft | 0:57df396a2de8 | 63 | if (addr < 50) { |
jksoft | 0:57df396a2de8 | 64 | addr ++; |
jksoft | 0:57df396a2de8 | 65 | } else { |
jksoft | 0:57df396a2de8 | 66 | addr = 0; |
jksoft | 0:57df396a2de8 | 67 | if (wakeup) { |
jksoft | 0:57df396a2de8 | 68 | busy = 1; |
jksoft | 0:57df396a2de8 | 69 | wakeup = 0; |
jksoft | 0:57df396a2de8 | 70 | goto repeat; |
jksoft | 0:57df396a2de8 | 71 | } |
jksoft | 0:57df396a2de8 | 72 | } |
jksoft | 0:57df396a2de8 | 73 | } |
jksoft | 0:57df396a2de8 | 74 | } |
jksoft | 0:57df396a2de8 | 75 | } |
jksoft | 0:57df396a2de8 | 76 | |
jksoft | 0:57df396a2de8 | 77 | void tapeInit (int freq, int n) { |
jksoft | 0:57df396a2de8 | 78 | |
jksoft | 0:57df396a2de8 | 79 | num = n; |
jksoft | 0:57df396a2de8 | 80 | // data = new int(num); |
jksoft | 0:57df396a2de8 | 81 | data = (int*)malloc(sizeof(int) * num); |
jksoft | 0:57df396a2de8 | 82 | for (int i = 0; i < num; i ++) { |
jksoft | 0:57df396a2de8 | 83 | data[i] = 0; |
jksoft | 0:57df396a2de8 | 84 | } |
jksoft | 0:57df396a2de8 | 85 | |
jksoft | 0:57df396a2de8 | 86 | tape.format(10, 1); |
jksoft | 0:57df396a2de8 | 87 | if (freq) { |
jksoft | 0:57df396a2de8 | 88 | tape.frequency(freq * 1000); |
jksoft | 0:57df396a2de8 | 89 | } else { |
jksoft | 0:57df396a2de8 | 90 | tape.frequency(8000000); |
jksoft | 0:57df396a2de8 | 91 | } |
jksoft | 0:57df396a2de8 | 92 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
jksoft | 0:57df396a2de8 | 93 | NVIC_SetVector(SSP0_IRQn, (uint32_t)SSP0_IRQHandler); |
jksoft | 0:57df396a2de8 | 94 | NVIC_SetPriority(SSP0_IRQn, 0); |
jksoft | 0:57df396a2de8 | 95 | NVIC_EnableIRQ(SSP0_IRQn); |
jksoft | 0:57df396a2de8 | 96 | #elif defined(TARGET_LPC11U24) |
jksoft | 0:57df396a2de8 | 97 | NVIC_SetVector(SSP1_IRQn, (uint32_t)SSP1_IRQHandler); |
jksoft | 0:57df396a2de8 | 98 | NVIC_SetPriority(SSP1_IRQn, 0); |
jksoft | 0:57df396a2de8 | 99 | NVIC_EnableIRQ(SSP1_IRQn); |
jksoft | 0:57df396a2de8 | 100 | #endif |
jksoft | 0:57df396a2de8 | 101 | _ssp->IMSC |= (1<<3); // TXIM |
jksoft | 0:57df396a2de8 | 102 | } |
jksoft | 0:57df396a2de8 | 103 | |
jksoft | 0:57df396a2de8 | 104 | void tapeSet (int n, int dat) { |
jksoft | 0:57df396a2de8 | 105 | if (n >= 0 && n < num) { |
jksoft | 0:57df396a2de8 | 106 | // RGB -> GRB |
jksoft | 0:57df396a2de8 | 107 | data[n] = ((dat & 0xff0000) >> 8) | ((dat & 0xff00) << 8) | (dat & 0xff); |
jksoft | 0:57df396a2de8 | 108 | } |
jksoft | 0:57df396a2de8 | 109 | } |
jksoft | 0:57df396a2de8 | 110 | void tapeSet(int n, int red , int green , int blue) { |
jksoft | 0:57df396a2de8 | 111 | if (n >= 0 && n < num) { |
jksoft | 0:57df396a2de8 | 112 | // RGB -> GRB |
jksoft | 0:57df396a2de8 | 113 | data[n] = ((red & 0xff) << 8) | ((green & 0xff) << 16) | (blue & 0xff); |
jksoft | 0:57df396a2de8 | 114 | } |
jksoft | 0:57df396a2de8 | 115 | } |
jksoft | 0:57df396a2de8 | 116 | |
jksoft | 0:57df396a2de8 | 117 | void tapeSend () { |
jksoft | 0:57df396a2de8 | 118 | if (busy) { |
jksoft | 0:57df396a2de8 | 119 | while (busy); |
jksoft | 0:57df396a2de8 | 120 | wait_us(50); |
jksoft | 0:57df396a2de8 | 121 | } |
jksoft | 0:57df396a2de8 | 122 | wakeup = 1; |
jksoft | 0:57df396a2de8 | 123 | while (wakeup); |
jksoft | 0:57df396a2de8 | 124 | } |
jksoft | 0:57df396a2de8 | 125 | |
jksoft | 0:57df396a2de8 | 126 | int tapeGet (int n) { |
jksoft | 0:57df396a2de8 | 127 | return ((data[n] & 0xff0000) >> 8) | ((data[n] & 0xff00) << 8) | (data[n] & 0xff); |
jksoft | 0:57df396a2de8 | 128 | } |