Robb Walters / Mbed 2 deprecated TB62D612FTG

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 
00004 // TOSHIBA TB62D612FTG
00005 
00006 #define START_BYTE          0xFF
00007 #define SLAVE_ADDR_ID       0x06 //ID2 = GND ; ID1 = GND ; ID0 = Vcc
00008 #define ALL_ADDRESSES       0x01 
00009 
00010 //SUB-ADDRESS = (BASE + COLOR) << 1
00011 #define CH0 0x01
00012 #define CH1 0x04
00013 #define CH2 0x07
00014 #define CH3 0x0A
00015 #define CH4 0x0D
00016 #define CH5 0x10
00017 #define CH6 0x13
00018 #define CH7 0x16
00019 
00020 //RED and GRN addresses are switched for layout purposes on our board.
00021 #define RED     0x00
00022 #define GRN     0x01   
00023 #define BLU     0x02
00024 
00025 #define ALL_CHANNEL_SELECT  0x40
00026 #define SPECIAL_MODE        0x60 //special mode programs all the channels sequentially (send serial sub, then 24 bytes of data)
00027 
00028 //DATA BYTES
00029 // 0x00 to 0x7F control brightness (always shift left by one)
00030 #define OFF                 0x00
00031 
00032 //PERIOD COMMAND
00033 #define PERIOD              0x81
00034 
00035 const char * byte_to_binary(uint8_t x) {
00036     static char b[9];
00037     b[0] = '\0';
00038     uint8_t z;
00039     for (z = 128; z > 0; z >>= 1) {
00040         strcat(b, ((x & z) == z) ? "1" : "0");
00041     }
00042     return b;
00043 }
00044 
00045 Serial pc(USBTX, USBRX);
00046 SPI spi(p11, p12, p13); // mosi, miso, sclk
00047 
00048 void clearAll(void) {
00049     spi.write(START_BYTE);
00050     spi.write(SLAVE_ADDR_ID);
00051     spi.write(ALL_CHANNEL_SELECT);
00052     spi.write(OFF);
00053     spi.write(PERIOD);
00054 }
00055 
00056 void blinkSpecial() {
00057     // level must be from (0 to 127) <<1
00058     const int repeats = 1;
00059     
00060     uint8_t level = 0x01;
00061     
00062     char bytes[28] = {START_BYTE, SLAVE_ADDR_ID, SPECIAL_MODE, 
00063                       0x00, 0x00, 0x00, //CH0 --> R/G/B @ 3,4,5
00064                       0x00, 0x00, 0x00, //CH1
00065                       0x00, 0x00, 0x00, //CH2
00066                       0x00, 0x00, 0x00, //CH3
00067                       0x00, 0x00, 0x00, //CH4
00068                       0x00, 0x00, 0x00, //CH5
00069                       0x00, 0x00, 0x00, //CH6
00070                       0x00, 0x00, 0x00, //CH7
00071                       PERIOD};
00072     
00073     wait_ms(50);
00074 
00075     bytes[3]  = level<<1; bytes[4]  = 0x00<<1;
00076     bytes[6]  = level<<1; bytes[7]  = 0x00<<1;
00077     bytes[9]  = level<<1; bytes[10] = 0x00<<1;
00078     bytes[12] = level<<1; bytes[13] = 0x00<<1;
00079     bytes[15] = level<<1; bytes[16] = 0x00<<1;
00080     bytes[18] = level<<1; bytes[19] = 0x00<<1;
00081     bytes[21] = level<<1; bytes[22] = 0x00<<1;
00082     bytes[24] = level<<1; bytes[25] = 0x00<<1;  
00083 
00084     for(int r=repeats; r>0; r--){    
00085         for(int i=0; i<28; i++){
00086             spi.write(bytes[i]);
00087         }
00088     }
00089 
00090     wait_ms(50);
00091     
00092     bytes[3]  = 0x00<<1; bytes[4]  = level<<1;
00093     bytes[6]  = 0x00<<1; bytes[7]  = level<<1;
00094     bytes[9]  = 0x00<<1; bytes[10] = level<<1;
00095     bytes[12] = 0x00<<1; bytes[13] = level<<1;
00096     bytes[15] = 0x00<<1; bytes[16] = level<<1;
00097     bytes[18] = 0x00<<1; bytes[19] = level<<1;
00098     bytes[21] = 0x00<<1; bytes[22] = level<<1;
00099     bytes[24] = 0x00<<1; bytes[25] = level<<1;  
00100 
00101     for(int r=repeats; r>0; r--){    
00102         for(int i=0; i<28; i++){
00103             spi.write(bytes[i]);
00104         }
00105     }
00106 
00107 }
00108 
00109 void setLevel(uint8_t led_address, uint8_t level) {
00110     // level must be from (0 to 127) <<1
00111     char bytes[5] = {START_BYTE, SLAVE_ADDR_ID, led_address, level, PERIOD};
00112     for(int i=0; i<5; i++){
00113         spi.write(bytes[i]);
00114     }
00115     //wait_ms(4); //wait for device ready (datasheet 9.7)
00116 }
00117 
00118 void fastblink(uint8_t led_address) {
00119     wait_ms(3);
00120     setLevel(led_address, (0x10)<<1);
00121     wait_ms(3);
00122     setLevel(led_address, (0x00)<<1);
00123 }
00124 
00125 void blinkAll(uint8_t level) {
00126     wait_ms(500);
00127     setLevel(ALL_CHANNEL_SELECT, (level)<<1);
00128     //wait(1/30.0);
00129     wait(1);
00130     setLevel(ALL_CHANNEL_SELECT, (0x00)<<1);
00131 }
00132 
00133 
00134 int main() {
00135     
00136     // Setup the spi for 8 bit data, high steady state clock,
00137     // second edge capture, with a 1MHz clock rate
00138     spi.format(8,1);
00139     spi.frequency(8000000);
00140     
00141     pc.baud(57600);
00142     pc.printf("blinky!\n\r");
00143 
00144     //clearAllChannels();
00145     
00146     uint8_t channels[8] = {CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7};
00147     uint8_t colors[3] = {RED, GRN, BLU};
00148 
00149     uint8_t level = 0;
00150 
00151     while(1){
00152         for(int channel_index=0; channel_index<8; channel_index++){
00153             uint8_t channel = channels[channel_index];
00154     
00155             for(int color_index=0; color_index<3; color_index++){
00156                 uint8_t color = colors[color_index];
00157                                 
00158                 //pc.printf("blinking subaddress %s\n\r", byte_to_binary((channel + color)<<1));
00159                 fastblink((channel + color)<<1);
00160                 
00161                 //blink((0x01)<<1);
00162                 //blinkAll(64);
00163                 //blinkSpecial();
00164                 //setLevel(ALL_CHANNEL_SELECT, (64)<<1);
00165                 //setLevel((channel + color)<<1, level<<1);
00166             }
00167         }
00168         if (level > 4) { level = 0; } else { level++; }
00169     }
00170 }