Skip to content

Commit bad7234

Browse files
committed
Create Example5_Set_Alarms.ino
1 parent 2cf56bf commit bad7234

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 RTC alarms.
7+
8+
Most SparkFun Artemis boards have the necessary external 32kHz crystal to
9+
enable the RTC. If you are using the Artemis module bare you will either
10+
need an external 32kHz xtal or use the internal LFRC. Read the datasheet
11+
section 12.1 for more information.
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+
19+
void setup()
20+
{
21+
Serial.begin(115200);
22+
Serial.println("SparkFun RTC Set Alarm Example");
23+
24+
//myRTC.setToCompilerTime(); // Easily set RTC using the system __DATE__ and __TIME__ macros from compiler
25+
myRTC.setTime(0, 50, 59, 12, 23, 5, 20); // Manually set RTC date and time (hund, ss, mm, hh, dd, mm, yy)
26+
27+
Serial.print("Initial alarm: "); printAlarm(); // Initial alarm should read all zeroes
28+
29+
// Set the RTC's alarm
30+
myRTC.setAlarm(0, 0, 0, 13, 23, 5); // Set alarm (hund, ss, mm, hh, dd, mm). Note: No year alarm register
31+
32+
// Set the RTC alarm mode
33+
/*
34+
0: Alarm interrupt disabled
35+
1: Alarm match every year
36+
2: Alarm match every month
37+
3: Alarm match every week
38+
4: Alarm match every day
39+
5: Alarm match every hour
40+
6: Alarm match every minute
41+
7: Alarm match every second
42+
8: Alarm match every 10th second
43+
9: Alarm match every 100th second
44+
*/
45+
myRTC.setAlarmMode(6); // Set the RTC alarm to match on minutes rollover
46+
myRTC.attachInterrupt(); // Attach RTC alarm interrupt
47+
48+
Serial.print("Next alarm: "); printAlarm(); // Print current alarm date and time
49+
}
50+
51+
void loop()
52+
{
53+
if (alarmFlag == true)
54+
{
55+
Serial.print("Alarm triggered: "); printDateTime();
56+
alarmFlag = false;
57+
}
58+
59+
printDateTime();
60+
delay(1000);
61+
}
62+
63+
// Print the RTC's current date and time
64+
void printDateTime()
65+
{
66+
myRTC.getTime();
67+
char dateTimeBuffer[25];
68+
sprintf(dateTimeBuffer, "20%02d-%02d-%02d %02d:%02d:%02d.%03d",
69+
myRTC.year, myRTC.month, myRTC.dayOfMonth,
70+
myRTC.hour, myRTC.minute, myRTC.seconds, myRTC.hundredths);
71+
Serial.println(dateTimeBuffer);
72+
}
73+
74+
// Print the RTC's alarm
75+
void printAlarm()
76+
{
77+
myRTC.getAlarm();
78+
char alarmBuffer[25];
79+
sprintf(alarmBuffer, "2020-%02d-%02d %02d:%02d:%02d.%03d",
80+
myRTC.alarmMonth, myRTC.alarmDayOfMonth,
81+
myRTC.alarmHour, myRTC.alarmMinute,
82+
myRTC.alarmSeconds, myRTC.alarmHundredths);
83+
Serial.println(alarmBuffer);
84+
}
85+
86+
// Interrupt handler for the RTC
87+
extern "C" void am_rtc_isr(void)
88+
{
89+
// Clear the RTC alarm interrupt.
90+
am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM);
91+
92+
// Set alarm flag
93+
alarmFlag = true;
94+
}

0 commit comments

Comments
 (0)