Skip to content

Commit 6ef5658

Browse files
committed
Add a function to check an alarm flag without resetting it,
and another function to reset an alarm flag.
1 parent 261ca7d commit 6ef5658

File tree

6 files changed

+184
-10
lines changed

6 files changed

+184
-10
lines changed

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ Tests whether an alarm has been triggered. If the alarm was triggered, returns t
311311
##### Parameters
312312
**alarmNumber:** The number of the alarm to test, ALARM_1 or ALARM_2 *(byte)*
313313
##### Returns
314-
Description *(type)*
314+
Value of the alarm flag bit *(bool)*
315315
##### Example
316316
```c++
317317
if ( RTC.alarm(ALARM_1) ) { //has Alarm1 triggered?
@@ -322,6 +322,44 @@ else {
322322
}
323323
```
324324

325+
### checkAlarm(byte alarmNumber)
326+
##### Description
327+
Tests whether an alarm has been triggered. If the alarm was triggered, returns true, else returns false. The alarm flag is not reset.
328+
##### Syntax
329+
`RTC.checkAlarm(alarmNumber);`
330+
##### Parameters
331+
**alarmNumber:** The number of the alarm to test, ALARM_1 or ALARM_2 *(byte)*
332+
##### Returns
333+
Value of the alarm flag bit *(bool)*
334+
##### Example
335+
```c++
336+
if ( RTC.checkAlarm(ALARM_1) ) { //has Alarm1 triggered?
337+
//yes, act on the alarm
338+
}
339+
else {
340+
//no alarm
341+
}
342+
```
343+
### clearAlarm(byte alarmNumber)
344+
##### Description
345+
Clears the given alarm flag bit.
346+
##### Syntax
347+
`RTC.clearAlarm(alarmNumber);`
348+
##### Parameters
349+
**alarmNumber:** The number of the alarm to test, ALARM_1 or ALARM_2 *(byte)*
350+
##### Returns
351+
Value of the alarm flag bit, before it was cleared *(bool)*
352+
##### Example
353+
```c++
354+
if ( RTC.checkAlarm(ALARM_1) ) { //has Alarm1 triggered?
355+
//yes, act on the alarm
356+
RTC.clearAlarm(ALARM_1); //clear the alarm flag
357+
}
358+
else {
359+
//no alarm
360+
}
361+
```
362+
325363
## Other functions
326364
### temperature(void)
327365
##### Description

examples/alarm_ex9/alarm_ex9.ino

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Arduino DS3232RTC Library
2+
// https://github.com/JChristensen/DS3232RTC
3+
// Copyright (C) 2020 by Jack Christensen and licensed under
4+
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
5+
//
6+
// DS3231/DS3232 Alarm Example Sketch #9
7+
//
8+
// Use Alarm 1 to turn on an LED at a given time, and Alarm 2 to turn
9+
// it off at another time.
10+
// Detect the alarms by polling the alarm flags.
11+
// Assumes the RTC time is already set.
12+
//
13+
// Hardware:
14+
// Arduino Uno, DS3231 RTC.
15+
// Connect RTC SDA to Arduino pin A4.
16+
// Connect RTC SCL to Arduino pin A5.
17+
// Connect a push button from Arduino pin 3 to ground.
18+
// Holding the button down during a reset or power on
19+
// will cause the alarm times to be set. This allows
20+
// us to test for the correct state after a reset or
21+
// power cycle.
22+
//
23+
// Jack Christensen 06Sep2020
24+
25+
#include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC
26+
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
27+
28+
// pin definitions
29+
const uint8_t BUTTON_PIN(3);
30+
31+
// current time from the RTC in text format
32+
char timestamp[32];
33+
34+
void setup()
35+
{
36+
Serial.begin(115200);
37+
delay(1000);
38+
Serial << F( "\n" __FILE__ " " __DATE__ " " __TIME__ "\n" );
39+
pinMode(LED_BUILTIN, OUTPUT);
40+
pinMode(BUTTON_PIN, INPUT_PULLUP);
41+
42+
// print the current time
43+
time_t t = RTC.get();
44+
formatTime(timestamp, t);
45+
Serial << millis() << F(" Current RTC time ") << timestamp << endl;
46+
47+
// set the alarms if the button is pushed
48+
if (!digitalRead(BUTTON_PIN)) {
49+
// initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags
50+
RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1);
51+
RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1);
52+
RTC.alarm(ALARM_1);
53+
RTC.alarm(ALARM_2);
54+
RTC.alarmInterrupt(ALARM_1, false);
55+
RTC.alarmInterrupt(ALARM_2, false);
56+
RTC.squareWave(SQWAVE_NONE);
57+
58+
// set alarm 1 to occur in 1-2 minutes
59+
// set alarm 2 to occur 5 minutes after alarm 1
60+
tmElements_t tm;
61+
breakTime(t + 120, tm);
62+
tm.Second = 0;
63+
time_t a1 = makeTime(tm);
64+
time_t a2 = a1 + 300;
65+
formatTime(timestamp, a1);
66+
timestamp[8] = 0; // keep just hh:mm:ss
67+
Serial << millis() << F(" Alarm 1 set to ") << timestamp << endl;
68+
formatTime(timestamp, a2);
69+
timestamp[5] = 0; // keep just hh:mm
70+
Serial << millis() << F(" Alarm 2 set to ") << timestamp << endl;
71+
RTC.setAlarm(ALM1_MATCH_HOURS, minute(a1), hour(a1), 1);
72+
RTC.alarm(ALARM_1); // ensure alarm flag is cleared
73+
RTC.setAlarm(ALM2_MATCH_HOURS, minute(a2), hour(a2), 1);
74+
RTC.alarm(ALARM_2); // ensure alarm flag is cleared
75+
}
76+
}
77+
78+
void loop()
79+
{
80+
enum states_t {LED_OFF, LED_ON};
81+
static states_t state(LED_OFF);
82+
83+
switch (state) {
84+
case LED_OFF:
85+
if (RTC.checkAlarm(ALARM_1)) { // time to turn on?
86+
state = LED_ON;
87+
RTC.clearAlarm(ALARM_2);
88+
digitalWrite(LED_BUILTIN, HIGH);
89+
formatTime(timestamp, RTC.get()); // get current RTC time
90+
Serial << millis() << F(" Alarm 1 at ") << timestamp << endl;
91+
}
92+
break;
93+
94+
case LED_ON:
95+
if (RTC.checkAlarm(ALARM_2)) { // time to turn on?
96+
state = LED_OFF;
97+
RTC.clearAlarm(ALARM_1);
98+
digitalWrite(LED_BUILTIN, LOW);
99+
formatTime(timestamp, RTC.get()); // get current RTC time
100+
Serial << millis() << F(" Alarm 2 at ") << timestamp << endl;
101+
}
102+
break;
103+
}
104+
}
105+
106+
// format a time_t value, return the formatted string in buf (must be at least 25 bytes)
107+
void formatTime(char *buf, time_t t)
108+
{
109+
char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
110+
strcpy(m, monthShortStr(month(t)));
111+
sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d",
112+
hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t));
113+
}

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ readRTC KEYWORD2
99
setAlarm KEYWORD2
1010
alarmInterrupt KEYWORD2
1111
alarm KEYWORD2
12+
checkAlarm KEYWORD2
13+
clearAlarm KEYWORD2
1214
squareWave KEYWORD2
1315
oscStopped KEYWORD2
1416
temperature KEYWORD2

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=DS3232RTC
2-
version=1.2.12
2+
version=1.3.0
33
author=Jack Christensen <jack.christensen@outlook.com>
44
maintainer=Jack Christensen <jack.christensen@outlook.com>
55
sentence=Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks.

