しかしながらまだ高いのが難点です。そこで価格の安い7セグを使ってみました。7セグなら暗闇でもくっきりはっきりそれなりの雰囲気があります。
ここでは4桁の7セグを直接 Arduino とつないでみました。各セグメントへ7+1ピン、各桁へピンが4つ、あとは GND をつなぎます。注意せねばならないのは、この7セグはカソードコモンなのでドライバ用のトランジスタがGND側で必要だということです。
あとはパターンをセグメントへ送ればいいわけですが、各桁ごとパターンを消去、そしてパターンを送るというプロセスを繰り返します。書き込んで間をおかないとうまく表示されません。
空いているピンでクロック・モジュールを読み込んでいます。
まずはブレッドボードで組んでみたのですが配線が多い。のでモジュールを作ってみました。
digitalWrite() を使ってひとつひとつ書いていくと画面がちらつくので、ポートレジスタへ直接データを書き込んでみました。
コードはこちらです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Clock.cpp | |
* | |
* Created: 2016-06-21 | |
* Author : easai | |
*/ | |
#include <DS3231.h> | |
//DS3231 rtc(SDA, SCL); | |
DS3231 rtc(SCL, SDA); | |
Time t; | |
int seven_segment[] = { | |
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7c, 0x07, 0x7f, 0x67, 0x0 | |
}; | |
int dot = 0b10000000; | |
int digit[] = { | |
0b00000010, 0b00000100, 0b00001000, 0b00010000 | |
}; | |
int pause = 4; | |
void setup() { | |
DDRD = 0b11111111; | |
PORTD = 0; | |
DDRB = 0b00011110; | |
PORTB = 0; | |
rtc.begin(); | |
} | |
void loop() { | |
t = rtc.getTime(); | |
PORTD = seven_segment[t.min % 10]; | |
PORTB = digit[3]; | |
delay(pause); | |
PORTD = seven_segment[(t.min / 10) % 10]; | |
PORTB = digit[2]; | |
delay(pause); | |
PORTD = seven_segment[t.hour % 10]; | |
PORTB = digit[1]; | |
delay(pause); | |
if (10 <= t.hour) | |
{ | |
PORTD = seven_segment[(t.hour / 10) % 10]; | |
PORTB = digit[0]; | |
delay(pause); | |
} | |
PORTD |= dot; | |
} |