LT-5016M1を74HC595で制御します。文字スクロールなどの参考にどうぞ。

Dependencies:   mbed

制作の過程はNotebookに書いています。

main.cpp

Committer:
p4ken
Date:
2017-03-04
Revision:
35:e0efaf59276e
Parent:
34:e9895343c300
Child:
36:cd90267b5a27

File content as of revision 35:e0efaf59276e:

#include "mbed.h"

DigitalOut RCK(D9);// 74595
DigitalOut G(D10);   // 74595
DigitalOut SER(D8); // 74164
DigitalOut CLK(D7); // 74164
//DigitalIn BUTTON(USER_BUTTON); // 点灯・消灯ボタン
//Serial pc(USBTX, USBRX); // USBシリアル通信
//Timer timer; // 経過時間のカウンター

// 点灯パターン 手前赤、手前緑...
unsigned short pattern1[16][2*11] =
{{0,16,0,8,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1028,0,0},
 {0,240,7,200,63,0,0,0,0,0,0,0,0,0,63736,3855,57568,771,3598,514,0,0},
 {0,8,2,4,0,0,49344,49344,257,32896,57825,3855,0,0,2056,2056,4112,1028,4369,514,0,0},
 {0,4,17,18,0,0,41120,8224,514,16448,8738,0,0,0,2056,2056,2056,2056,4369,257,0,0},
 {0,254,63,17,0,0,32896,4112,1028,8224,9252,0,0,0,2056,1028,6168,2056,4369,257,0,0},
 {0,1,16,8,64,0,32896,0,1028,8224,9252,0,0,0,0,1028,0,2056,37265,0,0,0},
 {0,0,16,232,127,0,32896,0,33924,8481,42148,771,0,0,0,514,0,1028,37265,0,0,0},
 {0,252,31,4,8,0,32896,0,33410,8481,25700,1028,0,0,0,514,0,514,20046,0,0,0},
 {0,0,16,4,8,0,32896,32896,257,8224,1028,2056,0,0,0,514,0,257,16448,7196,0,0},
 {0,0,16,6,8,0,32896,0,514,8224,1028,2056,0,0,0,257,32896,0,8224,8738,0,0},
 {0,252,31,5,8,0,32896,0,1028,8224,1028,2056,0,0,0,257,16448,0,8224,8738,0,0},
 {0,128,16,4,8,0,32896,0,1028,8224,1028,2056,0,0,0,257,8224,4112,4112,8738,0,0},
 {0,36,33,4,8,0,32896,4112,33924,8481,9252,2056,0,0,32896,0,4112,4112,4112,8738,0,0},
 {0,36,73,4,8,0,32896,8224,33410,16705,16962,1028,0,0,32896,0,63736,7967,2056,8738,0,0},
 {0,34,72,4,8,0,57568,50115,257,32896,33153,771,0,0,32896,0,0,0,2056,7196,0,0},
 {0,193,15,4,15,0,0,0,0,0,0,0,0,0,0,0,0,0,1028,0,0,0}};
 
 unsigned char pattern2[16][32*11/8] = // 急行13:05やや混雑
{{0,0,16,0,0,0,8,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,64,0,0,64,64,0,0,2,2,32,32,8,8,18,18,0,0},
 {0,0,240,0,7,0,200,0,63,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,128,0,0,128,128,0,0,228,228,63,63,8,8,18,18,0,0},
 {0,0,8,0,2,0,4,0,0,0,0,0,192,192,192,192,1,1,128,128,225,225,15,15,0,0,12,12,1,1,12,12,1,1,40,40,32,32,31,31,73,73,0,0},
 {0,0,4,0,17,0,18,0,0,0,0,0,160,160,32,32,2,2,64,64,34,34,0,0,0,0,8,8,0,0,8,8,0,0,32,32,32,32,20,20,127,127,0,0},
 {0,0,254,0,63,0,17,0,0,0,0,0,128,128,16,16,4,4,32,32,36,36,0,0,0,0,136,136,31,31,136,136,31,31,225,225,63,63,148,148,9,9,0,0},
 {0,0,1,0,16,0,8,0,64,0,0,0,128,128,0,0,4,4,32,32,36,36,0,0,0,0,112,112,32,32,112,112,32,32,42,42,32,32,82,82,9,9,0,0},
 {0,0,0,0,16,0,232,0,127,0,0,0,128,128,0,0,132,132,33,33,164,164,3,3,0,0,24,24,64,64,24,24,64,64,40,40,32,32,113,113,73,73,0,0},
 {0,0,252,0,31,0,4,0,8,0,0,0,128,128,0,0,130,130,33,33,100,100,4,4,0,0,21,21,64,64,21,21,64,64,228,228,63,63,8,8,127,127,0,0},
 {0,0,0,0,16,0,4,0,8,0,0,0,128,128,128,128,1,1,32,32,4,4,8,8,0,0,35,35,64,64,35,35,64,64,4,4,0,0,8,8,9,9,0,0},
 {0,0,0,0,16,0,6,0,8,0,0,0,128,128,0,0,2,2,32,32,4,4,8,8,0,0,32,32,49,49,32,32,49,49,34,34,4,4,127,127,9,9,0,0},
 {0,0,252,0,31,0,5,0,8,0,0,0,128,128,0,0,4,4,32,32,4,4,8,8,0,0,64,64,14,14,64,64,14,14,35,35,36,36,8,8,73,73,0,0},
 {0,0,128,0,16,0,4,0,8,0,0,0,128,128,0,0,4,4,32,32,4,4,8,8,0,0,64,64,0,0,64,64,0,0,226,226,21,21,28,28,127,127,0,0},
 {0,0,36,0,33,0,4,0,8,0,0,0,128,128,16,16,132,132,33,33,36,36,8,8,0,0,128,128,0,0,128,128,0,0,34,34,12,12,42,42,9,9,0,0},
 {0,0,36,0,73,0,4,0,8,0,0,0,128,128,32,32,130,130,65,65,66,66,4,4,0,0,128,128,0,0,128,128,0,0,34,34,68,68,74,74,9,9,0,0},
 {0,0,34,0,72,0,4,0,8,0,0,0,224,224,195,195,1,1,128,128,129,129,3,3,0,0,0,0,1,1,0,0,1,1,226,226,69,69,9,9,73,73,0,0},
 {0,0,193,0,15,0,4,0,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,58,58,120,120,8,8,127,127,0,0}};
