This repository has been archived by the owner on Apr 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dcf.cpp
296 lines (256 loc) · 8.32 KB
/
Dcf.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
/*
* Class for decoding the DCF77 signal
*
* Implemented using the "Pollin DCF1" module with
* with inverted output signal (bit starts on falling edge).
*
* This source file can be found under:
* http://www.github.com/microfarad-de/Dcf
*
* This source file is used by the Nixie Clock Arduino firmware
* found under http://www.github.com/microfarad-de/nixie-clock
*
* Please visit:
* http://www.microfarad.de
* http://www.github.com/microfarad-de
*
* Copyright (C) 2019 Karim Hraibi (khraibi at gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Dcf.h"
/*
* Uncomment for activating debug output over the serial port
*/
//#define SERIAL_DEBUG
//#define SERIAL_BAUD 115200
//#define VERBOSE
// use these macros for printing to serial port
#ifdef SERIAL_DEBUG
#define PRINT(...) Serial.print (__VA_ARGS__)
#define PRINTLN(...) Serial.println (__VA_ARGS__)
#else
#define PRINT(...)
#define PRINTLN(...)
#endif
void dcfIsr (void);
DcfClass Dcf;
/***********************************
* Initialize the DCF receiver
***********************************/
void DcfClass::initialize (uint8_t dcfPin, uint8_t bitStart, uint8_t dcfPinMode) {
uint8_t pinMd;
this->dcfPin = dcfPin;
this->ledPin = ledPin;
this->startEdge = (bitStart == RISING ? HIGH : LOW);
pinMd = ( dcfPinMode == INPUT_PULLUP ? INPUT_PULLUP : INPUT);
pinMode (dcfPin, pinMd);
#ifdef SERIAL_DEBUG
Serial.begin (SERIAL_BAUD);
#endif
PRINTLN (" ");
PRINTLN ("+ + + D C F + + +");
PRINTLN (" ");
this->interrupt = digitalPinToInterrupt (dcfPin);
attachInterrupt (interrupt, dcfIsr, CHANGE);
this->isConfigured = true;
}
/*********/
/***********************************
* Read the DCF time
***********************************/
uint8_t DcfClass::getTime ( void) {
uint8_t rv = 41;
cli();
DcfBit_e bit = dcfBit;
sei();
// too many bits -> restart
if (idx >= DCF_BIT_COUNT) {
PRINT ("many bits... idx = ");
PRINTLN (idx, DEC);
idx = 0;
rv = 31;
}
// synchronization bit detected
else if (bit == DCF_BIT_SYNC) {
// full word of 59 bits received -> verify
if (idx == DCF_BIT_COUNT - 1) {
bits[idx] = DCF_BIT_LOW;
cli();
dcfBit = DCF_BIT_NONE;
sei();
lastBit = DCF_BIT_SYNC;
rv = verify();
PRINT ("full word... idx = ");
PRINTLN (idx, DEC);
PRINT ("verify = ");
PRINTLN (rv, DEC);
idx = 0;
}
// too few bits -> restart
else {
cli();
dcfBit = DCF_BIT_NONE;
sei();
lastBit = DCF_BIT_SYNC;
PRINT ("few bits... idx = ");
PRINTLN (idx, DEC);
idx = 0;
rv = 32;
}
}
// received a new bit
else if (bit == DCF_BIT_HIGH || bit == DCF_BIT_LOW) {
bits[idx] = bit;
lastBit = bit;
cli();
dcfBit = DCF_BIT_NONE;
sei();
idx++;
lastIdx = idx;
rv = 33;
#ifdef VERBOSE
PRINT (" - ");
PRINTLN (idx, DEC);
#endif
}
return rv;
}
/*********/
/***********************************
* Pause DCF reception
***********************************/
void DcfClass::pauseReception (void) {
if (!isConfigured) return;
detachInterrupt (interrupt);
lastIrqTrigger = !startEdge;
}
/*********/
/***********************************
* Resume DCF reception
***********************************/
void DcfClass::resumeReception (void) {
if (!isConfigured) return;
attachInterrupt (interrupt, dcfIsr, CHANGE);
}
/*********/
#ifdef VERBOSE
#define ISR_PRINT PRINTLN (delta, DEC);
#else
#define ISR_PRINT
#endif
/***********************************
* ISR triggered on both falling and rising edges of the DCF signal
***********************************/
void dcfIsr () {
uint32_t delta;
uint32_t ts;
uint8_t dcfPinValue;
ts = millis ();
dcfPinValue = digitalRead (Dcf.dcfPin);
Dcf.lastIrqTrigger = dcfPinValue; // store the value of of input pin for later use
if (dcfPinValue == Dcf.startEdge) {
delta = ts - Dcf.startEdgeTs; // measure distance between consecutive falling edges
if (delta > 2050) { ISR_PRINT } // not valid but needed for avoiding deadlock
else if (delta > 1950) { Dcf.dcfBit = DCF_BIT_SYNC; ISR_PRINT } // no falling edge for 2s = sync
else if (delta > 1050) { return; }
else if (delta > 950) { ISR_PRINT } // expected falling edge every 1s
else { return; }
Dcf.startEdgeTs = ts;
Dcf.rxFlag = 0;
}
else {
delta = ts - Dcf.startEdgeTs; // measure pulse width
if (Dcf.rxFlag == 1) { return; } // reject any subsequent pulses until the next bit starts
/* else if (delta > 260) { return; } */
else if (delta > 175) { Dcf.dcfBit = DCF_BIT_HIGH; Dcf.rxFlag = 1; ISR_PRINT} // 200ms pulse width = 1
/* else if (delta > 160) { return; } */
else if (delta > 50) { Dcf.dcfBit = DCF_BIT_LOW; Dcf.rxFlag = 1; ISR_PRINT} // 100ms pulse width = 0
else { return; }
}
}
/*********/
/***********************************
* Verifies the recieved DCF word for correctness
***********************************/
uint8_t DcfClass::verify (void) {
uint8_t i, sum;
uint8_t minutes = bits[21]*1 + bits[22]*2 + bits[23]*4 + bits[24]*8 + bits[25]*10 + bits[26]*20 + bits[27]*40;
uint8_t hours = bits[29]*1 + bits[30]*2 + bits[31]*4 + bits[32]*8 + bits[33]*10 + bits[34]*20;
uint8_t dayM = bits[36]*1 + bits[37]*2 + bits[38]*4 + bits[39]*8 + bits[40]*10 + bits[41]*20;
uint8_t dayW = bits[42]*1 + bits[43]*2 + bits[44]*4;
uint8_t month = bits[45]*1 + bits[46]*2 + bits[47]*4 + bits[48]*8 + bits[49]*10;
uint8_t year = bits[50]*1 + bits[51]*2 + bits[52]*4 + bits[53]*8 + bits[54]*10 + bits[55]*20 + bits[56]*40 + bits[57]*80;
uint8_t cest = bits[17];
uint8_t cet = bits[18];
#ifdef SERIAL_DEBUG
if (hours < 10) PRINT ("0"); // add leading zero
PRINT (hours, DEC);
PRINT (":");
if (minutes < 10) PRINT ("0");
PRINT (minutes, DEC);
PRINT (" ");
PRINT (dayW, DEC);
PRINT (" ");
if (dayM < 10) PRINT ("0");
PRINT (dayM, DEC);
PRINT (".");
if (month < 10) PRINT ("0");
PRINT (month, DEC);
PRINT (".");
if (year < 10) PRINT ("0");
PRINT (year, DEC);
PRINT (" ");
PRINT (cest, DEC);
PRINT (" ");
PRINT (cet, DEC);
PRINTLN (" ");
#endif
// populate output structure
currentTm.tm_sec = 0; // seconds after the minute - [ 0 to 59 ]
currentTm.tm_min = minutes; // minutes after the hour - [ 0 to 59 ]
currentTm.tm_hour = hours; // hours since midnight - [ 0 to 23 ]
currentTm.tm_mday = dayM; // day of the month - [ 1 to 31 ]
currentTm.tm_wday = dayW % 7; // days since Sunday - [ 0 to 6 ]
currentTm.tm_mon = month - 1; // months since January - [ 0 to 11 ]
currentTm.tm_year = year + 100; // years since 1900
//currentTm.tm_yday; // days since January 1 - [ 0 to 365 ]
//currentTm.tm_isdst = cest; // Daylight Saving Time flag
// sanity checks
if (bits[0] != 0) return 1;
if (bits[20] != 1) return 2;
if (bits[59] != 0) return 3;
if (minutes > 59) return 4;
if (hours > 23) return 5;
if (dayM == 0) return 6;
if (dayM > 31) return 7;
if (dayW == 0) return 8;
if (dayW > 7) return 9;
if (month == 0) return 10;
if (month > 12) return 11;
if (year > 99) return 12;
if (cest == cet) return 13;
// parity checks
sum = 0;
for (i = 21; i <= 28; i++) sum += bits[i];
if (sum % 2 != 0) return 21;
sum = 0;
for (i = 29; i <= 35; i++) sum += bits[i];
if (sum % 2 != 0) return 22;
sum = 0;
for (i = 36; i <= 58; i++) sum += bits[i];
if (sum % 2 != 0) return 23;
return 0;
}
/*********/