dot Matrix Led Clock with TLC5940

Dependencies:   Kc_TLC5940 mbed

Committer:
kohacraft
Date:
Tue Jul 28 01:53:27 2015 +0000
Revision:
2:6edd801ae662
Parent:
1:7afb65e75dcd
Child:
3:25540a08a8d8
ver1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kohacraft 0:c61e5f0d54c1 1 #include "mbed.h"
kohacraft 0:c61e5f0d54c1 2 #include "TLC5940.h"
kohacraft 0:c61e5f0d54c1 3 SPI driver( dp2 , dp1 , dp6 ); //mosi , miso , sck
kohacraft 0:c61e5f0d54c1 4 DigitalOut vprg = dp4;
kohacraft 0:c61e5f0d54c1 5 DigitalOut xlat = dp9;
kohacraft 0:c61e5f0d54c1 6 DigitalOut blank = dp10;
kohacraft 0:c61e5f0d54c1 7 PwmOut gsclk = dp1;
kohacraft 0:c61e5f0d54c1 8 int num_ics = 1; //何個5940がつながっているか
kohacraft 0:c61e5f0d54c1 9
kohacraft 0:c61e5f0d54c1 10 #include "font.h"
kohacraft 0:c61e5f0d54c1 11
kohacraft 0:c61e5f0d54c1 12 DigitalOut col1 = dp11;
kohacraft 0:c61e5f0d54c1 13 DigitalOut col2 = dp13;
kohacraft 0:c61e5f0d54c1 14 DigitalOut col3 = dp14;
kohacraft 0:c61e5f0d54c1 15 DigitalOut col4 = dp15;
kohacraft 0:c61e5f0d54c1 16 DigitalOut col5 = dp16;
kohacraft 0:c61e5f0d54c1 17 DigitalOut col6 = dp17;
kohacraft 0:c61e5f0d54c1 18 DigitalOut col7 = dp18;
kohacraft 0:c61e5f0d54c1 19 DigitalOut col8 = dp26;
kohacraft 0:c61e5f0d54c1 20
kohacraft 0:c61e5f0d54c1 21 DigitalIn fastSw = dp28;
kohacraft 0:c61e5f0d54c1 22 DigitalIn slowSw = dp25;
kohacraft 0:c61e5f0d54c1 23
kohacraft 0:c61e5f0d54c1 24 #define dispTime 10
kohacraft 2:6edd801ae662 25 #define fastCount 500
kohacraft 0:c61e5f0d54c1 26 #define slowCount 50
kohacraft 2:6edd801ae662 27 #define brightChange 1
kohacraft 2:6edd801ae662 28 #define brightMax 2024
kohacraft 2:6edd801ae662 29
kohacraft 2:6edd801ae662 30 int brightCangeValue;
kohacraft 2:6edd801ae662 31
kohacraft 2:6edd801ae662 32 Ticker secTick;
kohacraft 2:6edd801ae662 33
kohacraft 2:6edd801ae662 34 int min;//
kohacraft 2:6edd801ae662 35 int hour =1;//
kohacraft 2:6edd801ae662 36 unsigned short LEDS[16];
kohacraft 0:c61e5f0d54c1 37
kohacraft 0:c61e5f0d54c1 38 //時間の数字をドッドの数字に変換
kohacraft 0:c61e5f0d54c1 39 unsigned char hourDecfont( int num , int colm )
kohacraft 0:c61e5f0d54c1 40 {
kohacraft 0:c61e5f0d54c1 41 unsigned char fontTemp = 0;
kohacraft 0:c61e5f0d54c1 42 unsigned char fontTemp2 = 0;
kohacraft 0:c61e5f0d54c1 43 int numTemp = 0 ;
kohacraft 0:c61e5f0d54c1 44 numTemp = num / 10;
kohacraft 0:c61e5f0d54c1 45 if( numTemp >0 )
kohacraft 0:c61e5f0d54c1 46 {
kohacraft 0:c61e5f0d54c1 47 fontTemp = font[ numTemp ][ colm ];
kohacraft 0:c61e5f0d54c1 48 fontTemp = fontTemp << 5;
kohacraft 0:c61e5f0d54c1 49 }
kohacraft 0:c61e5f0d54c1 50 numTemp = num - numTemp*10;
kohacraft 0:c61e5f0d54c1 51 fontTemp2 = font[ numTemp ][ colm ];
kohacraft 0:c61e5f0d54c1 52 fontTemp2 = fontTemp2 << 2;
kohacraft 0:c61e5f0d54c1 53 fontTemp = fontTemp | fontTemp2 | font[ 10 ][ colm ];
kohacraft 0:c61e5f0d54c1 54
kohacraft 0:c61e5f0d54c1 55 return fontTemp;
kohacraft 0:c61e5f0d54c1 56 }
kohacraft 0:c61e5f0d54c1 57
kohacraft 0:c61e5f0d54c1 58 //数字をドッドの数字に変換
kohacraft 0:c61e5f0d54c1 59 unsigned char decfont( int num , int colm )
kohacraft 0:c61e5f0d54c1 60 {
kohacraft 0:c61e5f0d54c1 61 unsigned char fontTemp = 0;
kohacraft 0:c61e5f0d54c1 62 int numTemp = 0 ;
kohacraft 0:c61e5f0d54c1 63 numTemp = num / 10;
kohacraft 0:c61e5f0d54c1 64 fontTemp = font[ numTemp ][ colm ];
kohacraft 0:c61e5f0d54c1 65 fontTemp = fontTemp << 4;
kohacraft 0:c61e5f0d54c1 66 numTemp = num - numTemp*10;
kohacraft 0:c61e5f0d54c1 67 fontTemp = fontTemp | font[ numTemp ][ colm ];
kohacraft 0:c61e5f0d54c1 68
kohacraft 0:c61e5f0d54c1 69 return fontTemp;
kohacraft 0:c61e5f0d54c1 70 }
kohacraft 0:c61e5f0d54c1 71
kohacraft 0:c61e5f0d54c1 72
kohacraft 0:c61e5f0d54c1 73 void fontOut( unsigned char font )
kohacraft 0:c61e5f0d54c1 74 {
kohacraft 0:c61e5f0d54c1 75 unsigned char mask = 0x01;
kohacraft 0:c61e5f0d54c1 76 unsigned char temp = font;
kohacraft 0:c61e5f0d54c1 77 col1 = ( temp & mask );
kohacraft 0:c61e5f0d54c1 78 mask = mask << 1;
kohacraft 0:c61e5f0d54c1 79 col2 = ( temp & mask );
kohacraft 0:c61e5f0d54c1 80 mask = mask << 1;
kohacraft 0:c61e5f0d54c1 81 col3 = ( temp & mask );
kohacraft 0:c61e5f0d54c1 82 mask = mask << 1;
kohacraft 0:c61e5f0d54c1 83 col4 = ( temp & mask );
kohacraft 0:c61e5f0d54c1 84 mask = mask << 1;
kohacraft 0:c61e5f0d54c1 85 col5 = ( temp & mask );
kohacraft 0:c61e5f0d54c1 86 mask = mask << 1;
kohacraft 0:c61e5f0d54c1 87 col6 = ( temp & mask );
kohacraft 0:c61e5f0d54c1 88 mask = mask << 1;
kohacraft 0:c61e5f0d54c1 89 col7 = ( temp & mask );
kohacraft 0:c61e5f0d54c1 90 mask = mask << 1;
kohacraft 0:c61e5f0d54c1 91 col8 = ( temp & mask );
kohacraft 0:c61e5f0d54c1 92 }
kohacraft 2:6edd801ae662 93
kohacraft 2:6edd801ae662 94 void minUp()
kohacraft 2:6edd801ae662 95 {
kohacraft 2:6edd801ae662 96 for( int bright = brightMax ; bright > 0; bright-=brightCangeValue )
kohacraft 2:6edd801ae662 97 {
kohacraft 2:6edd801ae662 98 for( int colmn=0 ; colmn<16 ; colmn++ )
kohacraft 2:6edd801ae662 99 {
kohacraft 2:6edd801ae662 100 brightCangeValue = brightChange;
kohacraft 2:6edd801ae662 101 if( fastSw == 1 )
kohacraft 2:6edd801ae662 102 brightCangeValue = fastCount;
kohacraft 2:6edd801ae662 103 if( slowSw == 1 )
kohacraft 2:6edd801ae662 104 brightCangeValue = slowCount;
kohacraft 2:6edd801ae662 105
kohacraft 2:6edd801ae662 106 for( int h=0; h<16 ; h++ )
kohacraft 2:6edd801ae662 107 LEDS[h] = 0;
kohacraft 2:6edd801ae662 108 LEDS[colmn] = (unsigned short)bright;
kohacraft 2:6edd801ae662 109 offDisp(blank, gsclk);
kohacraft 2:6edd801ae662 110 if( colmn<8 )
kohacraft 2:6edd801ae662 111 fontOut( decfont( min , colmn ) );
kohacraft 2:6edd801ae662 112 else
kohacraft 2:6edd801ae662 113 fontOut( hourDecfont( hour , colmn-8 ) );
kohacraft 2:6edd801ae662 114 update_led (driver, xlat, blank, gsclk , LEDS);
kohacraft 2:6edd801ae662 115 wait_us(dispTime);
kohacraft 2:6edd801ae662 116 }
kohacraft 2:6edd801ae662 117 }
kohacraft 2:6edd801ae662 118
kohacraft 2:6edd801ae662 119 //1分増加させる 60になったら1時間増やす 12時間を超えたら1時に戻す
kohacraft 2:6edd801ae662 120 min += 1;
kohacraft 2:6edd801ae662 121 if( min >= 60 )
kohacraft 2:6edd801ae662 122 {
kohacraft 2:6edd801ae662 123 min = 0;
kohacraft 2:6edd801ae662 124 hour++;
kohacraft 2:6edd801ae662 125 if( hour > 12 )
kohacraft 2:6edd801ae662 126 hour = 1;
kohacraft 2:6edd801ae662 127 }
kohacraft 2:6edd801ae662 128
kohacraft 2:6edd801ae662 129 for( int bright = 0 ; bright < brightMax; bright+=brightCangeValue )
kohacraft 2:6edd801ae662 130 {
kohacraft 2:6edd801ae662 131 for( int colmn=0 ; colmn<16 ; colmn++ )
kohacraft 2:6edd801ae662 132 {
kohacraft 2:6edd801ae662 133 //PWMをクリア
kohacraft 2:6edd801ae662 134 for( int h=0; h<16 ; h++ )
kohacraft 2:6edd801ae662 135 LEDS[h] = 0;
kohacraft 2:6edd801ae662 136 LEDS[colmn] = (unsigned short)bright; //必要なピンのみ指定した明るさでON
kohacraft 2:6edd801ae662 137 offDisp(blank, gsclk); //PWM強制OFF
kohacraft 2:6edd801ae662 138 if( colmn<8 )
kohacraft 2:6edd801ae662 139 fontOut( decfont( min , colmn ) );
kohacraft 2:6edd801ae662 140 else
kohacraft 2:6edd801ae662 141 fontOut( hourDecfont( hour , colmn-8 ) );
kohacraft 2:6edd801ae662 142 update_led (driver, xlat, blank, gsclk , LEDS);
kohacraft 2:6edd801ae662 143 wait_us(dispTime);
kohacraft 2:6edd801ae662 144 }
kohacraft 2:6edd801ae662 145 }
kohacraft 2:6edd801ae662 146
kohacraft 2:6edd801ae662 147 }
kohacraft 2:6edd801ae662 148
kohacraft 2:6edd801ae662 149
kohacraft 2:6edd801ae662 150
kohacraft 0:c61e5f0d54c1 151 int main() {
kohacraft 0:c61e5f0d54c1 152 initFont();
kohacraft 0:c61e5f0d54c1 153
kohacraft 0:c61e5f0d54c1 154 setup (driver, vprg, xlat, blank, gsclk, num_ics );
kohacraft 2:6edd801ae662 155 brightCangeValue = brightChange;
kohacraft 2:6edd801ae662 156 secTick.attach(&minUp, 60.0);
kohacraft 0:c61e5f0d54c1 157
kohacraft 0:c61e5f0d54c1 158 while(1)
kohacraft 0:c61e5f0d54c1 159 {
kohacraft 2:6edd801ae662 160 /* for( hour =1 ; hour<13 ; hour++ )
kohacraft 2:6edd801ae662 161 {
kohacraft 2:6edd801ae662 162 for( min = 1 ; min<60 ; min )
kohacraft 0:c61e5f0d54c1 163 {
kohacraft 2:6edd801ae662 164 while(1)
kohacraft 2:6edd801ae662 165 {*/
kohacraft 0:c61e5f0d54c1 166 if( fastSw == 1 )
kohacraft 2:6edd801ae662 167 minUp();
kohacraft 0:c61e5f0d54c1 168 if( slowSw == 1 )
kohacraft 2:6edd801ae662 169 minUp();
kohacraft 1:7afb65e75dcd 170 for( int colmn=0 ; colmn<16 ; colmn++ )
kohacraft 0:c61e5f0d54c1 171 {
kohacraft 2:6edd801ae662 172
kohacraft 0:c61e5f0d54c1 173 for( int h=0; h<16 ; h++ )
kohacraft 0:c61e5f0d54c1 174 LEDS[h] = 0;
kohacraft 2:6edd801ae662 175 LEDS[colmn] = (unsigned short)brightMax;
kohacraft 0:c61e5f0d54c1 176 offDisp(blank, gsclk);
kohacraft 1:7afb65e75dcd 177 if( colmn<8 )
kohacraft 1:7afb65e75dcd 178 fontOut( decfont( min , colmn ) );
kohacraft 0:c61e5f0d54c1 179 else
kohacraft 1:7afb65e75dcd 180 fontOut( hourDecfont( hour , colmn-8 ) );
kohacraft 0:c61e5f0d54c1 181 update_led (driver, xlat, blank, gsclk , LEDS);
kohacraft 0:c61e5f0d54c1 182 wait_us(dispTime);
kohacraft 0:c61e5f0d54c1 183 }
kohacraft 2:6edd801ae662 184 /* }
kohacraft 0:c61e5f0d54c1 185 }
kohacraft 0:c61e5f0d54c1 186
kohacraft 0:c61e5f0d54c1 187
kohacraft 2:6edd801ae662 188 }*/
kohacraft 0:c61e5f0d54c1 189 }
kohacraft 0:c61e5f0d54c1 190 }