Skip to content

Commit a8bec10

Browse files
committed
Added support for Mode 0 and Mode 1
Adds support for Mode 0 (32-bit counter) and Mode 1 (16-bit periodic counter). Mode 1 can operate with one or two compare values and optionally a set period. Adds options to reset on match and interrupt on overflow as well as a helper function to get the source of RTC interrupts and enums for prescale values and interrupt sources. Includes examples for Mode 0 with reset on match, Mode 1 with two compares and a set period and Mode 1 with interrupt on overflow enabled (along with two compares).
1 parent d8503ff commit a8bec10

File tree

7 files changed

+478
-44
lines changed

7 files changed

+478
-44
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
RTC counter for SAMD21 controllers (Arduino Zero, MKR1000, Adafruit Feather M0)
3+
4+
Demonstrates the use of the RTC library Mode 0 (32-bit counter) for SAMD21 controllers
5+
This example starts a counter that interrupts and resets after 10 seconds* since
6+
"reset on match" is set to true.
7+
8+
(*seconds, based on GCLK_RTC = 1.024khz and default 1/1024 prescaler)
9+
10+
This example code is in the public domain
11+
12+
created by A. McMahon
13+
20 Feb 2020
14+
*/
15+
16+
#include <RTCZero.h>
17+
18+
/* Create an rtc object */
19+
RTCZero rtc;
20+
21+
void countDone(void);
22+
23+
void setup()
24+
{
25+
Serial.begin(9600);
26+
27+
rtc.begin(true, 0, true); // initialize RTC: reset starting value, Mode 0 (32-bit counter), reset on match
28+
rtc.enableCounter(10); // set counter compare value
29+
rtc.attachInterrupt(countDone); // attach interrupt
30+
}
31+
32+
void loop()
33+
{
34+
Serial.println(rtc.getCount()); // print the current count
35+
delay(1000);
36+
}
37+
38+
void countDone() // interrupt when compare value is reached
39+
{
40+
Serial.println("Reset!");
41+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
RTC counter for SAMD21 controllers (Arduino Zero, MKR1000, Adafruit Feather M0)
3+
4+
Demonstrates the use of the RTC library Mode 1 (16-bit periodic counter) for SAMD21 controllers
5+
This example starts a counter that interrupts at 10 and 30 seconds* (for the compare values).
6+
The count is then reset after 60 since that is the set counter period.
7+
8+
(*seconds, based on GCLK_RTC = 1.024khz and default 1/1024 prescaler)
9+
10+
This example code is in the public domain
11+
12+
created by A. McMahon
13+
20 Feb 2020
14+
*/
15+
16+
#include "RTCZero.h"
17+
18+
/* Create an rtc object */
19+
RTCZero rtc;
20+
21+
void countInt(void);
22+
23+
void setup()
24+
{
25+
Serial.begin(9600);
26+
27+
rtc.begin(true, 1); // initialize RTC: reset starting value, Mode 1 (16-bit counter)
28+
rtc.enableCounter(10, 30); // set counter compare values (interrupt at 10 and 30)
29+
rtc.setPeriod(60); // set counter period
30+
rtc.attachInterrupt(countInt); // attach interrupt
31+
}
32+
33+
void loop()
34+
{
35+
Serial.println(rtc.getCount()); // print the current count
36+
delay(1000);
37+
}
38+
39+
void countInt() // interrupt when compare value is reached
40+
{
41+
uint8_t source;
42+
source = rtc.getIntSource(); // check what caused the interrupt
43+
44+
if (source == rtc.INT_COMP0) Serial.println("Count = Compare 0!");
45+
if (source == rtc.INT_COMP1) Serial.println("Count = Compare 1!");
46+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
RTC counter for SAMD21 controllers (Arduino Zero, MKR1000, Adafruit Feather M0)
3+
4+
Demonstrates the use of the RTC library Mode 1 (16-bit periodic counter) for SAMD21 controllers
5+
This example starts a counter that interrupts at 10 and 30 seconds* (for the compare values)
6+
and at 60 (since the overflow interrupt is also enabled). The count is then reset after 60
7+
since that is the set counter period.
8+
9+
(*seconds, based on GCLK_RTC = 1.024khz and default 1/1024 prescaler)
10+
11+
This example code is in the public domain
12+
13+
created by A. McMahon
14+
20 Feb 2020
15+
*/
16+
17+
#include "RTCZero.h"
18+
19+
/* Create an rtc object */
20+
RTCZero rtc;
21+
22+
void countInt(void);
23+
24+
void setup()
25+
{
26+
Serial.begin(9600);
27+
28+
rtc.begin(true, 1); // initialize RTC: reset starting value, Mode 1 (16-bit counter)
29+
rtc.enableCounter(10, 30); // set counter compare values (interrupt at 10 and 30)
30+
rtc.setPeriod(60); // set counter period
31+
rtc.enableOverflow(); // enable interrupt on overflow
32+
rtc.attachInterrupt(countInt); // attach interrupt
33+
}
34+
35+
void loop()
36+
{
37+
Serial.println(rtc.getCount()); // print the current count
38+
delay(1000);
39+
}
40+
41+
void countInt() // interrupt when compare value is reached
42+
{
43+
uint8_t source;
44+
source = rtc.getIntSource(); // check what caused the interrupt
45+
46+
if (source == rtc.INT_COMP0) Serial.println("Count = Compare 0!");
47+
if (source == rtc.INT_COMP1) Serial.println("Count = Compare 1!");
48+
if (source == rtc.INT_OVERFLOW) Serial.println("Overflow!");
49+
}

keywords.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ setAlarmTime KEYWORD2
5252
enableAlarm KEYWORD2
5353
disableAlarm KEYWORD2
5454

55+
enableCounter KEYWORD2
56+
disableCounter KEYWORD2
57+
58+
enableOverflow KEYWORD2
59+
disableOverflow KEYWORD2
60+
61+
getIntSource KEYWORD2
62+
getCount KEYWORD2
63+
getCompare KEYWORD2
64+
65+
setCount KEYWORD2
66+
setPeriod KEYWORD2
67+
5568
standbyMode KEYWORD2
5669

5770
#######################################

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=RTCZero
2-
version=1.6.0
2+
version=2.0.0
33
author=Arduino
44
maintainer=Arduino <info@arduino.cc>
55
sentence=Allows to use the RTC functionalities. For Arduino Zero, MKRZero and MKR1000 only.

0 commit comments

Comments
 (0)