char scan = 16; // 点灯中の行
int round = 0; // スキャンを何周したか
int scroll = 0; // 左スクロールした量

void receive() { // シリアル受信割り込み
//    if(pc.getc() == 0b00000001) pc.printf("received!\r\n");
//    pc.putc(pc.getc() + 1); // echo
}

int main() {
    SPI spi(D11, D12, D13); // SPIピン mosi, miso, sclk
    spi.format(8, 0); // 74595へ~ビットずつ送る
    spi.frequency(1*1000*1000); // 74595へのクロック周波数 最高96 初期1
//    pc.attach(receive, Serial::RxIrq); // シリアル受信割り込み
//    pc.baud(9600); // シリアル通信のクロックレート デフォルト9600
//    timer.start(); // 経過時間のカウントを開始
    
    while(1) {
//        timer.reset(); // 経過時間0秒
        
        // 行選択
        scan++; // 一行進む
        if(scan == 17+0) { // 16行目の次
            SER = 0; // 点灯
            scan = 1; // 1行目に戻る
            round++; // スキャンが1周した
        } else if(scan == 2) { // 2行目
            SER = 1; // 消灯
        }
        
        // 表示パターン送り込み
        for(int i=0; i<4*11; i++){ // iは最大で~*11文字
//            if(round < 500) { 
//                spi.write(pattern1[scan-1][8-1-i%8]);
//            } else if(round < 1000) {
//                spi.write(pattern2[scan-1][4*11-1-i%(4*11)]); //2*11-1-i%(2*11)
//            } else {
//                spi.write(pattern3[scan-1][8-1-i%8]);
//            }
spi.write(0); //消灯
//spi.write(0b1111111111111111);
        }
        
        // スクロール
        if(round > 1500) { // スクロール速度
            scroll++; // 1ドットスクロールする
            round = 0; // スキャン周回カウントリセット
        }
        if(scroll > 32) { // スクロール幅
            scroll = 0; // スクロール数リセット
        }
        
        RCK = 0;
        CLK = 0;
        G = 0; // 点灯
        RCK = 1;
        CLK = 1;
        
        // ここまでの処理時間が長いほど明るくなる
//        while(timer.read_us()<0) { // 数字のぶんだけ暗くする
//            wait_us(10);
//        }

//        G = BUTTON; // ボタン押すと消灯
        
        // ダイナミック点灯のパルス幅を決める
//        while(timer.read_us()<100) { // 100*2500でゆっくり
//            wait_us(10);
//        }
//        pc.printf("%d\r\n", timer.read_us());
//        pc.printf("%d\r\n", round/100);
    }
}