Skip to content

Commit 3ffaf56

Browse files
committed
Add functionality to address different I2C peripherals.
1 parent 4434928 commit 3ffaf56

File tree

9 files changed

+59
-12
lines changed

9 files changed

+59
-12
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +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.
26+
27+
## Version 2.0.0 notes
1728
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.
1829

1930
- The library no longer defines an `MCP79412RTC` object, therefore each sketch needs to define one. (Previous versions of the library defined an `MCP79412RTC` 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.)
@@ -35,6 +46,7 @@ The following example sketches are included with the **MCP79412RTC** library:
3546
- **rtcSet1:** Set the RTC date and time using a hard-coded value in the sketch.
3647
- **rtcSet2:** Similar to **rtcSet1**, a different way to hard-code the date and time.
3748
- **rtcSet3:** Set the RTC to the sketch compile date and time.
49+
- **rtc_wire1:** Raspberry Pi Pico example using `Wire1`.
3850
- **SetSerial:** Set the RTC's date, time, and calibration register from the Arduino serial monitor.
3951
- **TimeRTC:** Similar to the example of the same name provided with the **Time** library.
4052
- **PowerOutageLogger:** A comprehensive example that implements a power failure logger using the MCP79412's ability to capture power down and power up times. Power failure events are logged to the MCP79412's SRAM. Output is to the Arduino serial monitor.
@@ -71,18 +83,21 @@ Symbolic names used with the squareWave() function (described below).
7183
- SQWAVE_NONE
7284

7385
## Constructor
74-
### MCP79412RTC()
86+
### MCP79412RTC(TwoWire& wire)
7587
##### Description
7688
Instantiates an `MCP79412RTC` object.
7789
##### Syntax
78-
`MCP79412RTC myRTC;`
90+
`MCP79412RTC myRTC(wire);`
7991
##### Parameters
80-
None.
92+
**wire:** An optional parameter to specify which I2C bus to use. If omitted, defaults to `Wire`. *(TwoWire&)*
8193
##### Returns
8294
None.
8395
##### Example
8496
```c++
85-
MCP79412RTC myRTC;
97+
MCP79412RTC myRTC; // to use Wire
98+
// or
99+
MCP79412RTC myRTC(Wire1); // to use Wire1
100+
86101
```
87102
88103
## Initialization function

examples/PowerOutageLogger/PowerOutageLogger.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void setup()
2828
Serial.begin(115200);
2929
Serial << F( "\n" __FILE__ " " __DATE__ " " __TIME__ "\n" );
3030

31-
setSyncProvider(myRTC.get); // the function to get the time from the RTC
31+
setSyncProvider([](){return myRTC.get();}); // the function to get the time from the RTC
3232
Serial << "RTC SYNC";
3333
if (timeStatus()!= timeSet) Serial << " FAIL";
3434
Serial << endl;

examples/SetSerial/SetSerial.ino

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

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

examples/TimeRTC/TimeRTC.ino

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

examples/rtcSet1/rtcSet1.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void setup()
2626
myRTC.set(now()); // set the RTC from the system time
2727

2828
// sync the system time with the RTC every five minutes (by default)
29-
setSyncProvider(myRTC.get);
29+
setSyncProvider([](){return myRTC.get();});
3030
if (timeStatus() != timeSet)
3131
Serial.println("Unable to sync with the RTC");
3232
else

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 MCP79412RTC Library
2+
// https://github.com/JChristensen/MCP79412RTC
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 <MCP79412RTC.h> // http://github.com/JChristensen/MCP79412RTC
9+
10+
constexpr int sdaPin {26}, sclPin {27}; // I2C pins for Wire1 (your pins may vary)
11+
MCP79412RTC 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/tiny79412_KnockBang/tiny79412_KnockBang.ino

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

3636
// setSyncProvider() causes the Time library to synchronize with the
3737
// external RTC by calling RTC.get() every five minutes by default.
38-
setSyncProvider(myRTC.get);
38+
setSyncProvider([](){return myRTC.get();});
3939
Debug.print(F("RTC Sync"));
4040
if (timeStatus() != timeSet) Debug.print(F(" FAIL!"));
4141
Debug.println();

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MCP79412RTC
2-
version=2.0.0
2+
version=3.0.0
33
author=Jack Christensen <[email protected]>
44
maintainer=Jack Christensen <[email protected]>
55
sentence=Arduino library for the Microchip MCP79411/12 Real-Time Clock/Calendar.

src/MCP79412RTC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class MCP79412RTC
173173
void dumpEEPROM(const uint32_t startAddr=0, const uint32_t nBytes=128);
174174

175175
private:
176-
TwoWire& wire; // reference to Wire or Wire1
176+
TwoWire& wire; // reference to Wire, Wire1, etc.
177177
void ramWrite(const uint8_t addr, const uint8_t value);
178178
void ramWrite(const uint8_t addr, const uint8_t* values, const uint8_t nBytes);
179179
uint8_t ramRead(uint8_t addr);

0 commit comments

Comments
 (0)