2023年11月21日 星期二

利用DS1302 RTC 模組及TM1637 顯示器模組製作數位時鐘

料:Arduino主板一個、雙公頭杜邦線11條、DS1302 RTC 模組 一個、TM1637 顯示器模組一個。
            TM1637 四位數七段顯示器模組規格請參考:TM1637
            DS1302 RTC模組規格請參考:DS1302

效用:利用 DS1302 RTC 模組及 TM1637 顯示器模組製作數位時鐘。

應用:取代傳統的指針時鐘。

提示:1.本實作的 DS1302 RTC 模組 Vcc 接腳連接 3.3V 電源。
            2.DS1302 晶片不太精確,要獲得準確時間可改用 DS3231。

電路接法:


程式碼:
#include "TM1637Display.h"
#include <DS1302.h>
// DS1302 初始化設定
DS1302 rtc(2, 3, 4);

// 設定 TM1637 接腳
#define CLK 9
#define DIO 8

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(FRIDAY);         // 設定每週星期幾?
 //rtc.setTime(00, 27, 30);     // 設定24小時時間 20:16:30
 //rtc.setDate(12, 8, 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();
  segto = 0x80 | display.encodeDigit((t / 100)%10);
  display.setSegments(&segto, 1, 1);
  delay(500);
  display.showNumberDec(t, true);
  delay(500);

}

此實驗程式碼參考:Arduino筆記:即時時鐘 RTC 與 TM1637 四位數顯示器


沒有留言:

張貼留言

熱門文章