Skip to content

Commit 877ac2d

Browse files
committed
Create Example2_Set_Alarms.ino
1 parent ca602fe commit 877ac2d

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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. The code is
7+
configured so that the RTC alarm will trigger every minute. The RTC interrupt
8+
service routine will set an alarm flag each time the alarm triggers and the
9+
RTC date and time will printed to the Serial Monitor.
10+
*/
11+
12+
#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core
13+
APM3_RTC myRTC; // Create instance of RTC class
14+
15+
volatile bool alarmFlag = false;
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println("SparkFun RTC Set Alarm Example");
21+
22+
// Easily set RTC using the system __DATE__ and __TIME__ macros from compiler
23+
//myRTC.setToCompilerTime();
24+
25+
// Manually set RTC date and time
26+
myRTC.setTime(0, 50, 59, 12, 3, 6, 20); // 12:59:50.000, June 3rd, 2020 (hund, ss, mm, hh, dd, mm, yy)
27+
28+
// Set the RTC's alarm
29+
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
30+
31+
// Set the RTC alarm mode
32+
/*
33+
0: Alarm interrupt disabled
34+
1: Alarm match every year
35+
2: Alarm match every month
36+
3: Alarm match every week
37+
4: Alarm match every day
38+
5: Alarm match every hour
39+
6: Alarm match every minute
40+
7: Alarm match every second
41+
*/
42+
myRTC.setAlarmMode(6); // Set the RTC alarm to match on minutes rollover
43+
myRTC.attachInterrupt(); // Attach RTC alarm interrupt
44+
45+
// Print the RTC's alarm date and time
46+
Serial.print("Next alarm: "); printAlarm();
47+
}
48+
49+
void loop()
50+
{
51+
// Check if alarm flag was set
52+
if (alarmFlag == true)
53+
{
54+
// Print date and time of RTC alarm trigger
55+
Serial.print("Alarm triggered: "); printDateTime();
56+
57+
// Clear alarm flag
58+
alarmFlag = false;
59+
}
60+
61+
// Print RTC's date and time while waiting for alarm
62+
printDateTime();
63+
delay(1000);
64+
}
65+
66+
// Print the RTC's current date and time
67+
void printDateTime()
68+
{
69+
myRTC.getTime();
70+
char dateTimeBuffer[25];
71+
sprintf(dateTimeBuffer, "20%02d-%02d-%02d %02d:%02d:%02d.%03d",
72+
myRTC.year, myRTC.month, myRTC.dayOfMonth,
73+
myRTC.hour, myRTC.minute, myRTC.seconds, myRTC.hundredths);
74+
Serial.println(dateTimeBuffer);
75+
}
76+
77+
// Print the RTC's alarm
78+
void printAlarm()
79+
{
80+
myRTC.getAlarm();
81+
char alarmBuffer[25];
82+
sprintf(alarmBuffer, "2020-%02d-%02d %02d:%02d:%02d.%03d",
83+
myRTC.alarmMonth, myRTC.alarmDayOfMonth,
84+
myRTC.alarmHour, myRTC.alarmMinute,
85+
myRTC.alarmSeconds, myRTC.alarmHundredths);
86+
Serial.println(alarmBuffer);
87+
}
88+
89+
// Interrupt handler for the RTC
90+
extern "C" void am_rtc_isr(void)
91+
{
92+
// Clear the RTC alarm interrupt.
93+
am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM);
94+
95+
// Set alarm flag
96+
alarmFlag = true;
97+
}

0 commit comments

Comments
 (0)