Browse by type
https://github.com/JChristensen/DS3232RTC
README file
Jack Christensen
Mar 2013
Arduino DS3232RTC Library Copyright (C) 2018 Jack Christensen GNU GPL v3.0
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License v3.0 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/gpl.html
Version 3.0.0 adds the ability to address different I2C peripherals for microcontrollers that have more than one (e.g. Wire, Wire1, etc.) There is one situation that is not backwards compatible with earlier versions of the library. Code that calls setSyncProvider() in the Time library, e.g.:
setSyncProvider(myRTC.get);
Needs to change as follows:
setSyncProvider([](){return myRTC.get();});
This uses a lambda to provide the Time library with a static function, which it requires. The MCP79412::get() function was static in previous versions of the library, but no longer is in v3.0.0. This allows the flexibility to operate with different I2C peripherals.
The 2.0.0 version of the library has some significant changes and is not completely backwards compatible with earlier versions. These changes provide a more consistent API and reduce the possibility of name collisions. While sketches using this library will likely require changes as a result, these should be mostly straightforward.
DS3232RTC object, therefore each sketch needs to define one. (Previous versions of the library defined a DS3232RTC object named RTC, although only for AVR architecture. Consider using a name other than RTC as this can cause a name collision on some architectures.)DS3232RTC::begin() in the setup() function or elsewhere as appropriate.myRTC.alarm(DS3232RTC::ALARM_1);DS32_ (see the header file.)DS3232RTC is an Arduino library that supports the Maxim Integrated DS3231 and DS3232 Real-Time Clocks. This library is intended to be used with PJRC's Arduino Time library.
The DS3232RTC library also implements functions to support the additional features of the DS3232 and DS3231. The DS3232 has the same features as the DS3231, but the DS3232 also has these extra features:
The following example sketches are included with the DS3232RTC library:
Wire1.When using the DS3232RTC library, the user is responsible for ensuring that reads and writes do not exceed the device's address space (0x00-0x12 for DS3231, 0x00-0xFF for DS3232); no bounds checking is done by the library.
Symbolic names used with the setAlarm() function (described below).
Symbolic names used with alarm functions.
Symbolic names used with the squareWave() function (described below).
Instantiates a DS3232RTC object.
DS3232RTC myRTC(wire);
wire: An optional parameter to specify which I2C bus to use. If omitted, defaults to Wire. (TwoWire&).
None.
DS3232RTC myRTC; // to use Wire
// or
DS3232RTC myRTC(Wire1); // to use Wire1
Initializes the I2C bus. Calls Wire.begin().
begin();
None.
None.
DS3232RTC myRTC;
void setup() {
myRTC.begin();
}
Reads the current date and time from the RTC and returns it as a time_t value. Returns zero if an I2C error occurs (RTC not present, etc.).
myRTC.get();
None.
Current date and time (time_t)
time_t myTime = myRTC.get();
Sets the RTC date and time to the given time_t value. Clears the oscillator stop flag (OSF) bit in the control/status register. See the oscStopped() function and also the DS323x datasheet for more information on the OSF bit.
myRTC.set(t);
t: The date and time to set the RTC to (time_t)
I2C status (uint8_t). Returns zero if successful.
// this example first sets the system time (maintained by the Time library) to
// a hard-coded date and time, and then sets the RTC from the system time.
// the setTime() function is part of the Time library.
setTime(23, 31, 30, 13, 2, 2009); // set the system time to 23h31m30s on 13Feb2009
myRTC.set(now()); // set the RTC from the system time
Reads the current date and time from the RTC and returns it as a tmElements_t structure. See the Arduino Time Library for details on the tmElements_t structure.
myRTC.read(tm);
tm: Address of a tmElements_t structure to which the date and time are returned.
I2C status (uint8_t). Returns zero if successful. The date and time read from the RTC are returned to the tm parameter.
tmElements_t tm;
myRTC.read(tm);
Serial.print(tm.Hour, DEC);
Serial.print(':');
Serial.print(tm.Minute,DEC);
Serial.print(':');
Serial.println(tm.Second,DEC);
Sets the RTC to the date and time given by a tmElements_t structure. Clears the oscillator stop flag (OSF) bit in the control/status register. See the oscStopped() function and also the DS323x datasheet for more information on the OSF bit.
myRTC.write(tm);
tm: Address of a tmElements_t structure used to set the date and time.
I2C status (uint8_t). Returns zero if successful.
tmElements_t tm;
tm.Hour = 23; // set the tm structure to 23h31m30s on 13Feb2009
tm.Minute = 31;
tm.Second = 30;
tm.Day = 13;
tm.Month = 2;
tm.Year = 2009 - 1970; // tmElements_t.Year is the offset from 1970
myRTC.write(tm); // set the RTC from the tm structure
The DS3231 and DS3232 have two alarms. Alarm1 can be set to seconds precision; Alarm2 can only be set to minutes precision.
Set an alarm time. Sets the alarm registers only. To cause the INT pin to be asserted on alarm match, use alarmInterrupt(). This function can set either Alarm 1 or Alarm 2, depending on the value of alarmType (use the ALARM_TYPES_t enumeration above). When setting Alarm 2, the seconds value must be supplied but is ignored, recommend using zero. (Alarm 2 has no seconds register.)
myRTC.setAlarm(alarmType, seconds, minutes, hours, dayOrDate);
alarmType: A value from the ALARM_TYPES_t enumeration, above. (ALARM_TYPES_t)
seconds: The seconds value to set the alarm to. (uint8_t)
minutes: The minutes value to set the alarm to. (uint8_t)
hours: The hours value to set the alarm to. (uint8_t)
dayOrDate: The day of the week or the date of the month. For day of the week, use a value from the Time library timeDayOfWeek_t enumeration, i.e. dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday. (uint8_t)
None.
// Set Alarm1 for 12:34:56 on Sunday
myRTC.setAlarm(DS3232RTC::ALM1_MATCH_DAY, 56, 34, 12, dowSunday);
Set an alarm time. Sets the alarm registers only. To cause the INT pin to be asserted on alarm match, use alarmInterrupt(). This function can set either Alarm 1 or Alarm 2, depending on the value of alarmType (use the ALARM_TYPES_t enumeration above). However, when using this function to set Alarm 1, the seconds value is set to zero. (Alarm 2 has no seconds register.)
myRTC.setAlarm(alarmType, minutes, hours, dayOrDate);
alarmType: A value from the ALARM_TYPES_t enumeration, above. (ALARM_TYPES_t)
minutes: The minutes value to set the alarm to. (uint8_t)
hours: The hours value to set the alarm to. (uint8_t)
dayOrDate: The day of the week or the date of the month. For day of the week, use a value from the Time library timeDayOfWeek_t enumeration, i.e. dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday. (uint8_t)
None.
// Set Alarm2 for 12:34 on the 4th day of the month
myRTC.setAlarm(DS3232RTC::ALM2_MATCH_DATE, 34, 12, 4);
Enable or disable an alarm "interrupt". Note that this "interrupt" just causes the RTC's INT pin to be asserted. To use this signal as an actual interrupt to (for example) a microcontroller, the RTC "interrupt" pin needs to be connected to the microcontroller and the microcontroller application firmware must properly configure the interrupt and also provide for handling it.
myRTC.alarmInterrupt(alarmNumber, enable);
alarmNumber: The number of the alarm to enable or disable, ALARM_1 or ALARM_2 (ALARM_NBR_t)
alarmEnabled: true or false (bool)
None.
myRTC.alarmInterrupt(DS3232RTC::ALARM_1, true); // assert the INT pin when Alarm1 occurs.
myRTC.alarmInterrupt(DS3232RTC::ALARM_2, false); // disable Alarm2
Tests whether an alarm has been triggered. If the alarm was triggered, returns true and resets the alarm flag in the RTC, else returns false.
myRTC.alarm(alarmNumber);
alarmNumber: The number of the alarm to test, ALARM_1 or ALARM_2 (ALARM_NBR_t)
Value of the alarm flag bit (bool)
if ( myRTC.alarm(DS3232RTC::ALARM_1) ) { // has Alarm1 triggered?
// yes, act on the alarm
}
else {
// no alarm
}
Tests whether an alarm has been triggered. If the alarm was triggered, returns true, else returns false. The alarm flag is not reset.
myRTC.checkAlarm(alarmNumber);
alarmNumber: The
$ claude mcp add DS3232RTC \
-- python -m otcore.mcp_server <graph>