Robb Walters / Mbed 2 deprecated TB62D612FTG

Dependencies:   mbed

Revision:
0:e30668e5984b
Child:
1:2fae6f119cf4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jul 29 01:38:22 2015 +0000
@@ -0,0 +1,166 @@
+
+#include "mbed.h"
+
+// TOSHIBA TB62D612FTG
+
+#define START_BYTE          0xFF
+#define SLAVE_ADDR_ID       0x06 //ID2 = GND ; ID1 = GND ; ID0 = Vcc
+#define ALL_ADDRESSES       0x01 
+
+//SUB-ADDRESS = (BASE + COLOR) << 1
+#define CH0 0x01
+#define CH1 0x04
+#define CH2 0x07
+#define CH3 0x0A
+#define CH4 0x0D
+#define CH5 0x10
+#define CH6 0x13
+#define CH7 0x16
+
+//RED and GRN addresses are switched for layout purposes on our board.
+#define RED     0x00
+#define GRN     0x01   
+#define BLU     0x02
+
+#define ALL_CHANNEL_SELECT  0x40
+#define SPECIAL_MODE        0x60 //special mode programs all the channels sequentially (send serial sub, then 24 bytes of data)
+
+//DATA BYTES
+// 0x00 to 0x7F control brightness (always shift left by one)
+#define OFF                 0x00
+
+//PERIOD COMMAND
+#define PERIOD              0x81
+
+const char * byte_to_binary(uint8_t x) {
+    static char b[9];
+    b[0] = '\0';
+    uint8_t z;
+    for (z = 128; z > 0; z >>= 1) {
+        strcat(b, ((x & z) == z) ? "1" : "0");
+    }
+    return b;
+}
+
+Serial pc(USBTX, USBRX);
+SPI spi(p11, p12, p13); // mosi, miso, sclk
+
+void clearAll(void) {
+    spi.write(START_BYTE);
+    spi.write(SLAVE_ADDR_ID);
+    spi.write(ALL_CHANNEL_SELECT);
+    spi.write(OFF);
+    spi.write(PERIOD);
+}
+
+void blinkSpecial() {
+    // level must be from (0 to 127) <<1
+    uint8_t level = 0x01;
+    
+    char bytes[28] = {START_BYTE, SLAVE_ADDR_ID, SPECIAL_MODE, 
+                      0x00, 0x00, 0x00, //CH0 --> R/G/B @ 3,4,5
+                      0x00, 0x00, 0x00, //CH1
+                      0x00, 0x00, 0x00, //CH2
+                      0x00, 0x00, 0x00, //CH3
+                      0x00, 0x00, 0x00, //CH4
+                      0x00, 0x00, 0x00, //CH5
+                      0x00, 0x00, 0x00, //CH6
+                      0x00, 0x00, 0x00, //CH7
+                      PERIOD};
+    
+    wait_ms(50);
+
+    bytes[3]  = level<<1; bytes[4]  = 0x00<<1;
+    bytes[6]  = level<<1; bytes[7]  = 0x00<<1;
+    bytes[9]  = level<<1; bytes[10] = 0x00<<1;
+    bytes[12] = level<<1; bytes[13] = 0x00<<1;
+    bytes[15] = level<<1; bytes[16] = 0x00<<1;
+    bytes[18] = level<<1; bytes[19] = 0x00<<1;
+    bytes[21] = level<<1; bytes[22] = 0x00<<1;
+    bytes[24] = level<<1; bytes[25] = 0x00<<1;  
+
+    for(int repeats=4; repeats>0; repeats--){    
+        for(int i=0; i<28; i++){
+            spi.write(bytes[i]);
+        }
+    }
+
+    wait_ms(50);
+    
+    bytes[3]  = 0x00<<1; bytes[4]  = level<<1;
+    bytes[6]  = 0x00<<1; bytes[7]  = level<<1;
+    bytes[9]  = 0x00<<1; bytes[10] = level<<1;
+    bytes[12] = 0x00<<1; bytes[13] = level<<1;
+    bytes[15] = 0x00<<1; bytes[16] = level<<1;
+    bytes[18] = 0x00<<1; bytes[19] = level<<1;
+    bytes[21] = 0x00<<1; bytes[22] = level<<1;
+    bytes[24] = 0x00<<1; bytes[25] = level<<1;  
+
+    for(int repeats=4; repeats>0; repeats--){    
+        for(int i=0; i<28; i++){
+            spi.write(bytes[i]);
+        }
+    }
+
+}
+
+void setLevel(uint8_t led_address, uint8_t level) {
+    // level must be from (0 to 127) <<1
+    char bytes[5] = {START_BYTE, SLAVE_ADDR_ID, led_address, level, PERIOD};
+    for(int i=0; i<5; i++){
+        spi.write(bytes[i]);
+    }
+    //wait_ms(4); //wait for device ready (datasheet 9.7)
+}
+
+void blink(uint8_t led_address) {
+    wait_ms(250);
+    setLevel(led_address, (0x10)<<1);
+    wait_ms(250);
+    setLevel(led_address, (0x00)<<1);
+}
+
+void blinkAll(void) {
+    wait_ms(250);
+    setLevel(ALL_CHANNEL_SELECT, (0x10)<<1);
+    wait_ms(250);
+    setLevel(ALL_CHANNEL_SELECT, (0x00)<<1);
+}
+
+
+int main() {
+    
+    // Setup the spi for 8 bit data, high steady state clock,
+    // second edge capture, with a 1MHz clock rate
+    spi.format(8,3);
+    spi.frequency(8000000);
+    
+    pc.baud(57600);
+    pc.printf("blinky!\n\r");
+
+    //clearAllChannels();
+    
+    uint8_t channels[8] = {CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7};
+    uint8_t colors[3] = {RED, GRN, BLU};
+
+    uint8_t level = 0;
+
+    while(1){
+        for(int channel_index=0; channel_index<8; channel_index++){
+            uint8_t channel = channels[channel_index];
+    
+            for(int color_index=0; color_index<3; color_index++){
+                uint8_t color = colors[color_index];
+                                
+                pc.printf("blinking subaddress %s\n\r", byte_to_binary((channel + color)<<1));
+                //blink((channel + color)<<1);
+                //blink((0x01)<<1);
+                //blinkAll();
+                blinkSpecial();
+                
+                //setLevel((channel + color)<<1, level<<1);
+            }
+        }
+        if (level > 4) { level = 0; } else { level++; }
+    }
+}