Update xsns_01_counter.ino

This commit is contained in:
stefanbode 2020-07-01 17:46:13 +02:00 committed by GitHub
parent 2d3d4863b4
commit c11e948b81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 1 deletions

View File

@ -44,6 +44,9 @@ struct COUNTER {
bool any_counter = false;
} Counter;
uint32_t last_cycle;
uint32_t cycle_time;
#ifndef ARDUINO_ESP8266_RELEASE_2_3_0 // Fix core 2.5.x ISR not in IRAM Exception
void CounterUpdate(uint8_t index) ICACHE_RAM_ATTR;
void CounterUpdate1(void) ICACHE_RAM_ATTR;
@ -75,7 +78,32 @@ void CounterUpdate(uint8_t index)
Counter.timer_low_high[index] = time;
Counter.pin_state ^= (1<<index);
// do not count on rising edge
if bitRead(Counter.pin_state, index) return;
if bitRead(Counter.pin_state, index) {
// PWMfrequency 100
// restart PWM each second (german 50Hz has to up to 0.01% deviation)
// set COUNTERDEBOUNCELOW 1 to catch the raising edge
// Zero-HIGH is typical 2ms
if (RtcSettings.pulse_counter[index]%100 == 0 && PinUsed(GPIO_PWM1, index)) {
const uint32_t current_cycle = ESP.getCycleCount();
// stop pwm on PIN to start in Sync with rising edge
// calculate timeoffset to fire PWM
uint16_t dimm_time= 10000 * (100 - light_state.getDimmer(index)) / Settings.pwm_frequency;
digitalWrite(Pin(GPIO_PWM1, index), LOW);
// 1000µs to ensure not to fire on the next sinus wave
if (dimm_time < (1000000 / Settings.pwm_frequency)-1000) {
delayMicroseconds(dimm_time);
// fire small PWM signal to start TRIAC/SSR. Kill on next
// zero phase automatic.
// calculate actual cycle time and adapt frequency im milli Hz steps
// add 100.000 cpu ticks to ensure right step calculation
uint32_t steps = (current_cycle-last_cycle+100000)/(clockCyclesPerMicrosecond() * 10000);
cycle_time = (current_cycle-last_cycle)/steps;
analogWriteCCyPeriod(Pin(GPIO_PWM1, index), 5, cycle_time );
}
last_cycle = current_cycle;
}
return;
}
}
debounce_time = time - Counter.timer[index];