以前买过一个4位的数码管一直没用过, 放着也是吃灰,就拿来做一个简易时钟吧。我并不需要什么复杂的功能,能显示时间就行,所以连按钮也没有设计。
硬件:
Arduino nano一块
DS1302时钟电路一个
TM1637-4位数码管一个
杜邦线若干
连接:
DS1302:
VCC — VCC
GND — GND
RST — D4
DAT — D5
CLK — D6
4位数码管:
VCC — VCC
GND — GND
CLK — D3DIO — D2
连接图:
代码:
#include <ds1302.h>//这里因为我有多个1302的库,所以把库重命名了
#include <TM1637Display.h>
// DS1302 初始化設定
DS1302 rtc(4, 5, 6);//RST-4,DAT-5,CLK-6
// 設定 TM1637 接腳
#define CLK 3
#define DIO 2
TM1637Display display(CLK, DIO);
boolean colon = true ;
String dw = "";
String hh = "";
String mm = "";
String ss = "";
float t = 0;
void setup()
{
// 設定時鐘執行模式,取消寫入保護
rtc.halt(false);
rtc.writeProtect(false);
// Setup Serial connection
Serial.begin(9600);
display.setBrightness(0xA);
// 第一次設定寫入 DS1302 RTC時鐘,之後可以加上註解
// rtc.setDOW(SUNDAY); // 設定每週星期幾?
// rtc.setTime(20, 16, 30); // 設定24小時時間 20:16:30
// rtc.setDate(19, 3, 2017); // 設定日期(日, 月, 年)
}
void loop()
{
// 取得星期幾
Serial.print(rtc.getDOWStr());
Serial.print(" ");
// 取得日期
Serial.print(rtc.getDateStr());
Serial.print(" -- ");
// 取得時間
dw = rtc.getTimeStr();
Serial.println(dw);
hh = dw.substring(0,2); // 時數
mm = dw.substring(3,5); // 分鐘數
ss = dw.substring(6,8); // 秒數
// 顯示四位數中間的冒號
uint8_t segto;
int value = 1000;
// 顯示 時:分
int t = hh.toInt()*100 + mm.toInt();
// 顯示 分:秒
// int t = mm.toInt() *100 +ss.toInt();
segto = 0x80 | display.encodeDigit((t / 100)%10);
display.setSegments(&segto, 1, 1);
delay(500);
// 顯示時間
display.showNumberDec(t, true);
delay(500);
}
涉及到的库:
http://www.rinkydinkelectronics.com/download.php?f=DS1302.zip
https://github.com/avishorp/TM1637
成品图:
最后套一个壳子:
参考文章:http://atceiling.blogspot.ca/2017/03/arduino-rtc-tm1637.html







发表评论: