2019-02-04 20:02:30 +01:00
|
|
|
/*
|
2019-02-17 19:21:08 +01:00
|
|
|
// Emulate a DCF77 radio receiver to control an external clock
|
2019-02-03 21:19:08 +01:00
|
|
|
//
|
2019-02-05 23:50:05 +01:00
|
|
|
// a nice & free logic test program for DCF77 can be found here:
|
|
|
|
https://www-user.tu-chemnitz.de/~heha/viewzip.cgi/hs/Funkuhr.zip/
|
|
|
|
//
|
2019-02-17 22:12:14 +01:00
|
|
|
// a DCF77 digital scope for Arduino boards can be found here:
|
|
|
|
https://github.com/udoklein/dcf77
|
|
|
|
//
|
2019-02-03 21:19:08 +01:00
|
|
|
*/
|
|
|
|
|
2019-02-08 22:19:44 +01:00
|
|
|
#ifdef HAS_DCF77
|
|
|
|
|
2019-02-03 21:19:08 +01:00
|
|
|
#include "dcf77.h"
|
|
|
|
|
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = "main";
|
|
|
|
|
2019-02-23 18:12:43 +01:00
|
|
|
// array of dcf pulses for one minute, secs 0..16 and 20 are never touched, so
|
|
|
|
// we initialize them statically to avoid dumb recalculation every minute
|
|
|
|
static uint8_t DCFpulse[DCF77_FRAME_SIZE] = {
|
|
|
|
dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero,
|
|
|
|
dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero,
|
|
|
|
dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_one};
|
2019-02-04 20:02:30 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
// triggered by 1 second timepulse to ticker out DCF signal
|
|
|
|
void DCF_Pulse(time_t t) {
|
2019-02-04 20:02:30 +01:00
|
|
|
|
2019-02-23 18:12:43 +01:00
|
|
|
TickType_t startTime = xTaskGetTickCount();
|
2019-02-17 19:21:08 +01:00
|
|
|
uint8_t sec = second(t);
|
2019-02-16 15:02:07 +01:00
|
|
|
|
2019-02-23 18:12:43 +01:00
|
|
|
ESP_LOGD(TAG, "DCF77 sec %d", sec);
|
2019-02-17 19:21:08 +01:00
|
|
|
|
|
|
|
// induce 10 pulses
|
|
|
|
for (uint8_t pulse = 0; pulse <= 9; pulse++) {
|
2019-02-04 20:02:30 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
switch (pulse) {
|
2019-02-03 21:19:08 +01:00
|
|
|
|
2019-02-04 20:02:30 +01:00
|
|
|
case 0: // start of second -> start of timeframe for logic signal
|
2019-02-17 19:21:08 +01:00
|
|
|
if (DCFpulse[sec] != dcf_off)
|
2019-02-23 18:12:43 +01:00
|
|
|
digitalWrite(HAS_DCF77, dcf_low);
|
2019-02-17 19:21:08 +01:00
|
|
|
else // 59th second reached, nothing more to do
|
|
|
|
return;
|
2019-02-04 23:42:17 +01:00
|
|
|
break;
|
2019-02-03 21:19:08 +01:00
|
|
|
|
2019-02-04 20:02:30 +01:00
|
|
|
case 1: // 100ms after start of second -> end of timeframe for logic 0
|
2019-02-17 19:21:08 +01:00
|
|
|
if (DCFpulse[sec] == dcf_zero)
|
2019-02-23 18:12:43 +01:00
|
|
|
digitalWrite(HAS_DCF77, dcf_high);
|
2019-02-04 23:42:17 +01:00
|
|
|
break;
|
2019-02-03 21:19:08 +01:00
|
|
|
|
2019-02-04 20:02:30 +01:00
|
|
|
case 2: // 200ms after start of second -> end of timeframe for logic 1
|
2019-02-23 18:12:43 +01:00
|
|
|
digitalWrite(HAS_DCF77, dcf_high);
|
2019-02-04 23:42:17 +01:00
|
|
|
break;
|
2019-02-03 21:19:08 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
case 9: // 900ms after start -> last pulse
|
|
|
|
return;
|
2019-02-04 20:02:30 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
} // switch
|
2019-02-05 23:50:05 +01:00
|
|
|
|
2019-02-23 18:12:43 +01:00
|
|
|
// impulse period pause
|
2019-02-17 19:21:08 +01:00
|
|
|
vTaskDelayUntil(&startTime, pdMS_TO_TICKS(DCF77_PULSE_LENGTH));
|
2019-02-05 23:50:05 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
} // for
|
|
|
|
} // DCF_Pulse()
|
2019-02-05 23:50:05 +01:00
|
|
|
|
2019-02-23 18:12:43 +01:00
|
|
|
uint8_t IRAM_ATTR DCF77_Frame(time_t tt) {
|
2019-02-07 20:39:32 +01:00
|
|
|
|
2019-02-16 15:02:07 +01:00
|
|
|
uint8_t Parity;
|
2019-02-07 20:39:32 +01:00
|
|
|
time_t t = myTZ.toLocal(tt); // convert to local time
|
|
|
|
|
2019-02-23 18:12:43 +01:00
|
|
|
// ENCODE DST CHANGE ANNOUNCEMENT (Sec 16)
|
|
|
|
DCFpulse[16] = dcf_zero; // not yet implemented
|
|
|
|
|
|
|
|
// ENCODE DAYLIGHTSAVING (secs 17..18)
|
|
|
|
DCFpulse[17] = myTZ.locIsDST(t) ? dcf_one : dcf_zero;
|
|
|
|
DCFpulse[18] = myTZ.locIsDST(t) ? dcf_zero : dcf_one;
|
2019-02-07 20:39:32 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
// ENCODE MINUTE (secs 21..28)
|
2019-02-16 15:02:07 +01:00
|
|
|
Parity = dec2bcd(minute(t), 21, 27, DCFpulse);
|
2019-02-23 18:12:43 +01:00
|
|
|
DCFpulse[28] = setParityBit(Parity);
|
2019-02-07 20:39:32 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
// ENCODE HOUR (secs 29..35)
|
2019-02-16 15:02:07 +01:00
|
|
|
Parity = dec2bcd(hour(t), 29, 34, DCFpulse);
|
2019-02-23 18:12:43 +01:00
|
|
|
DCFpulse[35] = setParityBit(Parity);
|
2019-02-07 20:39:32 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
// ENCODE DATE (secs 36..58)
|
2019-02-16 15:02:07 +01:00
|
|
|
Parity = dec2bcd(day(t), 36, 41, DCFpulse);
|
|
|
|
Parity += dec2bcd((weekday(t) - 1) ? (weekday(t) - 1) : 7, 42, 44, DCFpulse);
|
|
|
|
Parity += dec2bcd(month(t), 45, 49, DCFpulse);
|
|
|
|
Parity += dec2bcd(year(t) - 2000, 50, 57,
|
|
|
|
DCFpulse); // yes, we have a millenium 3000 bug here ;-)
|
2019-02-23 18:12:43 +01:00
|
|
|
DCFpulse[58] = setParityBit(Parity);
|
|
|
|
|
|
|
|
// ENCODE MARK (sec 59)
|
|
|
|
DCFpulse[59] = dcf_off; // !! missing code here for leap second !!
|
|
|
|
|
|
|
|
// return the minute for which this frame is generated
|
|
|
|
return minute(t);
|
|
|
|
|
|
|
|
} // DCF77_Frame()
|
|
|
|
|
|
|
|
// helper function to encode parity
|
|
|
|
uint8_t IRAM_ATTR setParityBit(uint8_t p) {
|
|
|
|
return ((p & 1) ? dcf_one : dcf_zero);
|
2019-02-07 20:39:32 +01:00
|
|
|
}
|
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
// helper function to convert decimal to bcd digit
|
|
|
|
uint8_t IRAM_ATTR dec2bcd(uint8_t dec, uint8_t startpos, uint8_t endpos,
|
2019-02-23 18:12:43 +01:00
|
|
|
uint8_t pArray[]) {
|
2019-02-17 19:21:08 +01:00
|
|
|
|
|
|
|
uint8_t data = (dec < 10) ? dec : ((dec / 10) << 4) + (dec % 10);
|
|
|
|
uint8_t parity = 0;
|
|
|
|
|
|
|
|
for (uint8_t n = startpos; n <= endpos; n++) {
|
|
|
|
pArray[n] = (data & 1) ? dcf_one : dcf_zero;
|
|
|
|
parity += (data & 1);
|
|
|
|
data >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parity;
|
|
|
|
}
|
|
|
|
|
2019-02-03 21:19:08 +01:00
|
|
|
#endif // HAS_DCF77
|