ESP8266(ESP-12F) 第三方库使用 -- RtcDS1302

RTC时钟模块在各种设备中实时设备中都是不可或缺的模块之一,特别是在我们常用的电子钟,台式电脑笔记本,甚至手机上都可以见到其存在,本篇将大致介绍时钟模块和RTC库的使用及简单示例。


模块简介

DS1302时钟模块,DS1302是DALLAS公司推出的涓流充电时钟芯片,内含有一个实时时钟/日历和31字节静态RAM,通过简单的串行接口与单片机进行通信。实时时钟/日历电路提供秒、分、时、日、周、月、年的信息,每月的天数和闰年的天数可自动调整。时钟操作可通过AM/PM指示决定采用24或12小时格式。DS1302与单片机之间能简单地采用同步串行的方式进行通信,仅需用到三个口线:
1)RST复位;
2)I/O数据线;
3)SCLK串行时钟。
时钟/RAM的读/写数据以一个字节或多达31个字节的字符组方式通信。DS1302工作时功耗很低保持数据和时钟信息时功率小于1mW。
在这里插入图片描述

引脚说明
  • VCC:接电源正极5V/3.3V;

  • GND:接电源负极;

  • CLK:接控制板I/O;

  • DAT:接控制板I/O;

  • RST:接控制板I/O;

其中CLK、DAT、RST引脚可接控制板的任意IO

第三方库安装

打开Arduino IDE,项目 → 加载库 → 管理库...
在这里插入图片描述
在搜索框输入关键词 ds1302 ,安装第一个库,目前最新版本是2.3.5
在这里插入图片描述

RtcDS1302示例

添加头文件

#include <ThreeWire.h>
#include <RtcDS1302.h>

定义模块引脚与RTC类对象

// DS1302 CLK/SCLK --> ESP-12F D2
// DS1302 DAT/IO   --> ESP-12F D1
// DS1302 RST/CE   --> ESP-12F D0
int DS1302_RST = D0;
int DS1302_DAT = D1;
int DS1302_CLK = D2;

ThreeWire myWire(DS1302_DAT,DS1302_CLK,DS1302_RST); // DAT, CLK, RST
RtcDS1302<ThreeWire> Rtc(myWire);

在setup函数中初始化模块,并判断RTC模块中的时间日期是否有效(RTC模块出厂都已经写入了时间)

void setup ()
{
	Rtc.Begin();
	Serial.begin(115200);

	if (!Rtc.IsDateTimeValid()) 
    {
        Serial.println("RTC lost confidence in the DateTime!");
    }
}

当发现RTC时间丢失或错乱,可以为RTC重新写入时间,如下

if (!Rtc.IsDateTimeValid()) 
{
    Serial.println("RTC lost confidence in the DateTime!");
	//新建RtcDateTime 对象,并写入日期时间,参数顺序为Year,Month,Day,Hour,Minutes,Seconds
	//新建RTC时间对象 - 2021年6月3日9点整(默认24小时制)
	RtcDateTime DStime_update(2021,6,3,9,0,0);
	
	//调用SetDateTime函数为RTC模块写入时间
	Rtc.SetDateTime(DStime_update);
}

loop函数中间隔进行RTC时间打印

void loop () 
{
	//GetDateTime函数获取RTC模块时间
    RtcDateTime now = Rtc.GetDateTime();

    printDateTime(now);
    Serial.println();

    delay(10000); 					// ten seconds
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
    char datestring[20];

    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            dt.Month(),
            dt.Day(),
            dt.Year(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
    Serial.print(datestring);
}

完整示例代码

#include <ThreeWire.h>
#include <RtcDS1302.h>

// DS1302 CLK/SCLK --> ESP-12F D2
// DS1302 DAT/IO   --> ESP-12F D1
// DS1302 RST/CE   --> ESP-12F D0
int DS1302_RST = D0;
int DS1302_DAT = D1;
int DS1302_CLK = D2;

ThreeWire myWire(DS1302_DAT,DS1302_CLK,DS1302_RST); // DAT, CLK, RST
RtcDS1302<ThreeWire> Rtc(myWire);

void setup ()
{
	Rtc.Begin();
	Serial.begin(115200);

	if (!Rtc.IsDateTimeValid()) 
	{
	    Serial.println("RTC lost confidence in the DateTime!");
		//新建RtcDateTime 对象,并写入日期时间,参数顺序为Year,Month,Day,Hour,Minutes,Seconds
		//新建RTC时间对象 - 2021年6月3日9点整(默认24小时制)
		RtcDateTime DStime_update(2021,6,3,9,0,0);
		
		//调用SetDateTime函数为RTC模块写入时间
		Rtc.SetDateTime(DStime_update);
	}
}

void loop () 
{
	//GetDateTime函数获取RTC模块时间
    RtcDateTime now = Rtc.GetDateTime();

    printDateTime(now);
    Serial.println();

    delay(10000); 					// ten seconds
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
    char datestring[20];

    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            dt.Month(),
            dt.Day(),
            dt.Year(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
    Serial.print(datestring);
}
示例运行效果

间隔打印时间
在这里插入图片描述

  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GenCoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值