src/DS3232RTC.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,22 +296,41 @@ void DS3232RTC::alarmInterrupt(byte alarmNumber, bool interruptEnabled)
296296
// triggered, and resets the alarm flag bit.
297297
bool DS3232RTC::alarm(byte alarmNumber)
298298
{
299-
uint8_t statusReg, mask;
300-
301-
statusReg = readRTC(RTC_STATUS);
302-
mask = _BV(A1F) << (alarmNumber - 1);
303-
if (statusReg & mask)
304-
{
299+
uint8_t statusReg = readRTC(RTC_STATUS);
300+
uint8_t mask = _BV(A1F) << (alarmNumber - 1);
301+
if (statusReg & mask) {
305302
statusReg &= ~mask;
306303
writeRTC(RTC_STATUS, statusReg);
307304
return true;
308305
}
309-
else
310-
{
306+
else {
311307
return false;
312308
}
313309
}
314310

311+
// Returns true or false depending on whether the given alarm has been
312+
// triggered, without resetting the alarm flag bit.
313+
bool DS3232RTC::checkAlarm(byte alarmNumber)
314+
{
315+
uint8_t statusReg = readRTC(RTC_STATUS);
316+
uint8_t mask = _BV(A1F) << (alarmNumber - 1);
317+
return (statusReg & mask);
318+
}
319+
320+
// Clears the given alarm flag bit if it is set.
321+
// Returns the value of the flag bit before if was cleared.
322+
bool DS3232RTC::clearAlarm(byte alarmNumber)
323+
{
324+
uint8_t statusReg = readRTC(RTC_STATUS);
325+
uint8_t mask = _BV(A1F) << (alarmNumber - 1);
326+
bool retVal = statusReg & mask;
327+
if (retVal) {
328+
statusReg &= ~mask;
329+
writeRTC(RTC_STATUS, statusReg);
330+
}
331+
return retVal;
332+
}
333+
315334
// Enable or disable the square wave output.
316335
// Use a value from the SQWAVE_FREQS_t enumeration for the parameter.
317336
void DS3232RTC::squareWave(SQWAVE_FREQS_t freq)

src/DS3232RTC.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class DS3232RTC
6868
void setAlarm(ALARM_TYPES_t alarmType, byte minutes, byte hours, byte daydate);
6969
void alarmInterrupt(byte alarmNumber, bool alarmEnabled);
7070
bool alarm(byte alarmNumber);
71+
bool checkAlarm(byte alarmNumber);
72+
bool clearAlarm(byte alarmNumber);
7173
void squareWave(SQWAVE_FREQS_t freq);
7274
bool oscStopped(bool clearOSF = false);
7375
int16_t temperature();

0 commit comments

Comments
 (0)