如何使用DS3231模块制作一个计时器

电子说

1.2w人已加入

描述

步骤1:编码

首先,您需要一个DS3231模块及其库:

http://www.rinkydinkelectronics.com/library.php?id 。..

通过Sketch》 Include库将.zip文件夹添加到Arduino IDE中》添加.zip库并找到保存的DS3231.zip库。

使用编程的基本知识,请使用if操作员设置警报或所需的计时器功能。

将&&插入 add 和运算符。 (请参阅最后几行)

#include

// Init the DS3231 using the hardware interface

DS3231 rtc(SDA, SCL);

// Init a Time-data structure

Time t;

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(SUNDAY); // Set Day-of-Week to SUNDAY

//rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)

//rtc.setDate(1, 1, 2016); // Set the date to DD/MM/YYYY

}

void loop()

{

t = rtc.getTime(); // Get data from the DS3231

// Send date over serial connection

Serial.print(“Date: ”);

Serial.print(t.date, DEC);

Serial.print(“/”);

Serial.print(t.mon, DEC);

Serial.print(“/”);

Serial.print(t.year, DEC);

Serial.println();

// Send Day-of-Week and time

Serial.print(“Day of Week: ”);

Serial.print(t.dow, DEC);

Serial.println();

Serial.print(“Time: ”);

Serial.print(t.hour, DEC);

Serial.print(“:”);

Serial.print(t.min, DEC);

Serial.print(“:”);

Serial.print(t.sec, DEC);

Serial.println();

Serial.println(“--------------------------------”);

delay(1000); //Delay is for displaying the time in 1 second interval.

if (t.hour == 14 && t.min == 32 && t.sec == 53)

//Setting alarm/timer at every 2:32:53pm,

//in other words you can insert t.dow for every Thursday?, t.date for specific date?

{ digitalWrite(99, HIGH); delay(5000);

//Lets say that your component is wired to pin 99 and be switched on for 5 seconds,

//whatever you want to do with it

}

}

第2步:告诉时间

更新08/21/2016:显然,在您第一次设置时间后,

rtc.setDOW(SUNDAY); // Set Day-of-Week to SUNDAY

rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)

rtc.setDate(1, 1, 2016); // Set the date to DD/MM/YYYY

您几乎将时间“消耗”到了模块中。现在,

1。您可以关闭并打开Arduino的电源,而不会弄乱DS3231模块中的时间,否则Arduino会使用“ void setup()”命令将时间重置为您设置的原始时间。换句话说,重新启动Arduino意味着重做代码中的所有内容。

2。因此,删除上述命令并仅使用:

void loop(){

Serial.begin(115200);

rtc.begin();

}

,而不是通过读取RTC DS3231模块中的“燃烧”时间来告知时间。

步骤3:结论和参考

总而言之,如果要关闭电源并打开Arduino的电源,并且希望“燃烧”的时间保持静止,则需要进行两次上传过程。首先是“刻录”时间,其次是删除“刻录”代码。而已。简单吧?
        责任编辑:wv

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分