forked from Industruino/MCP7940-RTC-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMCP7940.cpp
More file actions
executable file
·237 lines (217 loc) · 5.33 KB
/
MCP7940.cpp
File metadata and controls
executable file
·237 lines (217 loc) · 5.33 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
This software is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
Attribution-ShareAlike
CC BY-SA
MCP7940.cpp - library for MCP7940 rtc
version 1.0 / 2014/01/27
http://smi.aii.pub.ro/arduino.html
*/
#include "Wire.h"
#include "MCP7940.h"
uint8_t MCP7940_VBAT_MASK=0;
MCP7940::MCP7940()
{
Wire.begin();
}
// PRIVATE FUNCTIONS
// Aquire data from the RTC chip in BCD format
// refresh the buffer
void MCP7940::read(void)
{
// use the Wire lib to connect to tho rtc
// reset the resgiter pointer to zero
Wire.beginTransmission(MCP7940_CTRL_ID);
Wire.write((uint8_t)0x00);
Wire.endTransmission();
// request the 7 bytes of data (secs, min, hr, WKDY, date. mth, yr)
Wire.requestFrom(MCP7940_CTRL_ID, 7);
for(int i=0; i<7; i++)
{
// store data in raw bcd format
rtc_bcd[i]=Wire.read();
}
}
// update the data on the IC from the bcd formatted data in the buffer
void MCP7940::save(void)
{
Wire.beginTransmission(MCP7940_CTRL_ID);
Wire.write((uint8_t)0x00); // reset register pointer
for(int i=0; i<7; i++)
{
Wire.write(rtc_bcd[i]);
}
Wire.endTransmission();
}
// PUBLIC FUNCTIONS
void MCP7940::get(int *rtc, boolean refresh) // Aquire data from buffer and convert to int, refresh buffer if required
{
if(refresh) read();
for(int i=0;i<7;i++) // cycle through each component, create array of data
{
rtc[i]=get(i, 0);
}
}
int MCP7940::get(int c, boolean refresh) // aquire individual RTC item from buffer, return as int, refresh buffer if required
{
if(refresh) read();
int v=-1;
switch(c)
{
case MCP7940_SEC:
v=(10*((rtc_bcd[MCP7940_SEC] & MCP7940_HI_SEC)>>4))+(rtc_bcd[MCP7940_SEC] & MCP7940_LO_BCD);
break;
case MCP7940_MIN:
v=(10*((rtc_bcd[MCP7940_MIN] & MCP7940_HI_MIN)>>4))+(rtc_bcd[MCP7940_MIN] & MCP7940_LO_BCD);
break;
case MCP7940_HR:
v=(10*((rtc_bcd[MCP7940_HR] & MCP7940_HI_HR)>>4))+(rtc_bcd[MCP7940_HR] & MCP7940_LO_BCD);
break;
case MCP7940_WKDY:
v=rtc_bcd[MCP7940_WKDY] & MCP7940_LO_WKDY;
break;
case MCP7940_DATE:
v=(10*((rtc_bcd[MCP7940_DATE] & MCP7940_HI_DATE)>>4))+(rtc_bcd[MCP7940_DATE] & MCP7940_LO_BCD);
break;
case MCP7940_MTH:
v=(10*((rtc_bcd[MCP7940_MTH] & MCP7940_HI_MTH)>>4))+(rtc_bcd[MCP7940_MTH] & MCP7940_LO_BCD);
break;
case MCP7940_YR:
v=(10*((rtc_bcd[MCP7940_YR] & MCP7940_HI_YR)>>4))+(rtc_bcd[MCP7940_YR] & MCP7940_LO_BCD)+MCP7940_BASE_YR;
break;
} // end switch
return v;
}
void MCP7940::set(int c, int v) // Update buffer, then update the chip
{
switch(c)
{
case MCP7940_SEC:
if(v<60 && v>-1)
{
//preserve existing clock state (running/stopped)
int state=rtc_bcd[MCP7940_SEC] & MCP7940_CLOCKHALT;
rtc_bcd[MCP7940_SEC]=state | ((v / 10)<<4) + (v % 10);
}
break;
case MCP7940_MIN:
if(v<60 && v>-1)
{
rtc_bcd[MCP7940_MIN]=((v / 10)<<4) + (v % 10);
}
break;
case MCP7940_HR:
// TODO : AM/PM 12HR/24HR
if(v<24 && v>-1)
{
rtc_bcd[MCP7940_HR]=((v / 10)<<4) + (v % 10);
}
break;
case MCP7940_WKDY:
if(v<8 && v>0)
{
rtc_bcd[MCP7940_WKDY]=v;
}
rtc_bcd[MCP7940_WKDY]=rtc_bcd[MCP7940_WKDY] | MCP7940_VBAT_MASK;
break;
case MCP7940_DATE:
if(v<31 && v>0)
{
rtc_bcd[MCP7940_DATE]=((v / 10)<<4) + (v % 10);
}
break;
case MCP7940_MTH:
if(v<10 && v>0)
{
rtc_bcd[MCP7940_MTH]= ((v / 10)<<4) + (v % 10);
}
break;
case MCP7940_YR:
if(v<80 && v>-1)
{
rtc_bcd[MCP7940_YR]=((v / 10)<<4) + (v % 10);
}
break;
} // end switch
save();
}
void MCP7940::stop(void)
{
read();
// set the ClockHalt bit high to stop the rtc
// this bit is part of the seconds byte
rtc_bcd[MCP7940_SEC]=rtc_bcd[MCP7940_SEC] & MCP7940_CLOCKHALT;
save();
}
void MCP7940::start(boolean VBAT)
{
if (VBAT) MCP7940_VBAT_MASK=B00001000;
read();
rtc_bcd[MCP7940_WKDY]=rtc_bcd[MCP7940_WKDY] | MCP7940_VBAT_MASK;
rtc_bcd[MCP7940_SEC]=rtc_bcd[MCP7940_SEC] | (~MCP7940_CLOCKHALT);
save();
}
void MCP7940::SetOutput(uint8_t c)
{
uint8_t out;
switch(c)
{
// only 0.6V ???
case HIGH :
out=MCP7940_HIGH_BIT;
break;
case LOW :
out=MCP7940_LOW_BIT;
break;
case MCP7940_SQW1HZ :
out=MCP7940_SQW1HZ_BIT;
break;
case MCP7940_SQW4KHZ :
out=MCP7940_SQW4KHZ_BIT;
break;
case MCP7940_SQW8KHZ :
out=MCP7940_SQW8KHZ_BIT;
break;
case MCP7940_SQW32KHZ :
out=MCP7940_SQW32KHZ_BIT;
case MCP7940_SQW64KHZ :
out=MCP7940_SQW64KHZ_BIT;
break;
// default:
// out=MCP7940_LOW_BIT;
}
Wire.beginTransmission(MCP7940_CTRL_ID);
Wire.write((uint8_t)0x07);
Wire.write(out);
Wire.endTransmission();
}
uint8_t MCP7940::GetOutput(void)
{
Wire.beginTransmission(MCP7940_CTRL_ID);
Wire.write((uint8_t)0x07);
Wire.endTransmission();
Wire.requestFrom(MCP7940_CTRL_ID, 1);
uint8_t out=Wire.read();
/* int c=-1;
switch(out)
{
case MCP7940_HIGH_BIT :
c=HIGH;
break;
case MCP7940_LOW_BIT :
c=LOW;
break;
case MCP7940_SQW1HZ_BIT :
c=MCP7940_SQW1HZ;
break;
case MCP7940_SQW4KHZ_BIT :
c=MCP7940_SQW4KHZ;
break;
case MCP7940_SQW8KHZ_BIT :
c=MCP7940_SQW8KHZ;
break;
case MCP7940_SQW32KHZ_BIT :
c=MCP7940_SQW32KHZ;
break;
} */
return out;
}