Skip to content

Commit ec80b44

Browse files
committed
Add functionality to address different I2C peripherals.
1 parent 772e281 commit ec80b44

File tree

10 files changed

+90
-32
lines changed

10 files changed

+90
-32
lines changed

README.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,18 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY
1313

1414
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>
1515

16-
## Important note: Library v2.0.0
16+
## Version 3.0.0 notes
17+
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](https://github.com/PaulStoffregen/Time), e.g.:
18+
```c++
19+
setSyncProvider(myRTC.get);
20+
```
21+
Needs to change as follows:
22+
```c++
23+
setSyncProvider([](){return myRTC.get();});
24+
```
25+
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.
1726

27+
## Version 2.0.0 notes
1828
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.
1929

2030
- The library no longer defines a `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.)
@@ -41,6 +51,7 @@ The following example sketches are included with the **DS3232RTC** library:
4151
- **rtcTimeTemp:** Displays time and temperature.
4252
- **rtc_temperature:** Displays time and temperature but initiates a temperature conversion every 10 seconds in addition to the RTC's default conversion period of 64 seconds.
4353
- **rtc_interrupt:** Uses a 1Hz interrupt from the RTC to keep time.
54+
- **rtc_wire1:** Raspberry Pi Pico example using `Wire1`.
4455
- **mcu_clock_drift:** Demonstrates that MCU time as kept by the **Time** library will drift as compared to RTC time, and that MCU time may not increase by one second every second.
4556
- **tiny3232_KnockBang:** Demonstrates interfacing an ATtiny45/85 to a DS3231 or DS3232 RTC.
4657
- Several examples for using the RTC alarms. See the [alarm primer](https://github.com/JChristensen/DS3232RTC/blob/master/alarm-primer.md) for more information.
@@ -87,18 +98,20 @@ Symbolic names used with the squareWave() function (described below).
8798
- SQWAVE_8192_HZ
8899

89100
## Constructor
90-
### DS3232RTC()
101+
### DS3232RTC(TwoWire& wire)
91102
##### Description
92-
Instantiates a DS3232RTC object.
103+
Instantiates a `DS3232RTC` object.
93104
##### Syntax
94-
`DS3232RTC myRTC;`
105+
`DS3232RTC myRTC(wire);`
95106
##### Parameters
96-
None.
107+
**wire:** An optional parameter to specify which I2C bus to use. If omitted, defaults to `Wire`. *(TwoWire&)*.
97108
##### Returns
98109
None.
99110
##### Example
100111
```c++
101-
DS3232RTC myRTC;
112+
DS3232RTC myRTC; // to use Wire
113+
// or
114+
DS3232RTC myRTC(Wire1); // to use Wire1
102115
```
103116
104117
## Initialization function
@@ -408,6 +421,21 @@ myRTC.squareWave(DS3232RTC::SQWAVE_1_HZ); // 1 Hz square wave
408421
myRTC.squareWave(DS3232RTC::SQWAVE_NONE); // no square wave
409422
```
410423

424+
### enable32kHz(bool enable)
425+
##### Description
426+
Enables or disables the 32kHz square wave output.
427+
##### Syntax
428+
`myRTC.enable32kHz(enable);`
429+
##### Parameters
430+
**enable:** true, false, or boolean expression. *(bool)*
431+
##### Returns
432+
None.
433+
##### Example
434+
```c++
435+
myRTC.enable32kHz(true); // 32kHz output enabled
436+
myRTC.enable32kHz(false); // 32kHz output disabled
437+
```
438+
411439
### oscStopped(bool clearOSF)
412440
##### Description
413441
Returns the value of the oscillator stop flag (OSF) bit in the control/status register which indicates that the oscillator is or was stopped, and that the timekeeping data may be invalid. Optionally clears the OSF bit depending on the argument passed. If the `clearOSF` argument is omitted, the OSF bit is cleared by default. Calls to `set()` and `write()` also clear the OSF bit.

examples/SetSerial/SetSerial.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void setup()
4040

4141
// setSyncProvider() causes the Time library to synchronize with the
4242
// external RTC by calling RTC.get() every five minutes by default.
43-
setSyncProvider(myRTC.get);
43+
setSyncProvider([](){return myRTC.get();});
4444
Serial << F("RTC Sync");
4545
if (timeStatus() != timeSet) Serial << F(" FAIL!");
4646
Serial << endl;

examples/TimeRTC/TimeRTC.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void setup()
1414
{
1515
Serial.begin(115200);
1616
myRTC.begin();
17-
setSyncProvider(myRTC.get); // the function to get the time from the RTC
17+
setSyncProvider([](){return myRTC.get();}); // the function to get the time from the RTC
1818
if(timeStatus() != timeSet)
1919
Serial.println("Unable to sync with the RTC");
2020
else

examples/alarm_ex6/alarm_ex6.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void setup()
5656

5757
// setSyncProvider() causes the Time library to synchronize with the
5858
// external RTC by calling myRTC.get() every five minutes by default.
59-
setSyncProvider(myRTC.get);
59+
setSyncProvider([](){return myRTC.get();});
6060
Serial << "RTC Sync";
6161
if (timeStatus() != timeSet) {
6262
Serial << " FAIL!";

examples/mcu_clock_drift/mcu_clock_drift.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void setup()
3636
Serial.begin(115200);
3737
Serial.println(F("\n" __FILE__ " " __DATE__ " " __TIME__));
3838
myRTC.begin();
39-
setSyncProvider(myRTC.get); // the function to get the time from the RTC
39+
setSyncProvider([](){return myRTC.get();}); // the function to get the time from the RTC
4040
Serial.print(F("RTC sync "));
4141
if (timeStatus() == timeSet)
4242
Serial.println(F("OK"));

examples/rtc_wire1/rtc_wire1.ino

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Arduino DS3232RTC Library
2+
// https://github.com/JChristensen/DS3232RTC
3+
// rtc_wire1.ino
4+
// Example sketch with the RTC on the Wire1 bus.
5+
// Tested with Raspberry Pi Pico using the Arduino-Pico core,
6+
// https://github.com/earlephilhower/arduino-pico
7+
8+
#include <DS3232RTC.h> // http://github.com/JChristensen/DS3232RTC
9+
10+
constexpr int sdaPin {26}, sclPin {27}; // I2C pins for Wire1 (your pins may vary)
11+
DS3232RTC myRTC(Wire1);
12+
13+
void setup()
14+
{
15+
Serial.begin(115200);
16+
while (!Serial && millis() < 2000) delay(50);
17+
18+
Wire1.setSDA(sdaPin); Wire1.setSCL(sclPin);
19+
myRTC.begin();
20+
setSyncProvider([](){return myRTC.get();}); // the function to get the time from the RTC
21+
if (timeStatus() != timeSet)
22+
Serial.println("Unable to sync with the RTC");
23+
else
24+
Serial.println("RTC has set the system time");
25+
}
26+
27+
void loop()
28+
{
29+
Serial.printf("%.4d-%.2d-%.2d %.2d:%.2d:%.2d\n", year(), month(), day(),
30+
hour(), minute(), second());
31+
delay(1000);
32+
}

examples/tiny3232_KnockBang/tiny3232_KnockBang.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void setup()
2929

3030
// setSyncProvider() causes the Time library to synchronize with the
3131
// external RTC by calling myRTC.get() every five minutes by default.
32-
setSyncProvider(myRTC.get);
32+
setSyncProvider([](){return myRTC.get();});
3333
Debug.print(F("RTC Sync"));
3434
if (timeStatus() != timeSet) Debug.print(F(" FAIL!"));
3535
Debug.println();

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=2.0.1
2+
version=3.0.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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
#include <DS3232RTC.h>
1010

11-
uint8_t DS3232RTC::errCode;
12-
1311
// Initialize the I2C bus.
1412
void DS3232RTC::begin()
1513
{

src/DS3232RTC.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@
2323
#define i2cWrite TinyWireM.send
2424
#elif ARDUINO >= 100
2525
#include <Wire.h>
26-
#define i2cBegin Wire.begin
27-
#define i2cBeginTransmission Wire.beginTransmission
28-
#define i2cEndTransmission Wire.endTransmission
29-
#define i2cRequestFrom Wire.requestFrom
30-
#define i2cRead Wire.read
31-
#define i2cWrite Wire.write
26+
#define i2cBegin wire.begin
27+
#define i2cBeginTransmission wire.beginTransmission
28+
#define i2cEndTransmission wire.endTransmission
29+
#define i2cRequestFrom wire.requestFrom
30+
#define i2cRead wire.read
31+
#define i2cWrite wire.write
3232
#else
3333
#include <Wire.h>
34-
#define i2cBegin Wire.begin
35-
#define i2cBeginTransmission Wire.beginTransmission
36-
#define i2cEndTransmission Wire.endTransmission
37-
#define i2cRequestFrom Wire.requestFrom
38-
#define i2cRead Wire.receive
39-
#define i2cWrite Wire.send
34+
#define i2cBegin wire.begin
35+
#define i2cBeginTransmission wire.beginTransmission
36+
#define i2cEndTransmission wire.endTransmission
37+
#define i2cRequestFrom wire.requestFrom
38+
#define i2cRead wire.receive
39+
#define i2cWrite wire.send
4040
#endif
4141

4242
#ifndef _BV
@@ -131,12 +131,11 @@ class DS3232RTC
131131
DS32_CENTURY {7}, // Century bit in Month register
132132
DS32_DYDT {6}; // Day/Date flag bit in alarm Day/Date registers
133133

134-
DS3232RTC() {};
135-
DS3232RTC(bool initI2C) { (void)initI2C; } // undocumented for backward compatibility
134+
DS3232RTC(TwoWire& tw=Wire) : wire(tw) {};
136135
void begin();
137-
static time_t get(); // static needed to work with setSyncProvider() in the Time library
136+
time_t get();
138137
uint8_t set(time_t t);
139-
static uint8_t read(tmElements_t &tm);
138+
uint8_t read(tmElements_t &tm);
140139
uint8_t write(tmElements_t &tm);
141140
uint8_t writeRTC(uint8_t addr, uint8_t* values, uint8_t nBytes);
142141
uint8_t writeRTC(uint8_t addr, uint8_t value);
@@ -152,11 +151,12 @@ class DS3232RTC
152151
void enable32kHz(bool enable);
153152
bool oscStopped(bool clearOSF = false);
154153
int16_t temperature();
155-
static uint8_t errCode;
154+
uint8_t errCode;
156155

157156
private:
157+
TwoWire& wire; // reference to Wire, Wire1, etc.
158158
uint8_t dec2bcd(uint8_t n);
159-
static uint8_t bcd2dec(uint8_t n);
159+
uint8_t bcd2dec(uint8_t n);
160160
};
161161

162162
#endif

0 commit comments

Comments
 (0)