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)实验场景图之二