DS1306實時時鐘模組-ESP32


DS1306實時時鐘模組-ESP32

tags: esp32 ds1302

這是廣華電子上的介紹
◎ 時鐘具有能計算2100 年之前的秒、分、時、日、星期、月、年的能力,還有閏年調整的能力
◎ 31 8 位暫存數據存儲RAM
◎ 串行 I/O 口方式使得管腳數量最少
◎ 寬範圍工作電壓2.0 5.5V
◎ 工作電流 2.0V 時,小於300nA
◎ 讀 / 寫時鐘或RAM 數據時有兩種傳送方式單字節傳送和多字節傳送字符組方式
◎ 8 腳DIP 封裝或可選的8 腳SOIC 封裝根據表面裝配
◎ 簡單 3 線接口
◎ 與 TTL 相容Vcc=5V
◎ 雙電源管用於主電源和備份電源供應
◎ 電池:CR2032 電壓3V,電流260mAh
◎ 晶振32.768KHz,匹配電容為6pF
◎ DS1302為8腳直插大晶片,晶片下面有IC座,方便更換及插拔片
6.模塊工作電壓兼容3.3V/5V,可與5V及3.3V單片機連接
◎帶4個定位孔,直徑3.1mm
◎工作溫度:0°~70°

◎ 安裝注意!
1.VCC與GND千萬不要接反,以免燒壞晶片
2.51系列單晶片P0接腳需要連接上拉電阻,如果單晶片沒有連接上拉電阻,可以將數據線接到其他接腳

步驟1:
在Arduino IDE下載程式庫DS1302,並選好版型,COM和波特率

接線

// DS1302 CLK/SCLK --> 5
// DS1302 DAT/IO --> 4
// DS1302 RST/CE --> 2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND

程式碼

#include <ThreeWire.h>  
#include <RtcDS1302.h>
 
ThreeWire myWire(4,5,2); // IO, SCLK, CE  //对应接到ESP32的IO号
RtcDS1302<ThreeWire> Rtc(myWire);
 
void setup () {
    Serial.begin(57600);
    Serial.print("compiled: ");
    Serial.print(__DATE__); //打印系统日期
    Serial.println(__TIME__); //打印系统时间
    Rtc.Begin();  //用管脚声明来初始化RTC
 
    RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); //将系统时间声明一个时间结构
    printDateTime(compiled);  //打印时间
    Serial.println();
 
    if (!Rtc.IsDateTimeValid())   //读RTC时间,如果没读到,就是电池没电了
    {
        // Common Causes:
        //    1) first time you ran and the device wasn't running yet
        //    2) the battery on the device is low or even missing
 
        Serial.println("RTC lost confidence in the DateTime!");
        Rtc.SetDateTime(compiled);    //设置系统时间为当前时间
    }
 
    if (Rtc.GetIsWriteProtected())    //检查是不是写保护了
    {
        Serial.println("RTC was write protected, enabling writing now");
        Rtc.SetIsWriteProtected(false);
    }
 
    if (!Rtc.GetIsRunning())  //检查是不是可读
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }
 
    RtcDateTime now = Rtc.GetDateTime();  //得到RTC的时间
    if (now < compiled) //如果读出的时间早于系统时间,则重新设置RTC时间
    {
        Serial.println("RTC is older than compile time!  (Updating DateTime)");
        Rtc.SetDateTime(compiled);
    }
    else if (now > compiled) //如果读出的时间晚于系统时间,则重新设置RTC时间
    {
        Serial.println("RTC is newer than compile time. (this is expected)");
    }
    else if (now == compiled) 
    {
        Serial.println("RTC is the same as compile time! (not expected but all is fine)");
    }
}
 
void loop () {
    RtcDateTime now = Rtc.GetDateTime();    //核心函数,读当前RTC时间
    printDateTime(now);
    Serial.println();
    if (!now.IsValid()){
        // Common Causes:
        //    1) the battery on the device is low or even missing and the power line was disconnected
        Serial.println("RTC lost confidence in the DateTime!");
    }
    delay(10000); // ten seconds    //延时10s
}
 
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)   //一个通过串口格式化打印日期和时间的函数
{
    char datestring[20];
 
    snprintf_P(datestring,                //avr的一个打印格式化函数
            countof(datestring),
            PSTR("%04u/%02u/%02u %02u:%02u:%02u"),    //和print类似,指定长度
            dt.Year(),
            dt.Month(),
            dt.Day(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
    Serial.print(datestring);
}

DS1306實時時鐘模組+0.96OLED
提示:先下載這些程式庫

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Wire.h>
#include <ThreeWire.h>  
#include <RtcDS1302.h>

ds1302在範例裡面有

// CONNECTIONS:
// DS1302 CLK/SCLK –> 5
// DS1302 DAT/IO –> 4
// DS1302 RST/CE –> 2
// DS1302 VCC –> 3.3v - 5v
// DS1302 GND –> GND

#include <SPI.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Wire.h>
#include <ThreeWire.h>  
#include <RtcDS1302.h>

ThreeWire myWire(4,5,2); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup () 
{
    Serial.begin(115200);
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C, OLED_RESET);
    Serial.print("compiled: ");
    Serial.print(__DATE__);
    Serial.println(__TIME__);

    Rtc.Begin();

    RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
    printDateTime(compiled);
    Serial.println();

    if (!Rtc.IsDateTimeValid()) 
    {
        // Common Causes:
        //    1) first time you ran and the device wasn't running yet
        //    2) the battery on the device is low or even missing

        Serial.println("RTC lost confidence in the DateTime!");
        Rtc.SetDateTime(compiled);
    }

    if (Rtc.GetIsWriteProtected())
    {
        Serial.println("RTC was write protected, enabling writing now");
        Rtc.SetIsWriteProtected(false);
    }

    if (!Rtc.GetIsRunning())
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }

    RtcDateTime now = Rtc.GetDateTime();
    if (now < compiled) 
    {
        Serial.println("RTC is older than compile time!  (Updating DateTime)");
        Rtc.SetDateTime(compiled);
    }
    else if (now > compiled) 
    {
        Serial.println("RTC is newer than compile time. (this is expected)");
    }
    else if (now == compiled) 
    {
        Serial.println("RTC is the same as compile time! (not expected but all is fine)");
    }
}

void loop () 
{
    RtcDateTime now = Rtc.GetDateTime();

    printDateTime(now);
    Serial.println();

    if (!now.IsValid())
    {
        // Common Causes:
        //    1) the battery on the device is low or even missing and the power line was disconnected
        Serial.println("RTC lost confidence in the DateTime!");
    }
    display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(0, 0);
    display.print(now.Year()); display.print("/"); display.print(now.Month()); display.print("/"); display.println(now.Day());
    display.setCursor(0, 20);
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.print(now.Hour()); display.print(":"); display.print(now.Minute()); display.print(":"); display.println(now.Second());
    display.display();
    delay(1000); // ten seconds
}

文章作者: blairan
版權聲明: 本博客所有文章除特別聲明外,均採用 CC BY 4.0 許可協議。轉載請註明來源 blairan !
评论
  目錄