|
| 1 | +/* |
| 2 | + Author: Adam Garbo and Nathan Seidle |
| 3 | + Created: June 3rd, 2020 |
| 4 | + License: MIT. See SparkFun Arduino Apollo3 Project for more information |
| 5 | +
|
| 6 | + This example demonstrates how to read and set the RTC alarms. |
| 7 | +
|
| 8 | + It is necessary to first set the RTC alarm date and time and then specify |
| 9 | + the alarm mode, which determines which date and time values will be used |
| 10 | + for comparison when generating an alarm interrupt. |
| 11 | +
|
| 12 | + The code is configured so that the RTC alarm will trigger every minute. |
| 13 | + The RTC interrupt service routine will set an alarm flag each time the |
| 14 | + alarm triggers and the RTC date and time will printed to the Serial Monitor. |
| 15 | +*/ |
| 16 | + |
| 17 | +#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core |
| 18 | +APM3_RTC myRTC; // Create instance of RTC class |
| 19 | + |
| 20 | +volatile bool alarmFlag = false; |
| 21 | + |
| 22 | +void setup() |
| 23 | +{ |
| 24 | + Serial.begin(115200); |
| 25 | + Serial.println("SparkFun RTC Set Alarm 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(12, 59, 50, 0, 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(13, 0, 0, 0, 3, 6); // 13:00:00.000, June 3rd (hund, ss, mm, hh, dd, mm). Note: No year alarm register |
| 35 | + // Set the RTC alarm mode |
| 36 | + /* |
| 37 | + 0: Alarm interrupt disabled |
| 38 | + 1: Alarm match every year (hundredths, seconds, minutes, hour, day, month) |
| 39 | + 2: Alarm match every month (hundredths, seconds, minutes, hours, day) |
| 40 | + 3: Alarm match every week (hundredths, seconds, minutes, hours, weekday) |
| 41 | + 4: Alarm match every day (hundredths, seconds, minute, hours) |
| 42 | + 5: Alarm match every hour (hundredths, seconds, minutes) |
| 43 | + 6: Alarm match every minute (hundredths, seconds) |
| 44 | + 7: Alarm match every second (hundredths) |
| 45 | + */ |
| 46 | + myRTC.setAlarmMode(6); // Set the RTC alarm to match on minutes rollover |
| 47 | + myRTC.attachInterrupt(); // Attach RTC alarm interrupt |
| 48 | + |
| 49 | + // Print the RTC's alarm date and time |
| 50 | + Serial.print("Next alarm: "); printAlarm(); |
| 51 | +} |
| 52 | + |
| 53 | +void loop() |
| 54 | +{ |
| 55 | + // Check if alarm flag was set |
| 56 | + if (alarmFlag == true) |
| 57 | + { |
| 58 | + // Print date and time of RTC alarm trigger |
| 59 | + Serial.print("Alarm triggered: "); printDateTime(); |
| 60 | + |
| 61 | + // Clear alarm flag |
| 62 | + alarmFlag = false; |
| 63 | + } |
| 64 | + |
| 65 | + // Print RTC's date and time while waiting for alarm |
| 66 | + static uint32_t nextPrint = 0; |
| 67 | + if(millis() > nextPrint){ |
| 68 | + printDateTime(); |
| 69 | + nextPrint = millis() + 1000; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Print the RTC's current date and time |
| 74 | +void printDateTime() |
| 75 | +{ |
| 76 | + myRTC.getTime(); |
| 77 | + char dateTimeBuffer[25]; |
| 78 | + sprintf(dateTimeBuffer, "20%02d-%02d-%02d %02d:%02d:%02d.%03d", |
| 79 | + myRTC.year, myRTC.month, myRTC.dayOfMonth, |
| 80 | + myRTC.hour, myRTC.minute, myRTC.seconds, myRTC.hundredths); |
| 81 | + Serial.println(dateTimeBuffer); |
| 82 | +} |
| 83 | + |
| 84 | +// Print the RTC's alarm |
| 85 | +void printAlarm() |
| 86 | +{ |
| 87 | + myRTC.getAlarm(); |
| 88 | + char alarmBuffer[25]; |
| 89 | + sprintf(alarmBuffer, "2020-%02d-%02d %02d:%02d:%02d.%03d", |
| 90 | + myRTC.alarmMonth, myRTC.alarmDayOfMonth, |
| 91 | + myRTC.alarmHour, myRTC.alarmMinute, |
| 92 | + myRTC.alarmSeconds, myRTC.alarmHundredths); |
| 93 | + Serial.println(alarmBuffer); |
| 94 | +} |
| 95 | + |
| 96 | +// Interrupt handler for the RTC |
| 97 | +extern "C" void am_rtc_isr(void) |
| 98 | +{ |
| 99 | + // Clear the RTC alarm interrupt. |
| 100 | + am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM); |
| 101 | + |
| 102 | + // Set alarm flag |
| 103 | + alarmFlag = true; |
| 104 | +} |
0 commit comments