diff --git a/tasmota/xsns_01_counter.ino b/tasmota/xsns_01_counter.ino index d87a88850..cfd09a8bf 100644 --- a/tasmota/xsns_01_counter.ino +++ b/tasmota/xsns_01_counter.ino @@ -52,6 +52,7 @@ struct COUNTER { struct AC_ZERO_CROSS_DIMMER { bool startReSync = false; // set to TRUE if zero-cross event occurs bool startMeasurePhase[MAX_COUNTERS] ; // set to TRUE if channel is ON and zero-cross occurs to initiate phase measure on channel + bool pwm_defined[MAX_COUNTERS]; bool PWM_ON[MAX_COUNTERS] ; // internal ON/OFF of the channel uint32_t current_cycle_ClockCycles = 0; // amount of clock cycles between two zero-cross events. uint32_t currentPWMCycleCount[MAX_COUNTERS] ; // clock cycle time of PWM channel, required to measure actual phase. [3] is phase of zero-cross @@ -93,12 +94,12 @@ void IRAM_ATTR CounterIsrArg(void *arg) { // restart initiated by setting Counter.startReSync = true; #ifdef USE_AC_ZERO_CROSS_DIMMER // if zero-cross events occur ond channel is on. phase on PWM must be measured - if ( Settings->flag4.zerocross_dimmer && ac_zero_cross_dimmer.startMeasurePhase[index] == true ) { + if ( ac_zero_cross_dimmer.startMeasurePhase[index] == true ) { ac_zero_cross_dimmer.currentPWMCycleCount[index] = ESP.getCycleCount(); ac_zero_cross_dimmer.startMeasurePhase[index] = false; } // if zero-cross event occurs (200ms window, 5-times a second) and device is online for 10sec - if (index == 3 && RtcSettings.pulse_counter[index]%(Settings->pwm_frequency / 5) == 0 && PinUsed(GPIO_CNTR1, index) && Settings->flag4.zerocross_dimmer && millis() > 10000) { + if (index == 3 && RtcSettings.pulse_counter[index]%(Settings->pwm_frequency / 5) == 0 && ac_zero_cross_dimmer.pwm_defined[index] && millis() > 10000) { ac_zero_cross_dimmer.currentPWMCycleCount[index] = ESP.getCycleCount(); // 1000µs to ensure not to fire on the next sinus wave if (ac_zero_cross_dimmer.lastCycleCount > 0) { @@ -164,11 +165,14 @@ void CounterInit(void) for (uint32_t i = 0; i < MAX_COUNTERS; i++) { if (PinUsed(GPIO_CNTR1, i)) { #ifdef USE_AC_ZERO_CROSS_DIMMER - - ac_zero_cross_dimmer.current_cycle_ClockCycles = ac_zero_cross_dimmer.tobe_cycle_timeClockCycles = microsecondsToClockCycles(1000000 / Settings->pwm_frequency); - // short fire on PWM to ensure not to hit next sinus curve but trigger the TRIAC. 0.78% of duty cycle (10ms) ~4µs - ac_zero_cross_dimmer.high = ac_zero_cross_dimmer.current_cycle_ClockCycles / 256; - + if (Settings->flag4.zerocross_dimmer) { + ac_zero_cross_dimmer.current_cycle_ClockCycles = ac_zero_cross_dimmer.tobe_cycle_timeClockCycles = microsecondsToClockCycles(1000000 / Settings->pwm_frequency); + // short fire on PWM to ensure not to hit next sinus curve but trigger the TRIAC. 0.78% of duty cycle (10ms) ~4µs + ac_zero_cross_dimmer.high = ac_zero_cross_dimmer.current_cycle_ClockCycles / 256; + if ((i < MAX_COUNTERS-1 && PinUsed(GPIO_PWM1, i)) || ( i == MAX_COUNTERS-1) ) { + ac_zero_cross_dimmer.pwm_defined[i] = true; + } + } #endif //USE_AC_ZERO_CROSS_DIMMER Counter.any_counter = true; pinMode(Pin(GPIO_CNTR1, i), bitRead(Counter.no_pullup, i) ? INPUT : INPUT_PULLUP); @@ -255,7 +259,7 @@ void SyncACDimmer(void) // currently only support one AC Dimmer PWM. Plan to support up to 4 Dimmer on same Phase. for (uint32_t i = 0; i < MAX_COUNTERS-1; i++) { if (Light.fade_start_10[i] == 0 && Light.fade_cur_10[i] == 0 && ac_zero_cross_dimmer.PWM_ON[i]==false ) continue; - if (PinUsed(GPIO_PWM1, i) && PinUsed(GPIO_CNTR1, i) && (ac_zero_cross_dimmer.startMeasurePhase[i] == 0 || ac_zero_cross_dimmer.PWM_ON[i] == false ) ) + if (ac_zero_cross_dimmer.pwm_defined[i] && (ac_zero_cross_dimmer.startMeasurePhase[i] == 0 || ac_zero_cross_dimmer.PWM_ON[i] == false ) ) { uint32_t phaseStart_ActualClockCycles; // As-Is positon of PWM after Zero Cross uint32_t phaseStart_ToBeClockCycles; // To be position after zero-cross to fire PWM start