1. 程式人生 > >Arduino UNO DS3231高精度RTC晶片 製作時鐘

Arduino UNO DS3231高精度RTC晶片 製作時鐘

DS3231 模組

是一個時鐘模組,上面包含一個鈕釦電池位置,可以在主機斷電的情況下還可以繼續計算時間,以便以後記錄使用。


模組引數:
   1.尺寸:38mm(長)*22mm(寬)*14mm(高)
   2.重量:8g
   3.工作電壓:3.3--5.5V
   4.時鐘晶片:高精度時鐘晶片DS3231
   5.時鐘精度:0-40℃範圍內,精度2ppm,年誤差約1分鐘
   6.帶2個日曆鬧鐘
   7.可程式設計方波輸出
   8.實時時鐘產生秒、分、時、星期、日期、月和年計時,並提供有效期到2100年的閏年補償
   9.晶片內部自帶溫度感測器,精度為±3℃
   10.儲存晶片:AT24C32(儲存容量32K)
   11.IIC匯流排介面,最高傳輸速度400KHz(工作電壓為5V時)
   12.可級聯其它IIC裝置,24C32地址可通過短路A0/A1/A2修改,預設地址為0x57
   13.帶可充電電池LIR2032,保證系統斷電後,時鐘任然正常走動

實驗效果

通過設定時間的程式後,

我們執行顯示時間的程式,就可以看到時鐘模組當前的時間了


BOM表

Arduino UNO    *1

DS3231 時鐘模組 *1

跳線若干

接線


Arduino   Uno                               DS3231

    GND                     <--->                GND

   5V                          <--->                VCC

   A4(SDA)                          <--->                SDA

   A5 (SCL)                         <--->                 SCL

程式

需要下載庫

設定時間的程式

// DS3231_Serial_Easy
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
//
// A quick demo of how to use my DS3231-library to 
// quickly send time and date information over a serial link
//
// To use the hardware I2C (TWI) interface of the Arduino you must connect
// the pins as follows:
//
// Arduino Uno/2009:
// ----------------------
// DS3231:  SDA pin   -> Arduino Analog 4 or the dedicated SDA pin
//          SCL pin   -> Arduino Analog 5 or the dedicated SCL pin
//
// Arduino Leonardo:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 2 or the dedicated SDA pin
//          SCL pin   -> Arduino Digital 3 or the dedicated SCL pin
//
// Arduino Mega:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 20 (SDA) or the dedicated SDA pin
//          SCL pin   -> Arduino Digital 21 (SCL) or the dedicated SCL pin
//
// Arduino Due:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin
//          SCL pin   -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin
//
// The internal pull-up resistors will be activated when using the 
// hardware I2C interfaces.
//
// You can connect the DS3231 to any available pin but if you use any
// other than what is described above the library will fall back to
// a software-based, TWI-like protocol which will require exclusive access 
// to the pins used, and you will also have to use appropriate, external
// pull-up resistors on the data and clock signals.
//

#include <DS3231.h>

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

void setup()
{
  // Setup Serial connection
  Serial.begin(115200);
  // Uncomment the next line if you are using an Arduino Leonardo
  //while (!Serial) {}
  
  // Initialize the rtc object
  rtc.begin();
  
  // The following lines can be uncommented to set the date and time
  rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(1, 1, 2014);   // Set the date to January 1st, 2014
}

void loop()
{
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
  
  // Wait one second before repeating :)
  delay (1000);
}

顯示時間的程式

// DS3231_Serial_Easy
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
//
// A quick demo of how to use my DS3231-library to 
// quickly send time and date information over a serial link
//
// To use the hardware I2C (TWI) interface of the Arduino you must connect
// the pins as follows:
//
// Arduino Uno/2009:
// ----------------------
// DS3231:  SDA pin   -> Arduino Analog 4 or the dedicated SDA pin
//          SCL pin   -> Arduino Analog 5 or the dedicated SCL pin
//
// Arduino Leonardo:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 2 or the dedicated SDA pin
//          SCL pin   -> Arduino Digital 3 or the dedicated SCL pin
//
// Arduino Mega:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 20 (SDA) or the dedicated SDA pin
//          SCL pin   -> Arduino Digital 21 (SCL) or the dedicated SCL pin
//
// Arduino Due:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin
//          SCL pin   -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin
//
// The internal pull-up resistors will be activated when using the 
// hardware I2C interfaces.
//
// You can connect the DS3231 to any available pin but if you use any
// other than what is described above the library will fall back to
// a software-based, TWI-like protocol which will require exclusive access 
// to the pins used, and you will also have to use appropriate, external
// pull-up resistors on the data and clock signals.
//

#include <DS3231.h>

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

void setup()
{
  // Setup Serial connection
  Serial.begin(115200);
  // Uncomment the next line if you are using an Arduino Leonardo
  //while (!Serial) {}
  
  // Initialize the rtc object
  rtc.begin();
  
  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(1, 1, 2014);   // Set the date to January 1st, 2014
}

void loop()
{
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
  
  // Wait one second before repeating :)
  delay (1000);
}

思路講解

1,#include <DS3231.h>    //載入DS3231庫

2,DS3231  rtc(SDA, SCL);    //設定I2C

3,rtc.begin();   //建立RTC物件

4,

  rtc.setDOW(WEDNESDAY);     // 設定星期幾,例如 SUNDAY
  rtc.setTime(12, 0, 0);     // 設定時間 12:00:00 (24小時制)
  rtc.setDate(1, 1, 2014);   // 設定日期  1月,1日 ,2014 年

如果在顯示程式中,或不需要設定時間的時候,可以在前面加//給註釋掉

5,

rtc.getDOWStr()   獲取星期幾

rtc.getDateStr()    獲取日期

rtc.getTimeStr()   獲取時間