forked from JChristensen/MCP79412RTC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMCP79412RTC.h
More file actions
105 lines (94 loc) · 3.38 KB
/
MCP79412RTC.h
File metadata and controls
105 lines (94 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Arduino MCP79412RTC Library
// https://github.com/JChristensen/MCP79412RTC
// Copyright (C) 2018 by Jack Christensen and licensed under
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
//
// Arduino library for the Microchip MCP7941x Real-Time Clocks.
// Requires PJRC's improved version of the Arduino Time Library,
// https://playground.arduino.cc/Code/Time
// https://github.com/PaulStoffregen/Time
//
// For AVR architecture, an MCP79412RTC object named RTC is instantiated
// by the library and I2C initialization occurs in the constructor;
// this is for backwards compatibility.
// For other architectures, the user needs to instantiate a MCP79412RTC
// object and optionally initialize the I2C bus by calling
// MCP79412RTC::begin(). The constructor has an optional bool parameter
// to indicate whether I2C initialization should occur in the
// constructor; this parameter defaults to true if not given.
#ifndef MCP79412RTC_H_INCLUDED
#define MCP79412RTC_H_INCLUDED
#include <Arduino.h>
#include <TimeLib.h> // https://github.com/PaulStoffregen/Time
// Alarm types for use with the enableAlarm() function
enum {
ALM_MATCH_SECONDS,
ALM_MATCH_MINUTES,
ALM_MATCH_HOURS,
ALM_MATCH_DAY, // triggers alarm at midnight
ALM_MATCH_DATE,
ALM_RESERVED_5, // do not use
ALM_RESERVED_6, // do not use
ALM_MATCH_DATETIME,
ALM_DISABLE
};
// Square-wave output frequencies for use with squareWave() function
enum {
SQWAVE_1_HZ,
SQWAVE_4096_HZ,
SQWAVE_8192_HZ,
SQWAVE_32768_HZ,
SQWAVE_NONE
};
// constants for use with alarm functions
#define ALARM_0 0
#define ALARM_1 1
class MCP79412RTC
{
public:
MCP79412RTC(bool initI2C = true);
void begin();
static time_t get();
static void set(time_t t);
static bool read(tmElements_t &tm);
static void write(tmElements_t &tm);
void sramWrite(byte addr, byte value);
void sramWrite(byte addr, byte *values, byte nBytes);
byte sramRead(byte addr);
void sramRead(byte addr, byte *values, byte nBytes);
void eepromWrite(byte addr, byte value);
void eepromWrite(byte addr, byte *values, byte nBytes);
byte eepromRead(byte addr);
void eepromRead(byte addr, byte *values, byte nBytes);
int calibRead();
void calibWrite(int value);
void idRead(byte *uniqueID);
void getEUI64(byte *uniqueID);
bool powerFail(time_t *powerDown, time_t *powerUp);
void squareWave(uint8_t freq);
void setAlarm(uint8_t alarmNumber, time_t alarmTime);
void enableAlarm(uint8_t alarmNumber, uint8_t alarmType);
bool alarm(uint8_t alarmNumber);
void out(bool level);
void alarmPolarity(bool polarity);
bool isRunning();
void vbaten(bool enable);
private:
static void ramWrite(byte addr, byte value);
static void ramWrite(byte addr, byte *values, byte nBytes);
static byte ramRead(byte addr);
static void ramRead(byte addr, byte *values, byte nBytes);
static byte eepromWait();
static uint8_t dec2bcd(uint8_t num);
static uint8_t bcd2dec(uint8_t num);
};
#if defined ARDUINO_ARCH_AVR
extern MCP79412RTC RTC;
#endif
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
#ifndef BUFFER_LENGTH // a horrible and limiting kludge for samd (arduino zero)
#define BUFFER_LENGTH 32
#endif
#endif