Skip to content

Commit ff0782c

Browse files
committed
Correct shutdown sequence for phase-sync waveform generator in analogWrite.
1 parent 36b2bd3 commit ff0782c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

cores/esp8266/core_esp8266_wiring_pwm.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,18 @@ extern void __analogWriteMode(uint8_t pin, int val, bool openDrain) {
8383
// val: the duty cycle: between 0 (always off) and 255 (always on).
8484
// So if val = 0 we have digitalWrite(LOW), if we have val==range we have digitalWrite(HIGH)
8585
if (_setPWM(pin, val, analogScale)) {
86-
analogMap |= (1 << pin);
87-
} else if (startWaveformClockCycles(pin, high, low, 0, phaseReference, 0, true)) {
88-
analogMap |= (1 << pin);
86+
if (val > 0 && val < analogScale) analogMap |= (1 << pin);
87+
} else {
88+
const bool detach = (val == 0 || val == analogScale);
89+
// To go steady LOW or HIGH, let the waveform run into next duty cycle, if any. Then stop.
90+
if (startWaveformClockCycles(pin, high, low, static_cast<uint32_t>(detach), phaseReference, 0, true)) {
91+
if (detach) {
92+
delay((1000 + analogFreq) / analogFreq);
93+
stopWaveform(pin);
94+
} else {
95+
analogMap |= (1 << pin);
96+
}
97+
}
8998
}
9099
}
91100

0 commit comments

Comments
 (0)