|
| 1 | +/* |
| 2 | + Author: Adam Garbo |
| 3 | + Created: May 23rd, 2020 |
| 4 | + License: MIT. See SparkFun Arduino Apollo3 Project for more information |
| 5 | +
|
| 6 | + This example demonstrates how to read and set rolling RTC alarms. |
| 7 | +*/ |
| 8 | + |
| 9 | +#include "RTC.h" //Include RTC library included with the Aruino_Apollo3 core |
| 10 | +APM3_RTC myRTC; //Create instance of RTC class |
| 11 | + |
| 12 | +volatile bool alarmFlag = false; |
| 13 | + |
| 14 | +void setup() |
| 15 | +{ |
| 16 | + Serial.begin(115200); |
| 17 | + Serial.println("SparkFun RTC Set Rolling Alarms Example"); |
| 18 | + |
| 19 | + //myRTC.setToCompilerTime(); //Easily set RTC using the system __DATE__ and __TIME__ macros from compiler |
| 20 | + myRTC.setTime(0, 50, 59, 12, 23, 5, 20); // Manually set RTC date and time (hund, ss, mm, hh, dd, mm, yy) |
| 21 | + |
| 22 | + Serial.print("Initial alarm: "); printAlarm(); // Initial alarm should read all zeroes |
| 23 | + |
| 24 | + // Set the RTC's alarm |
| 25 | + myRTC.setAlarm(0, 0, 0, 13, 23, 5); // Set alarm (hund, ss, mm, hh, dd, mm). Note: no year alarm register |
| 26 | + |
| 27 | + // Set the RTC alarm mode |
| 28 | + /* |
| 29 | + 0: Alarm interrupt disabled |
| 30 | + 1: Alarm match every year |
| 31 | + 2: Alarm match every month |
| 32 | + 3: Alarm match every week |
| 33 | + 4: Alarm match every day |
| 34 | + 5: Alarm match every hour |
| 35 | + 6: Alarm match every minute |
| 36 | + 7: Alarm match every second |
| 37 | + 8: Alarm match every 10th second |
| 38 | + 9: Alarm match every 100th second |
| 39 | + */ |
| 40 | + myRTC.setAlarmMode(6); // Set the RTC alarm to match on minutes rollover |
| 41 | + myRTC.attachInterrupt(); // Attach RTC alarm interrupt |
| 42 | + |
| 43 | + Serial.print("Next alarm: "); printAlarm(); // Print current alarm date and time |
| 44 | +} |
| 45 | + |
| 46 | +void loop() |
| 47 | +{ |
| 48 | + if (alarmFlag == true) |
| 49 | + { |
| 50 | + Serial.print("Alarm triggered: "); printDateTime(); |
| 51 | + alarmFlag = false; |
| 52 | + |
| 53 | + // Set the RTC's rolling alarm |
| 54 | + myRTC.setAlarm(0, (myRTC.seconds + 5) % 60, myRTC.minute, |
| 55 | + myRTC.hour, myRTC.dayOfMonth, myRTC.month); |
| 56 | + myRTC.setAlarmMode(2); |
| 57 | + Serial.print("Rolling alarm: "); printAlarm(); |
| 58 | + } |
| 59 | + |
| 60 | + printDateTime(); |
| 61 | + delay(1000); |
| 62 | +} |
| 63 | + |
| 64 | +// Print the RTC's current date and time |
| 65 | +void printDateTime() |
| 66 | +{ |
| 67 | + myRTC.getTime(); |
| 68 | + char dateTimeBuffer[25]; |
| 69 | + sprintf(dateTimeBuffer, "20%02d-%02d-%02d %02d:%02d:%02d.%03d", |
| 70 | + myRTC.year, myRTC.month, myRTC.dayOfMonth, |
| 71 | + myRTC.hour, myRTC.minute, myRTC.seconds, myRTC.hundredths); |
| 72 | + Serial.println(dateTimeBuffer); |
| 73 | +} |
| 74 | + |
| 75 | +// Print the RTC's alarm |
| 76 | +void printAlarm() |
| 77 | +{ |
| 78 | + myRTC.getAlarm(); |
| 79 | + char alarmBuffer[25]; |
| 80 | + sprintf(alarmBuffer, "2020-%02d-%02d %02d:%02d:%02d.%03d", |
| 81 | + myRTC.alarmMonth, myRTC.alarmDayOfMonth, |
| 82 | + myRTC.alarmHour, myRTC.alarmMinute, |
| 83 | + myRTC.alarmSeconds, myRTC.alarmHundredths); |
| 84 | + Serial.println(alarmBuffer); |
| 85 | +} |
| 86 | + |
| 87 | +// Interrupt handler for the RTC |
| 88 | +extern "C" void am_rtc_isr(void) |
| 89 | +{ |
| 90 | + // Clear the RTC alarm interrupt. |
| 91 | + am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM); |
| 92 | + |
| 93 | + // Set alarm flag |
| 94 | + alarmFlag = true; |
| 95 | +} |
0 commit comments