【雕爷学编程】Arduino动手做(29)---DS1302实时时钟模块5

在这里插入图片描述

37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手试试多做实验,不管成功与否,都会记录下来——小小的进步或是搞不掂的问题,希望能够抛砖引玉。

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二十九:DS1302实时时钟模块CR2032电子掉电走时RTC单片机扩展板 带电池

在这里插入图片描述

7、程序七:检查DS1302模块日期时间的有效性
(1)参考开源代码(Arduino):

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序七:检查DS1302模块日期时间的有效性
  下载驱动库:https://github.com/Makuna/Rtc
  实验接线:
  DS1302 CLK/SCLK --> D6
  DS1302 DAT/IO --> D7
  DS1302 RST/CE --> D8
  DS1302 VCC --> 3.3v - 5v
  DS1302 GND --> GND
*/

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

ThreeWire myWire(7, 6, 8); // DAT, CLK, RST
RtcDS1302<ThreeWire> Rtc(myWire);

void setup () {
  Serial.begin(57600);

  Serial.print("编译: ");
  Serial.print(__DATE__);
  Serial.println(__TIME__);

  Rtc.Begin();

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

  if (!Rtc.IsDateTimeValid())
  {
    //常见原因:
    //     1) 第一次运行时设备尚未运行
    //     2) 设备上的电池电量不足甚至丢失

    Serial.println(" RTC 对日期时间失去信心!");
    Rtc.SetDateTime(compiled);
  }

  if (Rtc.GetIsWriteProtected())
  {
    Serial.println(" RTC 被写保护,现在可以写");
    Rtc.SetIsWriteProtected(false);
  }

  if (!Rtc.GetIsRunning())
  {
    Serial.println("RTC 没有运行,现在开始");
    Rtc.SetIsRunning(true);
  }

  RtcDateTime now = Rtc.GetDateTime();
  if (now < compiled)
  {
    Serial.println("RTC 早于编译时间!(更新日期时间)");
    Rtc.SetDateTime(compiled);
  }
  else if (now > compiled)
  {
    Serial.println(" RTC在线完成更新(这是预期的)");
  }
  else if (now == compiled)
  {
    Serial.println("RTC 与编译时间相同!(不是预期的,但一切都很好)");
  }
}

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

  printDateTime(now);
  Serial.println();

  if (!now.IsValid())
  {
    //常见原因:
    // 1) 设备电池电量低甚至丢失,电源线断开
    Serial.println("RTC 对日期时间失去信心!");
  }

  delay(10000); // 十秒
}

#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);
}

(2)实验串口返回情况

在这里插入图片描述

8、程序八:RTC DS1302 与 TM1637 四位数显示器
(1)参考开源代码(Arduino):

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序八:RTC DS1302 与 TM1637 四位数显示器
  下载驱动库:https://github.com/avishorp/TM1637
  http://www.rinkydinkelectronics.com/library.php?id=5
  实验接线:
  DS1302 CLK/SCLK --> D6
  DS1302 DAT/IO --> D7
  DS1302 RST/CE --> D8
  DS1302 VCC --> 3.3v - 5v
  DS1302 GND --> GND
  TM1637 CLK --> D5
  TM1637 DIO --> D4
*/

#include <DS1302.h>
#include <TM1637Display.h>

// DS1302 初始化设定
DS1302 rtc(8, 7, 6);

// 设定 TM1637 接脚
#define CLK 5
#define DIO 4

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(SUNDAY);         // 设定每周星期几?
  rtc.setTime(19, 25, 30);     // 设定24小时时间 19:26:30
  rtc.setDate(28, 3, 2022);   // 设定日期(日, 月, 年)
}

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();
  // 显示 分:秒
  //int t =  mm.toInt() *100 +ss.toInt();
  segto = 0x80 | display.encodeDigit((t / 100) % 10);
  display.setSegments(&segto, 1, 1);
  delay(500);

  // 显示时间
  display.showNumberDec(t, true);
  delay(500);
}

(2)实验串口返回情况

在这里插入图片描述

(3)实验场景图

在这里插入图片描述
9、程序九:在IIC/I2C 1602 LCD液晶屏上显示动态日期与时间
(1)实验开源仿真编程(Linkboy V4.63)

在这里插入图片描述
(2)实验场景图之一

在这里插入图片描述

(3)实验场景图之二

在这里插入图片描述

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

驴友花雕

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

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

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

打赏作者

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

抵扣说明:

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

余额充值