步驟1:編碼
首先,您需要一個(gè)DS3231模塊及其庫(kù):
http://www.rinkydinkelectronics.com/library.php?id 。..
通過(guò)Sketch》 Include庫(kù)將.zip文件夾添加到Arduino IDE中》添加.zip庫(kù)并找到保存的DS3231.zip庫(kù)。
使用編程的基本知識(shí),請(qǐng)使用if操作員設(shè)置警報(bào)或所需的計(jì)時(shí)器功能。
將&&插入 add 和運(yùn)算符。 (請(qǐng)參閱最后幾行)
#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步:告訴時(shí)間
更新08/21/2016:顯然,在您第一次設(shè)置時(shí)間后,
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
您幾乎將時(shí)間“消耗”到了模塊中?,F(xiàn)在,
1。您可以關(guān)閉并打開(kāi)Arduino的電源,而不會(huì)弄亂DS3231模塊中的時(shí)間,否則Arduino會(huì)使用“ void setup()”命令將時(shí)間重置為您設(shè)置的原始時(shí)間。換句話說(shuō),重新啟動(dòng)Arduino意味著重做代碼中的所有內(nèi)容。
2。因此,刪除上述命令并僅使用:
void loop(){
Serial.begin(115200);
rtc.begin();
}
,而不是通過(guò)讀取RTC DS3231模塊中的“燃燒”時(shí)間來(lái)告知時(shí)間。
步驟3:結(jié)論和參考
總而言之,如果要關(guān)閉電源并打開(kāi)Arduino的電源,并且希望“燃燒”的時(shí)間保持靜止,則需要進(jìn)行兩次上傳過(guò)程。首先是“刻錄”時(shí)間,其次是刪除“刻錄”代碼。而已。簡(jiǎn)單吧?
責(zé)任編輯:wv
-
計(jì)時(shí)器
+關(guān)注
關(guān)注
1文章
417瀏覽量
32610 -
DS3231
+關(guān)注
關(guān)注
2文章
51瀏覽量
23815
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論