forked from rei-vilo/pic32_RTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic32_RTC.cpp
248 lines (201 loc) · 4.96 KB
/
pic32_RTC.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
//
// pic32_RTC
//
// © http://sites.google.com/site/vilorei
// CC = BY NC SA
//
// Jun 19, 2011 version 1 - initial release
// Nov 21, 2011 version 2 - all functions made private
// to avoid collusion between pic32_RTC and I2C_Clock
// Dec 24, 2011 version 3 - fix added by majenko to support mpide build 20111215
// Jan 15, 2012 version 4 - dayWeek fixed
//
// based on Paul_L chipKITRTCC library
//
// Additional 32.768 kHz crystal required.
//
// See
// Section 6: Oscillators
// Section 29: Real-Time Clock and Calendar (RTCC)
// from
// PIC32 Family Reference Manual on Microchip website
//
#ifndef __PIC32MX__
#error pic32_RTC requires PIC32 chip
#else
#include "WProgram.h"
#include "pic32_RTC.h"
#include <sys/attribs.h>
pic32_RTC::pic32_RTC() {
_year = 0;
_month = 0;
_day = 0;
_hour = 0;
_minute = 0;
_second = 0;
_dayWeekNumber=0;
}
String pic32_RTC::WhoAmI() {
String s;
s= "PIC32 Real Time Clock";
return s;
}
void pic32_RTC::begin() {
// Unlock OSCCON;
SYSKEY = 0x0;
SYSKEY = 0xAA996655;
SYSKEY = 0x556699AA;
// OSCCONbits.SOSCEN set
OSCCONSET = 2;
SYSKEY = 0x0;
}
void pic32_RTC::get() {
uint32_t rtct=RTCTIME;
uint32_t rtcd=RTCDATE;
rtct=rtct >>8;
_second = bcd2dec(rtct & 0xff);
rtct=rtct >>8;
_minute = bcd2dec(rtct & 0xff);
rtct=rtct >>8;
_hour = bcd2dec(rtct & 0xff); // 24 hours format
_dayWeekNumber=rtcd & 0x0f;
rtcd=rtcd >>8;
_day = bcd2dec(rtcd & 0xff);
rtcd=rtcd >>8;
_month = bcd2dec(rtcd & 0xff);
rtcd=rtcd >>8;
_year = bcd2dec(rtcd & 0xff); // 24 hours format
}
String pic32_RTC::time() {
String _time = "";
if (_hour<10) _time = _time + "0";
_time = _time + String(_hour, 10) + ":";
if (_minute<10) _time = _time + "0";
_time = _time + String(_minute, 10) + ":";
if (_second<10) _time = _time + "0";
_time = _time + String(_second, 10);
return _time;
}
String pic32_RTC::dayWeek() {
// array is 0-6, but the dow register holds 1-7, so subtract 1.
// 0 = Sunday
String days[7] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
return days[_dayWeekNumber];
}
String pic32_RTC::date() {
String _date = "";
if (_day<10) _date = _date + "0";
_date = _date + String(_day, 10) + "/";
if (_month<10) _date = _date + "0";
_date = _date + String(_month, 10) + "/";
_date = _date + String(_year, 10);
return _date;
}
String pic32_RTC::datetime() {
// Convenient mmddhhmm 8 characters string for file name
String _datetime = "";
if (_month<10) _datetime = _datetime + "0";
_datetime = _datetime + String(_month, 10);
if (_day<10) _datetime = _datetime + "0";
_datetime = _datetime + String(_day, 10);
if (_hour<10) _datetime = _datetime + "0";
_datetime = _datetime + String(_hour, 10);
if (_minute<10) _datetime = _datetime + "0";
_datetime = _datetime + String(_minute, 10);
return _datetime;
}
uint16_t pic32_RTC::year() {
return _year;
}
uint16_t pic32_RTC::month() {
return bcd2dec(_month);
}
uint16_t pic32_RTC::day() {
return _day;
}
uint16_t pic32_RTC::hour() {
return _hour;
}
uint16_t pic32_RTC::minute() {
return _minute;
}
uint16_t pic32_RTC::second() {
_second;
}
void pic32_RTC::set(uint16_t year0, uint16_t month0, uint16_t day0, uint16_t hour0, uint16_t minute0, uint16_t second0)
{
if (year0 < 100) {
year0 +=2000;
}
// Day of week calculation
// code by Tomohiko Sakamoto 0 = Sunday
// comp.lang.c on March 10th, 1993
uint16_t y;
uint16_t m;
uint16_t d;
uint16_t dow; // day of week
static uint16_t t[] = {
0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y = year0;
m = month0;
d = day0;
y -= m < 3;
dow = (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
_dayWeekNumber=dow;
uint32_t rtct;
rtct=dec2bcd(hour0);
rtct=(rtct<<8)+dec2bcd(minute0);
rtct=(rtct<<8)+dec2bcd(second0);
rtct=(rtct<<8);
uint32_t rtcd;
rtcd=dec2bcd(year0-2000);
rtcd=(rtcd<<8)+dec2bcd(month0);
rtcd=(rtcd<<8)+dec2bcd(day0);
rtcd=(rtcd<<8)+dow;
int wren = 1 ;
int temporar = 0 ;
if (RTCCONbits.RTCWREN == 0 ) {
writeEnable();
wren = 0;
};
RTCTIME = rtct;
RTCDATE = rtcd;
if ( wren = 0) {
writeDisable();
wren = 1;
}
get();
}
void pic32_RTC::writeEnable() {
//Unlock RTCCON;
SYSKEY = 0x0;
SYSKEY = 0xAA996655;
SYSKEY = 0x556699AA;
//RTCTIMEbits.RTCWREN set
RTCCONSET = 8;
SYSKEY = 0x0;
}
void pic32_RTC::writeDisable() {
//Unlock RTCCON;
SYSKEY = 0x0;
SYSKEY = 0xAA996655;
SYSKEY = 0x556699AA;
//RTCTIMEbits.RTCWREN clear
RTCCONCLR = 8;
SYSKEY = 0x0;
}
void pic32_RTC::reset() {
if (RTCCONbits.RTCWREN == 1) {
RTCTIME = 0;
}
}
// Convert normal decimal numbers to binary coded decimal
uint16_t pic32_RTC::dec2bcd(uint16_t val) {
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
uint16_t pic32_RTC::bcd2dec(uint16_t val) {
return ( (val/16*10) + (val%16) );
}
#endif // #if defined(__PIC32MX__)