-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS1307.cpp
More file actions
168 lines (145 loc) · 3.59 KB
/
DS1307.cpp
File metadata and controls
168 lines (145 loc) · 3.59 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
//extern "C" {
//#include <../Wire/Wire.h>
//}
#include "wire.h"
#include "DS1307.h"
int i = 0; //The new wire library needs to take an int when you are sending for the zero register
DS1307::DS1307()
{
Wire.begin();
}
DS1307 RTC=DS1307();
// PRIVATE FUNCTIONS
// Aquire data from the RTC chip in BCD format
// refresh the buffer
void DS1307::read(void)
{
// use the Wire lib to connect to tho rtc
// reset the resgiter pointer to zero
Wire.beginTransmission(DS1307_CTRL_ID);
Wire.write(i);
Wire.endTransmission();
// request the 7 bytes of data (secs, min, hr, dow, date. mth, yr)
Wire.requestFrom(DS1307_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 DS1307::save(void)
{
Wire.beginTransmission(DS1307_CTRL_ID);
Wire.write(i); // reset register pointer
for(int i=0; i<7; i++)
{
Wire.write(rtc_bcd[i]);
}
Wire.endTransmission();
}
// PUBLIC FUNCTIONS
void DS1307::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 DS1307::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 DS1307_SEC:
v=(10*((rtc_bcd[DS1307_SEC] & DS1307_HI_SEC)>>4))+(rtc_bcd[DS1307_SEC] & DS1307_LO_BCD);
break;
case DS1307_MIN:
v=(10*((rtc_bcd[DS1307_MIN] & DS1307_HI_MIN)>>4))+(rtc_bcd[DS1307_MIN] & DS1307_LO_BCD);
break;
case DS1307_HR:
v=(10*((rtc_bcd[DS1307_HR] & DS1307_HI_HR)>>4))+(rtc_bcd[DS1307_HR] & DS1307_LO_BCD);
break;
case DS1307_DOW:
v=rtc_bcd[DS1307_DOW] & DS1307_LO_DOW;
break;
case DS1307_DATE:
v=rtc_bcd[DS1307_DATE]/16 * 10 + rtc_bcd[DS1307_DATE] % 16;
break;
case DS1307_MTH:
v=(10*((rtc_bcd[DS1307_MTH] & DS1307_HI_MTH)>>4))+(rtc_bcd[DS1307_MTH] & DS1307_LO_BCD);
break;
case DS1307_YR:
v=2000 + rtc_bcd[DS1307_YR]/16 * 10 + rtc_bcd[DS1307_YR] % 16;
break;
} // end switch
return v;
}
void DS1307::set(int c, int v) // Update buffer, then update the chip
{
switch(c)
{
case DS1307_SEC:
if(v<60 && v>-1)
{
//preserve existing clock state (running/stopped)
int state=rtc_bcd[DS1307_SEC] & DS1307_CLOCKHALT;
rtc_bcd[DS1307_SEC]=state | ((v / 10)<<4) + (v % 10);
}
break;
case DS1307_MIN:
if(v<60 && v>-1)
{
rtc_bcd[DS1307_MIN]=((v / 10)<<4) + (v % 10);
}
break;
case DS1307_HR:
// TODO : AM/PM 12HR/24HR
if(v<24 && v>-1)
{
rtc_bcd[DS1307_HR]=((v / 10)<<4) + (v % 10);
}
break;
case DS1307_DOW:
if(v<8 && v>-1)
{
rtc_bcd[DS1307_DOW]=v;
}
break;
case DS1307_DATE:
if(v<32 && v>-1)
{
rtc_bcd[DS1307_DATE]=((v / 10)<<4) + (v % 10);
}
break;
case DS1307_MTH:
if(v<13 && v>-1)
{
rtc_bcd[DS1307_MTH]=((v / 10)<<4) + (v % 10);
}
break;
case DS1307_YR:
if(v<50 && v>-1)
{
rtc_bcd[DS1307_YR]=((v / 10)<<4) + (v % 10);
}
break;
} // end switch
save();
}
void DS1307::stop(void)
{
// set the ClockHalt bit high to stop the rtc
// this bit is part of the seconds byte
rtc_bcd[DS1307_SEC]=rtc_bcd[DS1307_SEC] | DS1307_CLOCKHALT;
save();
}
void DS1307::start(void)
{
// unset the ClockHalt bit to start the rtc
// TODO : preserve existing seconds
rtc_bcd[DS1307_SEC]=0;
save();
}