Skip to content

Commit 10194da

Browse files
author
JChristensen
committed
Added vbaten() method to set or clear the VBATEN bit in the day register (0x03).
Added sketch to demonstrate interfacing the MCP79412 to an ATtiny45/85.
1 parent 7b21c50 commit 10194da

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*----------------------------------------------------------------------*
2+
* Digital clock display using an MCP79412 Real-Time Clock/Calendar *
3+
* and an ATtiny45/85 with a 1MHz system clock. *
4+
* *
5+
* Tested with Arduino 1.0.5. Also Arduino-Tiny Core, TinyISP, and *
6+
* TinyDebugKnockBang from http://code.google.com/p/arduino-tiny/ *
7+
* *
8+
* Run TinyISP on an ATmega microcontroller that does not have an LED *
9+
* connected to pin 13 (SCK). The LED causes problems because the SPI *
10+
* pins are also the I2C pins on the ATtiny. Connect MISO, MOSI, SCK *
11+
* on the ATmega to the corresponding pins on the ATtiny through 220Ω *
12+
* resistors for safety. Use 4.7K pullup resistors on the ATtiny *
13+
* I2C bus. *
14+
* *
15+
* Jack Christensen 21Aug2013 *
16+
* *
17+
* This work is licensed under the Creative Commons Attribution- *
18+
* ShareAlike 3.0 Unported License. To view a copy of this license, *
19+
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
20+
* letter to Creative Commons, 171 Second Street, Suite 300, *
21+
* San Francisco, California, 94105, USA. *
22+
*----------------------------------------------------------------------*/
23+
24+
#include <MCP79412RTC.h> //http://github.com/JChristensen/MCP79412RTC
25+
#include <Time.h> //http://playground.arduino.cc/Code/Time
26+
#include <TinyDebugKnockBang.h> //http://code.google.com/p/arduino-tiny/
27+
#include <TinyWireM.h> //http://playground.arduino.cc/Code/USIi2c
28+
29+
void setup(void)
30+
{
31+
Debug.begin(250000);
32+
33+
//setSyncProvider() causes the Time library to synchronize with the
34+
//external RTC by calling RTC.get() every five minutes by default.
35+
setSyncProvider(RTC.get);
36+
Debug.print(F("RTC Sync"));
37+
if (timeStatus() != timeSet) Debug.print(F(" FAIL!"));
38+
Debug.println();
39+
}
40+
41+
void loop(void)
42+
{
43+
static time_t tLast;
44+
45+
time_t t = now();
46+
if (t != tLast) {
47+
tLast = t;
48+
printDateTime(t);
49+
Debug.println();
50+
}
51+
}
52+
53+
//print date and time to Serial
54+
void printDateTime(time_t t)
55+
{
56+
printDate(t);
57+
Debug.print(' ');
58+
printTime(t);
59+
}
60+
61+
//print time to Serial
62+
void printTime(time_t t)
63+
{
64+
printI00(hour(t), ':');
65+
printI00(minute(t), ':');
66+
printI00(second(t), ' ');
67+
}
68+
69+
//print date to Serial
70+
void printDate(time_t t)
71+
{
72+
printI00(day(t), 0);
73+
Debug.print(monthShortStr(month(t)));
74+
Debug.print(year(t), DEC);
75+
}
76+
77+
//Print an integer in "00" format (with leading zero),
78+
//followed by a delimiter character to Serial.
79+
//Input value assumed to be between 0 and 99.
80+
void printI00(int val, char delim)
81+
{
82+
if (val < 10) Debug.print('0');
83+
Debug.print(val, DEC);;
84+
if (delim > 0) Debug.print(delim);
85+
return;
86+
}

MCP79412RTC.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,25 @@ boolean MCP79412RTC::isRunning(void)
559559
return i2cRead() & _BV(ST);
560560
}
561561

562+
/*----------------------------------------------------------------------*
563+
* Set or clear the VBATEN bit. Setting the bit powers the clock and *
564+
* SRAM from the backup battery when Vcc falls. Note that setting the *
565+
* time via set() or write() sets the VBATEN bit. *
566+
*----------------------------------------------------------------------*/
567+
void MCP79412RTC::vbaten(boolean enable)
568+
{
569+
uint8_t day;
570+
571+
ramRead(DAY_REG, &day, 1);
572+
if (enable)
573+
day |= _BV(VBATEN);
574+
else
575+
day &= ~_BV(VBATEN);
576+
577+
ramWrite(DAY_REG, &day, 1);
578+
return;
579+
}
580+
562581
/*----------------------------------------------------------------------*
563582
* Decimal-to-BCD conversion *
564583
*----------------------------------------------------------------------*/

MCP79412RTC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class MCP79412RTC
138138
void out(boolean level);
139139
void alarmPolarity(boolean polarity);
140140
boolean isRunning(void);
141+
void vbaten(boolean enable);
141142

142143
private:
143144
static void ramWrite(byte addr, byte value);

ReadMe.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ logger using the MCP79412RTC's ability to capture power down and power up times.
5050
Power failure events are logged to the MCP79412RTC's SRAM. Output is to the
5151
Arduino serial monitor.
5252

53+
tiny79412_KnockBang: Demonstrates interfacing an ATtiny45/85 to the
54+
MCP79412.
55+
5356
--------------------------------------------------------------------------------
5457
Similar to the DS1307RTC library, the MCP79412 library instantiates an RTC
5558
object; the user does not need to do this.

0 commit comments

Comments
 (0)