see: http://mbed.org/users/okini3939/notebook/led_strip/

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Mon Jul 08 01:36:53 2013 +0000
Commit message:
1st build

Changed in this revision

LEDTape.h Show annotated file Show diff for this revision Revisions of this file
LEDTape_HL1606.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 6fc9fe87e0c5 LEDTape.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LEDTape.h	Mon Jul 08 01:36:53 2013 +0000
@@ -0,0 +1,8 @@
+/*
+ * HL1606 tape led IC
+ */
+ 
+void tape_write (int d);
+void tapeInit (int speed, int n);
+void tapeSet (int n, int dat);
+void tapeSend ();
diff -r 000000000000 -r 6fc9fe87e0c5 LEDTape_HL1606.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LEDTape_HL1606.cpp	Mon Jul 08 01:36:53 2013 +0000
@@ -0,0 +1,124 @@
+/*
+ * HL1606 tape led IC
+ */
+#include "mbed.h"
+#include "LEDTape.h"
+
+#define PWM_STEP 64
+
+SPI tape(p11, p12, p13);
+DigitalOut latch(p14);
+
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    LPC_SSP_TypeDef *_ssp = LPC_SSP0;
+#elif defined(TARGET_LPC11U24)
+    LPC_SSPx_Type *_ssp = LPC_SSP1;
+#endif
+
+int num = 160;
+int *data;
+
+
+//#define tape_write(d) tape.write(d)
+#ifndef tape_write
+void tape_write (int d) {
+    while (! (_ssp->SR & (1<<1))); // TNF
+    _ssp->DR = d;
+}
+#endif
+
+extern "C"
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+void SSP0_IRQHandler() {
+#elif defined(TARGET_LPC11U24)
+void SSP1_IRQHandler() {
+#endif
+    static int addr = num - 1;
+    static int pwm = 0;
+    int d, dr, r, g, b;
+repeat:
+    // led data
+    while (_ssp->SR & (1<<1)) { // TNF
+        d = data[addr];
+        r = (d >> 16) & 0xff;
+        g = (d >> 8) & 0xff;
+        b = d & 0xff;
+
+        dr = 0x80;
+#ifdef PWM_STEP
+        if (r > pwm || r == 0xff) {
+            dr |= 0x04;
+        }
+        if (g > pwm || g == 0xff) {
+            dr |= 0x10;
+        }
+        if (b > pwm || b == 0xff) {
+            dr |= 0x01;
+        }
+#else
+        if (r) {
+            dr |= 0x04;
+        }
+        if (g) {
+            dr |= 0x10;
+        }
+        if (b) {
+            dr |= 0x01;
+        }
+#endif
+        _ssp->DR = dr;
+
+        addr --;
+        if (addr < 0) {
+            addr = num - 1;
+#ifdef PWM_STEP
+            pwm += PWM_STEP;
+            if (pwm > 255) {
+                pwm = 0;
+            }
+#endif
+            while ((_ssp->SR & ((1<<4)|(1<<0))) != (1<<0)); // BSY, TFE
+            latch = 1;
+            wait_us(10);
+            latch = 0;
+            goto repeat;
+        }
+    }
+}
+
+void tapeInit (int freq, int n) {
+
+    num = n;
+    data = new int(num);
+    for (int i = 0; i < num; i ++) {
+        data[i] = 0;
+    }
+
+    tape.format(8, 0);
+    latch = 0;
+    if (freq) {
+        tape.frequency(freq * 1000);
+    } else {
+        // 450KHz, higher speed = draw errors
+        tape.frequency(450000);
+    }
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    NVIC_SetVector(SSP0_IRQn, (uint32_t)SSP0_IRQHandler);
+    NVIC_SetPriority(SSP0_IRQn, 0);
+    NVIC_EnableIRQ(SSP0_IRQn);
+#elif defined(TARGET_LPC11U24)
+    NVIC_SetVector(SSP1_IRQn, (uint32_t)SSP1_IRQHandler);
+    NVIC_SetPriority(SSP1_IRQn, 0);
+    NVIC_EnableIRQ(SSP1_IRQn);
+#endif
+    _ssp->IMSC |= (1<<3); // TXIM
+}
+
+void tapeSet (int n, int dat) {
+    if (n < num) {
+        data[n] = dat;
+    }
+}
+
+void tapeSend () {
+}
diff -r 000000000000 -r 6fc9fe87e0c5 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Jul 08 01:36:53 2013 +0000
@@ -0,0 +1,44 @@
+/*
+ * HL1606 tape led IC
+ */
+#include "mbed.h"
+#include "LEDTape.h"
+
+Serial pc(USBTX, USBRX);
+DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
+
+extern "C"
+void HardFault_Handler() {
+    printf("Hard Fault!\r\n");
+    exit(-1);
+}
+
+int main() {
+    int i, j, k = 0;
+
+    pc.baud(115200);
+    tapeInit(0, 16);
+    led1 = 1;
+
+    pc.printf("led\r\n");
+
+    for (;;) {
+        for (i = 0; i < 256; i ++) {
+            led2 = 1;
+            tapeSet(0, (i << 16));
+            tapeSet(2, (i << 8));
+            tapeSet(4, i);
+            tapeSet(6, (i << 8)|i);
+            tapeSet(7, (i << 16)|i);
+            tapeSet(8, (i << 16)|(i << 8));
+            tapeSet(9, (i << 16)|(i << 8)|i);
+            tapeSend();
+            led2 = 0;
+            wait_ms(20);
+        }
+        for (i = 0; i < 160; i ++) {
+            tapeSet(i, 0);
+        }
+        tapeSend();
+    }
+}
diff -r 000000000000 -r 6fc9fe87e0c5 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Jul 08 01:36:53 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/3d0ef94e36ec
\ No newline at end of file