dot Matrix Led Clock with TLC5940

Dependencies:   Kc_TLC5940 mbed

Committer:
kohacraft
Date:
Wed Jul 29 06:45:25 2015 +0000
Revision:
4:a7ed06634621
Parent:
3:25540a08a8d8
dot Matrix Led Clock

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 3:25540a08a8d8 94 //カウントアップ
kohacraft 2:6edd801ae662 95 void minUp()
kohacraft 2:6edd801ae662 96 {
kohacraft 2:6edd801ae662 97 for( int bright = brightMax ; bright > 0; bright-=brightCangeValue )
kohacraft 2:6edd801ae662 98 {
kohacraft 2:6edd801ae662 99 for( int colmn=0 ; colmn<16 ; colmn++ )
kohacraft 2:6edd801ae662 100 {
kohacraft 2:6edd801ae662 101 brightCangeValue = brightChange;
kohacraft 2:6edd801ae662 102 if( fastSw == 1 )
kohacraft 3:25540a08a8d8 103 {
kohacraft 2:6edd801ae662 104 brightCangeValue = fastCount;
kohacraft 3:25540a08a8d8 105 secTick.detach();
kohacraft 3:25540a08a8d8 106 secTick.attach(&minUp, 60.0); //スイッチが押されている間は自動で進まないようにTickをリセット
kohacraft 3:25540a08a8d8 107 }
kohacraft 2:6edd801ae662 108 if( slowSw == 1 )
kohacraft 3:25540a08a8d8 109 {
kohacraft 2:6edd801ae662 110 brightCangeValue = slowCount;
kohacraft 3:25540a08a8d8 111 secTick.detach();
kohacraft 3:25540a08a8d8 112 secTick.attach(&minUp, 60.0);
kohacraft 3:25540a08a8d8 113 }
kohacraft 2:6edd801ae662 114
kohacraft 2:6edd801ae662 115 for( int h=0; h<16 ; h++ )
kohacraft 2:6edd801ae662 116 LEDS[h] = 0;
kohacraft 2:6edd801ae662 117 LEDS[colmn] = (unsigned short)bright;
kohacraft 2:6edd801ae662 118 offDisp(blank, gsclk);
kohacraft 2:6edd801ae662 119 if( colmn<8 )
kohacraft 2:6edd801ae662 120 fontOut( decfont( min , colmn ) );
kohacraft 2:6edd801ae662 121 else
kohacraft 2:6edd801ae662 122 fontOut( hourDecfont( hour , colmn-8 ) );
kohacraft 2:6edd801ae662 123 update_led (driver, xlat, blank, gsclk , LEDS);
kohacraft 2:6edd801ae662 124 wait_us(dispTime);
kohacraft 2:6edd801ae662 125 }
kohacraft 2:6edd801ae662 126 }
kohacraft 2:6edd801ae662 127
kohacraft 2:6edd801ae662 128 //1分増加させる 60になったら1時間増やす 12時間を超えたら1時に戻す
kohacraft 2:6edd801ae662 129 min += 1;
kohacraft 2:6edd801ae662 130 if( min >= 60 )
kohacraft 2:6edd801ae662 131 {
kohacraft 2:6edd801ae662 132 min = 0;
kohacraft 2:6edd801ae662 133 hour++;
kohacraft 2:6edd801ae662 134 if( hour > 12 )
kohacraft 2:6edd801ae662 135 hour = 1;
kohacraft 2:6edd801ae662 136 }
kohacraft 2:6edd801ae662 137
kohacraft 2:6edd801ae662 138 for( int bright = 0 ; bright < brightMax; bright+=brightCangeValue )
kohacraft 2:6edd801ae662 139 {
kohacraft 2:6edd801ae662 140 for( int colmn=0 ; colmn<16 ; colmn++ )
kohacraft 2:6edd801ae662 141 {
kohacraft 2:6edd801ae662 142 //PWMをクリア
kohacraft 2:6edd801ae662 143 for( int h=0; h<16 ; h++ )
kohacraft 2:6edd801ae662 144 LEDS[h] = 0;
kohacraft 2:6edd801ae662 145 LEDS[colmn] = (unsigned short)bright; //必要なピンのみ指定した明るさでON
kohacraft 2:6edd801ae662 146 offDisp(blank, gsclk); //PWM強制OFF
kohacraft 2:6edd801ae662 147 if( colmn<8 )
kohacraft 2:6edd801ae662 148 fontOut( decfont( min , colmn ) );
kohacraft 2:6edd801ae662 149 else
kohacraft 2:6edd801ae662 150 fontOut( hourDecfont( hour , colmn-8 ) );
kohacraft 2:6edd801ae662 151 update_led (driver, xlat, blank, gsclk , LEDS);
kohacraft 2:6edd801ae662 152 wait_us(dispTime);
kohacraft 2:6edd801ae662 153 }
kohacraft 2:6edd801ae662 154 }
kohacraft 2:6edd801ae662 155
kohacraft 2:6edd801ae662 156 }
kohacraft 2:6edd801ae662 157
kohacraft 2:6edd801ae662 158
kohacraft 2:6edd801ae662 159
kohacraft 0:c61e5f0d54c1 160 int main() {
kohacraft 0:c61e5f0d54c1 161 initFont();
kohacraft 0:c61e5f0d54c1 162
kohacraft 0:c61e5f0d54c1 163 setup (driver, vprg, xlat, blank, gsclk, num_ics );
kohacraft 2:6edd801ae662 164 brightCangeValue = brightChange;
kohacraft 3:25540a08a8d8 165 secTick.attach(&minUp, 60.0); //1分でカウントアップ処理を行う
kohacraft 0:c61e5f0d54c1 166
kohacraft 0:c61e5f0d54c1 167 while(1)
kohacraft 0:c61e5f0d54c1 168 {
kohacraft 3:25540a08a8d8 169 //スイッチが押された時の処理
kohacraft 3:25540a08a8d8 170 if( fastSw == 1 )
kohacraft 3:25540a08a8d8 171 minUp();
kohacraft 3:25540a08a8d8 172 if( slowSw == 1 )
kohacraft 3:25540a08a8d8 173 minUp();
kohacraft 3:25540a08a8d8 174
kohacraft 3:25540a08a8d8 175 //16行分ダイナミック点灯させる
kohacraft 3:25540a08a8d8 176 for( int colmn=0 ; colmn<16 ; colmn++ )
kohacraft 2:6edd801ae662 177 {
kohacraft 3:25540a08a8d8 178
kohacraft 3:25540a08a8d8 179 for( int h=0; h<16 ; h++ )
kohacraft 3:25540a08a8d8 180 LEDS[h] = 0;
kohacraft 3:25540a08a8d8 181 LEDS[colmn] = (unsigned short)brightMax;
kohacraft 3:25540a08a8d8 182 offDisp(blank, gsclk);
kohacraft 3:25540a08a8d8 183 if( colmn<8 )
kohacraft 3:25540a08a8d8 184 fontOut( decfont( min , colmn ) );
kohacraft 3:25540a08a8d8 185 else
kohacraft 3:25540a08a8d8 186 fontOut( hourDecfont( hour , colmn-8 ) );
kohacraft 3:25540a08a8d8 187 update_led (driver, xlat, blank, gsclk , LEDS);
kohacraft 3:25540a08a8d8 188 wait_us(dispTime);
kohacraft 3:25540a08a8d8 189 }
kohacraft 0:c61e5f0d54c1 190 }
kohacraft 0:c61e5f0d54c1 191 }