Skip to content

Commit 8dbc5d1

Browse files
author
Owen
authored
Merge pull request #170 from adamgarbo/rtcAlarms
2 parents 4d4db9c + 0d0b549 commit 8dbc5d1

File tree

11 files changed

+587
-134
lines changed

11 files changed

+587
-134
lines changed

libraries/RTC/examples/Example1_getTime/Example1_getTime.ino renamed to libraries/RTC/examples/Example1_Get_Time/Example1_Get_Time.ino

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
/* Author: Nathan Seidle
1+
/*
2+
Author: Nathan Seidle
23
Created: Septempter 27th, 2019
34
License: MIT. See SparkFun Arduino Apollo3 Project for more information
45
5-
This example demonstrates how to initialize and read from the on board RTC.
6+
This example demonstrates how to initialize and read from the on-board RTC.
7+
68
Most SparkFun Artemis boards have the necessary external 32kHz crystal to
79
enable the RTC. If you are using the Artemis module bare you will either
810
need an external 32kHz xtal or use the internal LFRC. Read the datasheet
@@ -11,16 +13,19 @@
1113
This example is based on the Ambiq SDK EVB2 RTC example.
1214
*/
1315

14-
#include "RTC.h" //Include RTC library included with the Aruino_Apollo3 core
15-
APM3_RTC myRTC; //Create instance of RTC class
16+
#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core
17+
APM3_RTC myRTC; // Create instance of RTC class
1618

1719
void setup()
1820
{
1921
Serial.begin(115200);
2022
Serial.println("SparkFun RTC Example");
2123

22-
myRTC.setToCompilerTime(); //Easily set RTC using the system __DATE__ and __TIME__ macros from compiler
23-
//myRTC.setTime(7, 28, 51, 0, 21, 10, 15); //Manually set RTC back to the future: Oct 21st, 2015 at 7:28.51 AM
24+
// Easily set RTC using the system __DATE__ and __TIME__ macros from compiler
25+
myRTC.setToCompilerTime();
26+
27+
// Manually set RTC date and time
28+
//myRTC.setTime(12, 59, 50, 0, 3, 6, 20); // 12:59:50.000, June 3rd, 2020 (hund, ss, mm, hh, dd, mm, yy)
2429
}
2530

2631
void loop()

libraries/RTC/examples/Example2_RTCwithSleep/Example2_RTCwithSleep.ino

-81
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

libraries/RTC/examples/Example3_TestRTC/Example3_TestRTC.ino renamed to libraries/RTC/examples/Example3_Test_RTC/Example3_Test_RTC.ino

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
/* Author: Nathan Seidle and stephenf7072
1+
/*
2+
Author: Nathan Seidle and stephenf7072
23
Created: January 28th, 2020
34
License: MIT. See SparkFun Arduino Apollo3 Project for more information
45
56
This example test the internal HAL to make sure the days advance correctly.
67
*/
78

89
#include "RTC.h"
9-
APM3_RTC myRTC; //Create instance of RTC class
10+
APM3_RTC myRTC; // Create instance of RTC class
1011

11-
int previousDay = 1;
12+
int previousDay;
1213

1314
void setup()
1415
{
1516
Serial.begin(115200);
1617
delay(10);
17-
Serial.println("Artemis RTC testing");
18+
Serial.println("Artemis RTC Testing");
1819

19-
//myRTC.setTime(hh, mm, ss, hund, dd, mm, yy);
20-
myRTC.setTime(23, 59, 59, 99, 1, 1, 19); //Manually set RTC to 1s before midnight
20+
// Manually set RTC date and time to the start of 2020 so that myRTC contains valid times
21+
myRTC.setTime(23, 59, 59, 0, 1, 1, 20); // Set to 1 second before midnight Jan 1
2122
}
2223

2324
void loop()
2425
{
25-
printArtemisTime();
26+
myRTC.setTime(23, 59, 59, 99, myRTC.dayOfMonth, myRTC.month, myRTC.year); // Manually set RTC 1/100th of a second from the next day
27+
previousDay = myRTC.weekday;
28+
delay(11); //Allow us to roll from midnight the night before to the new day
2629

27-
myRTC.getTime();
28-
myRTC.setTime(23, 59, 59, 99, myRTC.dayOfMonth, myRTC.month, myRTC.year); //Manually set RTC
29-
delay(11); //Allow us to roll from midnight the night before to the new day
30+
printArtemisTime();
3031
}
3132

3233
void printArtemisTime()
@@ -76,7 +77,5 @@ void printArtemisTime()
7677
;
7778
}
7879

79-
previousDay = myRTC.weekday;
80-
8180
Serial.println();
8281
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 set the RTC using UNIX Epoch time.
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+
void setup()
13+
{
14+
Serial.begin(115200);
15+
Serial.println("SparkFun RTC Set UNIX Epoch Time Example");
16+
17+
// Set the RTC time using UNIX Epoch time
18+
myRTC.setEpoch(1591185600); // E.g. 12:00:00, June 3rd, 2020
19+
}
20+
21+
void loop()
22+
{
23+
// Print UNIX Epoch timestamp
24+
Serial.print("Epoch time: "); Serial.println(myRTC.getEpoch());
25+
26+
// Print RTC's date and time
27+
Serial.print("Timestamp: "); printDateTime();
28+
29+
delay(1000);
30+
}
31+
32+
// Print the RTC's current date and time
33+
void printDateTime()
34+
{
35+
myRTC.getTime();
36+
char dateTime[20];
37+
sprintf(dateTime, "20%02d-%02d-%02d %02d:%02d:%02d",
38+
myRTC.year, myRTC.month, myRTC.dayOfMonth,
39+
myRTC.hour, myRTC.minute, myRTC.seconds);
40+
Serial.println(dateTime);
41+
}

0 commit comments

Comments
 (0)