Home> Note> Arduino>時鐘模組 DS1307

溫室自動控制系統 時鐘模組 DS1307

2016.11.27

  這篇要介紹的是 時鐘模組 DS1307 ,如下圖:

基本資料:

Arduino IDE : 1.0.6
Arduino module: Arduino UNO
感測器模組:DS1307

這個時鐘模組非常簡單,因為網路上的很多大神們,都已經把資料開源了,只要使用該資料庫設定就可以設定以及拿到資料

注意!! Arduino 1.6.X 不能執行本模組的library,寫文章的時候一直無法成功,後來才發現是版本問題。

接法

備註:Arduino 其他型號I2C並不是A4、A5,請上官網查詢資料。

  • SDA    <=> A4
  • SCL    <=> A5
  • VCC   <=> 5V
  • GND   <=> 接地

時間設定

將以下的兩個檔案解壓縮之後,丟入Arduino librarys 中

1.Time:https://drive.google.com/open?id=0B2tatd52yWyQLXl6N0pXWkxHR1E

2.DS1307:https://drive.google.com/open?id=0B2tatd52yWyQRi1VMjBSeGVraXc

丟完之後,Arduino IDE 關閉在打開,避免沒有讀到資料

。打開「Example」 > 「DS1307」 > 「Settime」

將程式燒入Arduino 打開 Monitor,他就會自動設定了

。結束之後打開「Expample」 > 「DS1307」 > 「ReadTest」

就可以得到模組裡面的時間了,他會自動的計算

以上就是設定跟讀取資料的方法,這很容易

利用DS1307 設計定時裝置

要用這個模組,就是想要做出定時的功能,當然...也可以用硬體的計時模組,直接斷電開關,不過使用軟體的話,有更多的可以配合的應用。

  • 宣告變數
  • //秒、分、時、天、月
    int h_time;
    int m_time;
    int s_time;
    int m_day;
    int d_day;
  • 取得時間,並且存入變數
  • tmElements_t tm;
    RTC.read(tm);
    h_time=tm.Hour;
    m_time=tm.Minute;
    s_time=tm.Second;
    m_day=tm.Month;
    d_day=tm.Day;
  • 再宣告一組手動設定的時間變數,就是讓使用者定時設定的功能
  • //預設12:30:30
    int hand_h_time = 12;
    int hand_m_time = 30;
    int hand_s_time = 30;
    
  • 我是使用按鈕讓使用者增減時間,當然會有超過判斷副程式
  • /******   如果overflow 就減回去多出來的時間********/
    // timer_counter 一次增加的數值
    // timer_maximun 小時就是24,分鐘是60,傳入時自行判斷
    int timer_overflow(int value,int timer_counter,int maximun){
      if(value + timer_counter >= maximun){
        value = value + timer_counter - maximun;
      }
      else{
        value = value + timer_counter ;
      }
      return(value);
    }
  • 最後判斷時間
  •     void timer_check_fun(){
      if(h_time == hand_h_time && m_time == hand_m_time && s_time == hand_s_time){
        timer_check = true;
      }
    }
    

備註:以上的程式碼都是部分重要功能,需要經過一些思考再直接應用,不是全部複製在一起就可以用。

----------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------

Top