|
| 1 | +/* |
| 2 | + Author: Adam Garbo and Nathan Seidle |
| 3 | + Created: June 3rdrd, 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. Each time |
| 7 | + the alarm triggers, a user-specified additional amount of time (seconds, |
| 8 | + minutes or hours) can be added to create a rolling RTC alarm. Second, |
| 9 | + minute and hour rollovers are handled using modulo calculations. |
| 10 | +
|
| 11 | + The current code is configured as a 5-second rolling RTC alarm. |
| 12 | +*/ |
| 13 | + |
| 14 | +#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core |
| 15 | +APM3_RTC myRTC; // Create instance of RTC class |
| 16 | + |
| 17 | +volatile bool alarmFlag = false; |
| 18 | +int alarmSeconds = 5; |
| 19 | +int alarmMinutes = 0; |
| 20 | +int alarmHours = 0; |
| 21 | + |
| 22 | +void setup() |
| 23 | +{ |
| 24 | + Serial.begin(115200); |
| 25 | + Serial.println("SparkFun RTC Set Rolling Alarms Example"); |
| 26 | + |
| 27 | + // Easily set RTC using the system __DATE__ and __TIME__ macros from compiler |
| 28 | + //myRTC.setToCompilerTime(); |
| 29 | + |
| 30 | + // Manually set RTC date and time |
| 31 | + myRTC.setTime(0, 50, 59, 12, 3, 6, 20); // 12:59:50.000, June 3rd, 2020 (hund, ss, mm, hh, dd, mm, yy) |
| 32 | + |
| 33 | + // Set the RTC's alarm |
| 34 | + myRTC.setAlarm(0, 0, 0, 13, 3, 6); // 13:00:00.000, June 3rd (hund, ss, mm, hh, dd, mm). Note: No year alarm register |
| 35 | + |
| 36 | + // Set the RTC alarm mode |
| 37 | + /* |
| 38 | + 0: Alarm interrupt disabled |
| 39 | + 1: Alarm match every year |
| 40 | + 2: Alarm match every month |
| 41 | + 3: Alarm match every week |
| 42 | + 4: Alarm match every day |
| 43 | + 5: Alarm match every hour |
| 44 | + 6: Alarm match every minute |
| 45 | + 7: Alarm match every second |
| 46 | + */ |
| 47 | + myRTC.setAlarmMode(6); // Set the RTC alarm to match on minutes rollover |
| 48 | + myRTC.attachInterrupt(); // Attach RTC alarm interrupt |
| 49 | + |
| 50 | + // Print the RTC's alarm date and time |
| 51 | + Serial.print("Next alarm: "); printAlarm(); |
| 52 | +} |
| 53 | + |
| 54 | +void loop() |
| 55 | +{ |
| 56 | + // Check if alarm flag was set |
| 57 | + if (alarmFlag == true) |
| 58 | + { |
| 59 | + // Print date and time of RTC alarm trigger |
| 60 | + Serial.print("Alarm triggered: "); printDateTime(); |
| 61 | + |
| 62 | + // Clear alarm flag |
| 63 | + alarmFlag = false; |
| 64 | + |
| 65 | + // Set the RTC's rolling alarm |
| 66 | + myRTC.setAlarm(0, (myRTC.seconds + alarmSeconds) % 60, |
| 67 | + (myRTC.minute + alarmMinutes) % 60, |
| 68 | + (myRTC.hour + alarmHours) % 24, |
| 69 | + myRTC.dayOfMonth, |
| 70 | + myRTC.month); |
| 71 | + myRTC.setAlarmMode(6); |
| 72 | + |
| 73 | + // Print next RTC alarm date and time |
| 74 | + Serial.print("Next rolling alarm: "); printAlarm(); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// Print the RTC's current date and time |
| 79 | +void printDateTime() |
| 80 | +{ |
| 81 | + myRTC.getTime(); |
| 82 | + char dateTimeBuffer[25]; |
| 83 | + sprintf(dateTimeBuffer, "20%02d-%02d-%02d %02d:%02d:%02d.%03d", |
| 84 | + myRTC.year, myRTC.month, myRTC.dayOfMonth, |
| 85 | + myRTC.hour, myRTC.minute, myRTC.seconds, myRTC.hundredths); |
| 86 | + Serial.println(dateTimeBuffer); |
| 87 | +} |
| 88 | + |
| 89 | +// Print the RTC's alarm |
| 90 | +void printAlarm() |
| 91 | +{ |
| 92 | + myRTC.getAlarm(); |
| 93 | + char alarmBuffer[25]; |
| 94 | + sprintf(alarmBuffer, "2020-%02d-%02d %02d:%02d:%02d.%03d", |
| 95 | + myRTC.alarmMonth, myRTC.alarmDayOfMonth, |
| 96 | + myRTC.alarmHour, myRTC.alarmMinute, |
| 97 | + myRTC.alarmSeconds, myRTC.alarmHundredths); |
| 98 | + Serial.println(alarmBuffer); |
| 99 | +} |
| 100 | + |
| 101 | +// Interrupt handler for the RTC |
| 102 | +extern "C" void am_rtc_isr(void) |
| 103 | +{ |
| 104 | + // Clear the RTC alarm interrupt. |
| 105 | + am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM); |
| 106 | + |
| 107 | + // Set alarm flag |
| 108 | + alarmFlag = true; |
| 109 | +} |
0 commit comments