ESP32-PaxCounter/src/dcf77.cpp

112 lines
2.7 KiB
C++
Raw Normal View History

2019-02-04 20:02:30 +01:00
/*
// Emulate a DCF77 radio receiver to control an external clock
//
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
//
*/
#ifdef HAS_DCF77
#include "dcf77.h"
// Local logging tag
2019-02-27 00:52:27 +01:00
static const char TAG[] = __FILE__;
2019-02-23 20:28:11 +01:00
// triggered by second timepulse to ticker out DCF signal
2022-01-18 01:00:40 +01:00
void DCF77_Pulse(uint8_t bit) {
2019-02-04 20:02:30 +01:00
2022-01-19 14:25:17 +01:00
TickType_t startTime;
2019-02-27 22:40:58 +01:00
// induce a DCF Pulse
2022-01-19 14:25:17 +01:00
for (uint8_t pulseLength = 0; pulseLength <= 2; pulseLength++) {
2019-02-04 20:02:30 +01:00
2022-01-19 14:25:17 +01:00
startTime = xTaskGetTickCount(); // reference time pulse start
2022-01-19 14:25:17 +01:00
switch (pulseLength) {
case 0: // 0ms = start of pulse
2022-01-18 01:00:40 +01:00
digitalWrite(HAS_DCF77, dcf_low);
2019-02-04 23:42:17 +01:00
break;
2022-01-19 14:25:17 +01:00
case 1: // 100ms = logic 0
2022-01-18 01:00:40 +01:00
if (bit == 0)
2019-02-23 18:12:43 +01:00
digitalWrite(HAS_DCF77, dcf_high);
2019-02-04 23:42:17 +01:00
break;
2022-01-19 14:25:17 +01:00
case 2: // 200ms = logic 1
2019-02-23 18:12:43 +01:00
digitalWrite(HAS_DCF77, dcf_high);
2019-02-04 23:42:17 +01:00
break;
} // switch
2019-02-05 23:50:05 +01:00
2022-01-19 14:25:17 +01:00
// delay to genrate pulseLength
vTaskDelayUntil(&startTime, pdMS_TO_TICKS(100));
2019-02-05 23:50:05 +01:00
} // for
2019-02-23 20:28:11 +01:00
} // DCF77_Pulse()
2019-02-05 23:50:05 +01:00
2022-01-18 01:00:40 +01:00
// helper function to convert decimal to bcd digit
uint64_t dec2bcd(uint8_t const dec, uint8_t const startpos,
uint8_t const endpos, uint8_t *odd_parity) {
uint8_t data = (dec < 10) ? dec : ((dec / 10) << 4) + (dec % 10);
uint64_t bcd = 0;
2022-01-18 01:00:40 +01:00
*odd_parity = 0;
for (uint8_t i = startpos; i <= endpos; i++) {
bcd += (data & 1) ? set_dcfbit(i) : 0;
*odd_parity += (data & 1);
data >>= 1;
}
*odd_parity %= 2;
2019-02-07 20:39:32 +01:00
2022-01-18 01:00:40 +01:00
return bcd;
}
2019-02-07 20:39:32 +01:00
2022-01-18 01:00:40 +01:00
// generates a 1 minute dcf pulse frame for calendar time t
uint64_t DCF77_Frame(const struct tm t) {
2022-01-16 19:42:54 +01:00
2022-01-18 01:00:40 +01:00
uint8_t parity = 0, parity_sum = 0;
uint64_t frame = 0; // start with all bits 0
2022-01-16 19:42:54 +01:00
2022-01-18 01:00:40 +01:00
// DST CHANGE ANNOUNCEMENT (16)
// -- not implemented --
2022-01-16 19:42:54 +01:00
2022-01-18 01:00:40 +01:00
// DAYLIGHTSAVING (17, 18)
// "01" = MEZ / "10" = MESZ
2022-01-18 01:00:40 +01:00
frame += t.tm_isdst > 0 ? set_dcfbit(17) : set_dcfbit(18);
2019-02-07 20:39:32 +01:00
2022-01-18 01:00:40 +01:00
// LEAP SECOND (19)
// -- not implemented --
2019-02-07 20:39:32 +01:00
2022-01-18 01:00:40 +01:00
// BEGIN OF TIME INFORMATION (20)
frame += set_dcfbit(20);
2019-02-07 20:39:32 +01:00
2022-01-18 01:00:40 +01:00
// MINUTE (21..28)
frame += dec2bcd(t.tm_min, 21, 27, &parity);
frame += parity ? set_dcfbit(28) : 0;
2019-02-23 18:12:43 +01:00
2022-01-18 01:00:40 +01:00
// HOUR (29..35)
frame += dec2bcd(t.tm_hour, 29, 34, &parity);
frame += parity ? set_dcfbit(35) : 0;
2019-02-23 18:12:43 +01:00
2022-01-18 01:00:40 +01:00
// DATE (36..58)
frame += dec2bcd(t.tm_mday, 36, 41, &parity);
parity_sum += parity;
frame += dec2bcd((t.tm_wday == 0) ? 7 : t.tm_wday, 42, 44, &parity);
parity_sum += parity;
frame += dec2bcd(t.tm_mon + 1, 45, 49, &parity);
parity_sum += parity;
frame += dec2bcd(t.tm_year + 1900 - 2000, 50, 57, &parity);
parity_sum += parity;
frame += parity_sum % 2 ? set_dcfbit(58) : 0;
2019-02-23 18:12:43 +01:00
2022-01-18 01:00:40 +01:00
return frame;
2019-02-23 18:12:43 +01:00
2019-02-23 20:28:11 +01:00
} // DCF77_Frame()
2019-02-07 20:39:32 +01:00
#endif // HAS_DCF77