From 0d54299aafc5b5cae76a2324a5472ccd399d3281 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 23 Mar 2021 15:42:15 +0100 Subject: [PATCH] Make ZCD user configurable using GPIO ZC Pulse --- tasmota/support_tasmota.ino | 18 ++++++++++++------ tasmota/xnrg_19_cse7761.ino | 8 ++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 4dbaae508..9ca5da0bd 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -169,7 +169,7 @@ void ZeroCrossMomentStart(void) { while (!TimeReached(timeout) && !TimeReachedUsec(trigger_moment)) {} // uint32_t dbg_endtime = micros(); -// AddLog(LOG_LEVEL_DEBUG, PSTR("ZCR: CodeExecTime %d, StartTime %d, EndTime %d, ZcTime %d, Interval %d"), +// AddLog(LOG_LEVEL_DEBUG, PSTR("ZCD: CodeExecTime %d, StartTime %d, EndTime %d, ZcTime %d, Interval %d"), // dbg_endtime - dbg_starttime, dbg_starttime, dbg_endtime, dbg_zctime, dbg_interval); TasmotaGlobal.zc_code_offset = micros(); @@ -180,7 +180,7 @@ void ZeroCrossMomentEnd(void) { TasmotaGlobal.zc_code_offset = (micros() - TasmotaGlobal.zc_code_offset) / 2; -// AddLog(LOG_LEVEL_DEBUG, PSTR("ZCR: CodeExecTime %d"), TasmotaGlobal.zc_code_offset * 2); +// AddLog(LOG_LEVEL_DEBUG, PSTR("ZCD: CodeExecTime %d"), TasmotaGlobal.zc_code_offset * 2); } void ICACHE_RAM_ATTR ZeroCrossIsr(void) { @@ -190,10 +190,16 @@ void ICACHE_RAM_ATTR ZeroCrossIsr(void) { if (!TasmotaGlobal.zc_time) {TasmotaGlobal.zc_time = 1; } } -void ZeroCrossInit(uint32_t gpio, uint32_t offset) { - TasmotaGlobal.zc_offset = offset; - pinMode(gpio, INPUT_PULLUP); - attachInterrupt(gpio, ZeroCrossIsr, CHANGE); +void ZeroCrossInit(uint32_t offset) { + if (PinUsed(GPIO_ZEROCROSS)) { + TasmotaGlobal.zc_offset = offset; + + uint32_t gpio = Pin(GPIO_ZEROCROSS); + pinMode(gpio, INPUT_PULLUP); + attachInterrupt(gpio, ZeroCrossIsr, CHANGE); + + AddLog(LOG_LEVEL_INFO, PSTR("ZCD: Activated")); // Zero-cross detection activated + } } /********************************************************************************************/ diff --git a/tasmota/xnrg_19_cse7761.ino b/tasmota/xnrg_19_cse7761.ino index fb078544d..103b4f3c6 100644 --- a/tasmota/xnrg_19_cse7761.ino +++ b/tasmota/xnrg_19_cse7761.ino @@ -21,8 +21,13 @@ #ifdef USE_CSE7761 /*********************************************************************************************\ * CSE7761 - Energy (Sonoff Dual R3 Pow) + * + * Without zero-cross detection * {"NAME":"Sonoff Dual R3","GPIO":[32,0,0,0,0,0,0,0,0,576,225,0,0,0,0,0,0,0,0,0,0,7296,7328,224,0,0,0,0,160,161,0,0,0,0,0,0],"FLAG":0,"BASE":1} * + * With zero-cross detection + * {"NAME":"Sonoff Dual R3 (ZCD)","GPIO":[32,0,0,0,7552,0,0,0,0,576,225,0,0,0,0,0,0,0,0,0,0,7296,7328,224,0,0,0,0,160,161,0,0,0,0,0,0],"FLAG":0,"BASE":1} + * * Based on datasheet from ChipSea and analysing serial data * See https://github.com/arendst/Tasmota/discussions/10793 \*********************************************************************************************/ @@ -33,7 +38,6 @@ #define CSE7761_FREQUENCY // Add support for frequency monitoring #define CSE7761_ZEROCROSS // Add zero cross detection #define CSE7761_ZEROCROSS_OFFSET 2200 // Zero cross offset due to chip calculation (microseconds) - #define CSE7761_ZEROCROSS_GPIO 4 // Sonoff Dual R3 pulse input #define CSE7761_UREF 42563 // RmsUc #define CSE7761_IREF 52241 // RmsIAC @@ -576,7 +580,7 @@ void Cse7761SnsInit(void) { #ifdef CSE7761_FREQUENCY #ifdef CSE7761_ZEROCROSS - ZeroCrossInit(CSE7761_ZEROCROSS_GPIO, CSE7761_ZEROCROSS_OFFSET); + ZeroCrossInit(CSE7761_ZEROCROSS_OFFSET); #endif // CSE7761_ZEROCROSS #endif // CSE7761_FREQUENCY