-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAB0805.cpp
318 lines (290 loc) · 11.1 KB
/
AB0805.cpp
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// I2Cdev library collection - AB0805 I2C device class
// Based on Abracon AB08X5 Real-Time Clock Family datasheet, 2015
// 03/07/2015 by Curran Sinha ([email protected])
// Based heavily off of Jeff Rowberg's DS1307 RTC Library
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
// I2C Device Library hosted at http://www.i2cdevlib.com
//
// Changelog:
// 2015-03-09 - initial release
/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2012 Jeff Rowberg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include "AB0805.h"
/** Default constructor, uses default I2C address.
* @see AB0805_DEFAULT_ADDRESS
*/
AB0805::AB0805() {
devAddr = AB0805_DEFAULT_ADDRESS;
}
/** Specific address constructor.
* @param address I2C address
* @see AB0805_DEFAULT_ADDRESS
* @see AB0805_ADDRESS
*/
AB0805::AB0805(uint8_t address) {
devAddr = address;
}
/** Power on and prepare for general usage.
* Stops clock. Intialized to use crystal oscillator unless one is not found
* then the RC oscillator is used.
*/
void AB0805::initialize() {
uint8_t stopClk = 0x91;
uint8_t allowOscEdit = 0xA1;
uint8_t xtOscSel = 0x08;
I2Cdev::writeByte(devAddr, AB0805_RA_CONTROL1, stopClk); //stops clock
I2Cdev::writeByte(devAddr, AB0805_RA_CONFIG_KEY, allowOscEdit); //Allows edits to osc. cntrl register (0x1C)
I2Cdev::writeByte(devAddr, AB0805_RA_OSC_CONTROL, xtOscSel); //Crystal used (switch to RC if XT osc failure)
}
/** Verify the I2C connection.
* Make sure the device is connected and responds as expected.
* @return True if connection is valid, false otherwise
*/
bool AB0805::testConnection() {
if (I2Cdev::readByte(devAddr, AB0805_RA_ID0 , buffer) == 0x08) {
return true;
}
return false;
}
void AB0805::startClock() {
I2Cdev::writeBit(devAddr, AB0805_RA_CONTROL1, AB0805_CONTROL1_STOP_BIT, 0);
}
void AB0805::stopClock() {
I2Cdev::writeBit(devAddr, AB0805_RA_CONTROL1, AB0805_CONTROL1_STOP_BIT, 1);
}
void AB0805::useRcOsc() {
uint8_t allowOscEdit = 0xA1;
I2Cdev::writeByte(devAddr, AB0805_RA_CONFIG_KEY, allowOscEdit); //Allows edits to osc. cntrl register (0x1C)
I2Cdev::writeBit(devAddr, AB0805_RA_OSC_CONTROL, AB0805_OSC_CONTROL_OSC_SEL, 1);
}
// HUNDREDTHS register -- only valid with XT oscillator
uint16_t AB0805::getHundredths() {
I2Cdev::readByte(devAddr, AB0805_RA_HUNDREDTHS, buffer);
return (buffer[0] & 0x0F) + ((buffer[0] & 0xF0) >> 4) * 10;
}
void AB0805::setHundredths(uint16_t hundredths) {
if (hundredths > 99) return;
uint8_t value = ((hundredths/ 10) << 4) + (hundredths % 10);
I2Cdev::writeByte(devAddr, AB0805_RA_HUNDREDTHS, value);
}
// SECONDS register
uint8_t AB0805::getSeconds() {
// Byte: [7 = CH] [6:4 = 10SEC] [3:0 = 1SEC]
I2Cdev::readByte(devAddr, AB0805_RA_SECONDS, buffer);
return (buffer[0] & 0x0F) + ((buffer[0] & 0x70) >> 4) * 10;
}
void AB0805::setSeconds(uint8_t seconds) {
if (seconds > 59) return;
uint8_t value = 0x00 + ((seconds / 10) << 4) + (seconds % 10);
I2Cdev::writeByte(devAddr, AB0805_RA_SECONDS, value);
}
// MINUTES register
uint8_t AB0805::getMinutes() {
// Byte: [7 = 0] [6:4 = 10MIN] [3:0 = 1MIN]
I2Cdev::readByte(devAddr, AB0805_RA_MINUTES, buffer);
return (buffer[0] & 0x0F) + ((buffer[0] & 0x70) >> 4) * 10;
}
void AB0805::setMinutes(uint8_t minutes) {
if (minutes > 59) return;
uint8_t value = ((minutes / 10) << 4) + (minutes % 10);
I2Cdev::writeByte(devAddr, AB0805_RA_MINUTES, value);
}
// HOURS register
uint8_t AB0805::getMode() {
I2Cdev::readBit(devAddr, AB0805_RA_CONTROL1, AB0805_CONTROL1_12OR24_BIT, buffer);
mode12 = buffer[0];
return buffer[0];
}
void AB0805::setMode(uint8_t mode) {
I2Cdev::writeBit(devAddr, AB0805_RA_CONTROL1, AB0805_CONTROL1_12OR24_BIT, mode);
}
uint8_t AB0805::getAMPM() {
I2Cdev::readBit(devAddr, AB0805_RA_HOURS, AB0805_HOURS_AMPM_BIT, buffer);
return buffer[0];
}
//0 = AM Hours, 1 = PM Hours
void AB0805::setAMPM(uint8_t ampm) {
I2Cdev::writeBit(devAddr, AB0805_RA_HOURS, AB0805_HOURS_AMPM_BIT, ampm);
}
uint8_t AB0805::getHours12() {
I2Cdev::readByte(devAddr, AB0805_RA_HOURS, buffer);
mode12 = getMode();
if (mode12) {
// bit 6 is high, 12-hour mode
// Byte: [5 = AM/PM] [4 = 10HR] [3:0 = 1HR]
return (buffer[0] & 0x0F) + ((buffer[0] & 0x10) >> 4) * 10;
} else {
// bit 6 is low, 24-hour mode (default)
// Byte: [5:4 = 10HR] [3:0 = 1HR]
uint8_t hours = (buffer[0] & 0x0F) + ((buffer[0] & 0x30) >> 4) * 10;
// convert 24-hour to 12-hour format, since that's what's requested
if (hours > 12) hours -= 12;
else if (hours == 0) hours = 12;
return hours;
}
}
void AB0805::setHours12(uint8_t hours, uint8_t ampm) {
if (hours > 12 || hours < 1) return;
if (mode12) {
// bit 6 is high, 12-hour mode
// Byte: [5 = AM/PM] [4 = 10HR] [3:0 = 1HR]
if (ampm > 0) ampm = 0x20;
uint8_t value = ampm + ((hours / 10) << 4) + (hours % 10);
I2Cdev::writeByte(devAddr, AB0805_RA_HOURS, value);
} else {
// bit 6 is low, 24-hour mode (default)
// Byte: [5:4 = 10HR] [3:0 = 1HR]
if (ampm > 0) hours += 12;
if (hours == 0) hours = 12; // 12 AM
else if (hours == 24) hours = 12; // 12 PM, after +12 adjustment
uint8_t value = ((hours / 10) << 4) + (hours % 10);
I2Cdev::writeByte(devAddr, AB0805_RA_HOURS, value);
}
}
uint8_t AB0805::getHours24() {
I2Cdev::readByte(devAddr, AB0805_RA_HOURS, buffer);
mode12 = getMode();
if (mode12) {
// bit 6 is high, 12-hour mode
// Byte: [5 = AM/PM] [4 = 10HR] [3:0 = 1HR]
uint8_t hours = (buffer[0] & 0x0F) + ((buffer[0] & 0x10) >> 4) * 10;
// convert 12-hour to 24-hour format, since that's what's requested
if (buffer[0] & 0x20) {
// currently PM
if (hours < 12) hours += 12;
} else {
// currently AM
if (hours == 12) hours = 0;
}
return hours;
} else {
// bit 6 is low, 24-hour mode (default)
// Byte: [5:4 = 10HR] [3:0 = 1HR]
return (buffer[0] & 0x0F) + ((buffer[0] & 0x30) >> 4) * 10;
}
}
void AB0805::setHours24(uint8_t hours) {
if (hours > 23) return;
if (mode12) {
// bit 6 is high, 12-hour mode
// Byte: [5 = AM/PM] [4 = 10HR] [3:0 = 1HR]
uint8_t ampm = 0;
if (hours > 11) ampm = 0x20;
if (hours > 12) hours -= 12;
else if (hours == 0) hours = 12;
uint8_t value = ampm + ((hours / 10) << 4) + (hours % 10);
I2Cdev::writeByte(devAddr, AB0805_RA_HOURS, value);
} else {
// bit 6 is low, 24-hour mode (default)
// Byte: [5:4 = 10HR] [3:0 = 1HR]
uint8_t value = ((hours / 10) << 4) + (hours % 10);
I2Cdev::writeByte(devAddr, AB0805_RA_HOURS, value);
}
}
// DAY register
uint8_t AB0805::getDayOfWeek() {
I2Cdev::readBits(devAddr, AB0805_RA_DAY, AB0805_DAY_BIT, AB0805_DAY_LENGTH, buffer);
return buffer[0];
}
void AB0805::setDayOfWeek(uint8_t dow) {
if (dow < 1 || dow > 7) return;
I2Cdev::writeBits(devAddr, AB0805_RA_DAY, AB0805_DAY_BIT, AB0805_DAY_LENGTH, dow);
}
// DATE register
uint8_t AB0805::getDay() {
// Byte: [7:6 = 0] [5:4 = 10DAY] [3:0 = 1DAY]
I2Cdev::readByte(devAddr, AB0805_RA_DATE, buffer);
return (buffer[0] & 0x0F) + ((buffer[0] & 0x30) >> 4) * 10;
}
void AB0805::setDay(uint8_t day) {
uint8_t value = ((day / 10) << 4) + (day % 10);
I2Cdev::writeByte(devAddr, AB0805_RA_DATE, value);
}
// MONTH register
uint8_t AB0805::getMonth() {
// Byte: [7:5 = 0] [4 = 10MONTH] [3:0 = 1MONTH]
I2Cdev::readByte(devAddr, AB0805_RA_MONTH, buffer);
return (buffer[0] & 0x0F) + ((buffer[0] & 0x10) >> 4) * 10;
}
void AB0805::setMonth(uint8_t month) {
if (month < 1 || month > 12) return;
uint8_t value = ((month / 10) << 4) + (month % 10);
I2Cdev::writeByte(devAddr, AB0805_RA_MONTH, value);
}
// YEAR register
uint16_t AB0805::getYear() {
I2Cdev::readByte(devAddr, AB0805_RA_YEAR, buffer);
return 2000 + (buffer[0] & 0x0F) + ((buffer[0] & 0xF0) >> 4) * 10;
}
void AB0805::setYear(uint16_t year) {
if (year < 2000) return;
year -= 2000;
uint8_t value = ((year / 10) << 4) + (year % 10);
I2Cdev::writeByte(devAddr, AB0805_RA_YEAR, value);
}
// convenience methods
void AB0805::getDate(uint16_t *year, uint8_t *month, uint8_t *day) {
*year = getYear();
*month = getMonth();
*day = getDay();
}
void AB0805::setDate(uint16_t year, uint8_t month, uint8_t day) {
setYear(year);
setMonth(month);
setDay(day);
}
void AB0805::getTime12(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint8_t *ampm) {
*hours = getHours12();
*minutes = getMinutes();
*seconds = getSeconds();
*ampm = getAMPM();
}
void AB0805::setTime12(uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t ampm) {
setSeconds(seconds);
setMinutes(minutes);
setHours12(hours, ampm);
}
void AB0805::getTime24(uint8_t *hours, uint8_t *minutes, uint8_t *seconds) {
*hours = getHours24();
*minutes = getMinutes();
*seconds = getSeconds();
}
void AB0805::setTime24(uint8_t hours, uint8_t minutes, uint8_t seconds) {
setSeconds(seconds);
setMinutes(minutes);
setHours24(hours);
}
void AB0805::getDateTime12(uint16_t *year, uint8_t *month, uint8_t *day, uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint8_t *ampm) {
getTime12(hours, minutes, seconds, ampm);
getDate(year, month, day);
}
void AB0805::setDateTime12(uint16_t year, uint8_t month, uint8_t day, uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t ampm) {
setTime12(hours, minutes, seconds, ampm);
setDate(year, month, day);
}
void AB0805::getDateTime24(uint16_t *year, uint8_t *month, uint8_t *day, uint8_t *hours, uint8_t *minutes, uint8_t *seconds) {
getTime24(hours, minutes, seconds);
getDate(year, month, day);
}
void AB0805::setDateTime24(uint16_t year, uint8_t month, uint8_t day, uint8_t hours, uint8_t minutes, uint8_t seconds) {
setTime24(hours, minutes, seconds);
setDate(year, month, day);
}