|
| 1 | +// Arduino DS3232RTC Library |
| 2 | +// https://github.com/JChristensen/DS3232RTC |
| 3 | +// Copyright (C) 2022 by Jack Christensen and licensed under |
| 4 | +// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html |
| 5 | +// |
| 6 | +// Example sketch to create an interrupt with Alarm 1 |
| 7 | +// at regular intervals. |
| 8 | +// Assumes the RTC is already set and running. |
| 9 | +// |
| 10 | +// Notes: |
| 11 | +// Using the INT/SQW pin for alarms is mutually exclusive with using |
| 12 | +// it to output a square wave. However, alarms can still be set when |
| 13 | +// a square wave is output, but then the alarm() function will need |
| 14 | +// to be used to determine if an alarm has triggered. Even though |
| 15 | +// the DS3231 power-on default for the INT/SQW pin is as an alarm |
| 16 | +// output, it's good practice to call DS3232RTC::squareWave(SQWAVE_NONE) |
| 17 | +// before setting alarms. |
| 18 | +// |
| 19 | +// I recommend calling DS3232RTC::alarm() before DS3232RTC::alarmInterrupt() |
| 20 | +// to ensure the RTC's alarm flag is cleared. |
| 21 | +// |
| 22 | +// Tested with Arduino 1.8.19, Arduino Uno, DS3231. |
| 23 | +// Connect RTC SDA to Arduino pin A4. |
| 24 | +// Connect RTC SCL to Arduino pin A5. |
| 25 | +// Connect RTC INT/SQW to Arduino pin 3. |
| 26 | +// |
| 27 | +// Jack Christensen 01Apr2022 |
| 28 | + |
| 29 | +#include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC |
| 30 | +#include <Streaming.h> // https://github.com/janelia-arduino/Streaming |
| 31 | + |
| 32 | +constexpr uint8_t sqwPin{3}; // RTC provides an interrupt signal on this pin |
| 33 | + // (Can use Pin 2 (INT0) or Pin 3 (INT1) with Arduino Uno) |
| 34 | +constexpr time_t alarmInterval{60}; // alarm interval in seconds |
| 35 | +DS3232RTC myRTC; |
| 36 | + |
| 37 | +void setup() |
| 38 | +{ |
| 39 | + Serial.begin(115200); |
| 40 | + Serial << F( "\n" __FILE__ "\n" __DATE__ " " __TIME__ "\n\n" ); |
| 41 | + |
| 42 | + // initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags |
| 43 | + myRTC.begin(); |
| 44 | + myRTC.setAlarm(DS3232RTC::ALM1_MATCH_DATE, 0, 0, 0, 1); |
| 45 | + myRTC.setAlarm(DS3232RTC::ALM2_MATCH_DATE, 0, 0, 0, 1); |
| 46 | + myRTC.alarm(DS3232RTC::ALARM_1); |
| 47 | + myRTC.alarm(DS3232RTC::ALARM_2); |
| 48 | + myRTC.alarmInterrupt(DS3232RTC::ALARM_1, false); |
| 49 | + myRTC.alarmInterrupt(DS3232RTC::ALARM_2, false); |
| 50 | + myRTC.squareWave(DS3232RTC::SQWAVE_NONE); |
| 51 | + |
| 52 | + // configure an interrupt on the falling edge from the SQW pin |
| 53 | + pinMode(sqwPin, INPUT_PULLUP); |
| 54 | + attachInterrupt(digitalPinToInterrupt(sqwPin), alarmIsr, FALLING); |
| 55 | + |
| 56 | + Serial << "Alarm interval is " << alarmInterval << " seconds\n"; |
| 57 | + time_t t = myRTC.get(); |
| 58 | + time_t a = t + alarmInterval - t % alarmInterval; |
| 59 | + if (a <= t) a += alarmInterval; |
| 60 | + // set the alarm |
| 61 | + myRTC.setAlarm(DS3232RTC::ALM1_MATCH_HOURS, second(a), minute(a), hour(a), 0); |
| 62 | + myRTC.alarm(DS3232RTC::ALARM_1); // clear the alarm flag |
| 63 | + myRTC.alarmInterrupt(DS3232RTC::ALARM_1, true); |
| 64 | + printDateTime(t); |
| 65 | + Serial << " Current RTC time\n"; |
| 66 | + printDateTime(a); |
| 67 | + Serial << " Alarm1 set time\n"; |
| 68 | +} |
| 69 | + |
| 70 | +volatile bool alarmFlag{false}; |
| 71 | + |
| 72 | +void alarmIsr() |
| 73 | +{ |
| 74 | + alarmFlag = true; |
| 75 | +} |
| 76 | + |
| 77 | +void loop() |
| 78 | +{ |
| 79 | + if (alarmFlag) { |
| 80 | + alarmFlag = false; |
| 81 | + time_t t = myRTC.get(); |
| 82 | + time_t a = t + alarmInterval; |
| 83 | + // set the alarm |
| 84 | + myRTC.setAlarm(DS3232RTC::ALM1_MATCH_HOURS, second(a), minute(a), hour(a), 0); |
| 85 | + myRTC.alarm(DS3232RTC::ALARM_1); // clear the alarm flag |
| 86 | + //myRTC.alarmInterrupt(DS3232RTC::ALARM_1, true); |
| 87 | + printDateTime(t); |
| 88 | + Serial << " Alarm1 interrupt occurred\n"; |
| 89 | + printDateTime(a); |
| 90 | + Serial << " Alarm1 set time\n"; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void printDateTime(time_t t) |
| 95 | +{ |
| 96 | + Serial << ((day(t)<10) ? "0" : "") << _DEC(day(t)); |
| 97 | + Serial << monthShortStr(month(t)) << _DEC(year(t)) << ' '; |
| 98 | + Serial << ((hour(t)<10) ? "0" : "") << _DEC(hour(t)) << ':'; |
| 99 | + Serial << ((minute(t)<10) ? "0" : "") << _DEC(minute(t)) << ':'; |
| 100 | + Serial << ((second(t)<10) ? "0" : "") << _DEC(second(t)); |
| 101 | +} |
0 commit comments