From 2aed66a955f023d36c3230aa7477e9d384a82f90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= Date: Sun, 12 Mar 2017 22:10:21 +0100 Subject: [PATCH] temperature: Fix SOFT_PWM off by one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A 128 step PWM has 127 intervals (0/127 ... 127/127 duty). Currently, a PWM setting of 1/127 is active for 2/128, i.e. double the expected time, or, in general n+1/128 instead of n/127. Fixes issue#6003. Signed-off-by: Stefan BrĂ¼ns --- Marlin/temperature.cpp | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 883133806..66390d8c7 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1547,7 +1547,8 @@ void Temperature::isr() { /** * Standard PWM modulation */ - if (pwm_count == 0) { + if (pwm_count >= 127) { + pwm_count = 0; soft_pwm_0 = soft_pwm[0]; WRITE_HEATER_0(soft_pwm_0 > 0 ? HIGH : LOW); #if HOTENDS > 1 @@ -1584,30 +1585,30 @@ void Temperature::isr() { #endif } - if (soft_pwm_0 < pwm_count) WRITE_HEATER_0(0); + if (soft_pwm_0 <= pwm_count) WRITE_HEATER_0(0); #if HOTENDS > 1 - if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0); + if (soft_pwm_1 <= pwm_count) WRITE_HEATER_1(0); #if HOTENDS > 2 - if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0); + if (soft_pwm_2 <= pwm_count) WRITE_HEATER_2(0); #if HOTENDS > 3 - if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0); + if (soft_pwm_3 <= pwm_count) WRITE_HEATER_3(0); #endif #endif #endif #if HAS_HEATER_BED - if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0); + if (soft_pwm_BED <= pwm_count) WRITE_HEATER_BED(0); #endif #if ENABLED(FAN_SOFT_PWM) #if HAS_FAN0 - if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0); + if (soft_pwm_fan[0] <= pwm_count) WRITE_FAN(0); #endif #if HAS_FAN1 - if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0); + if (soft_pwm_fan[1] <= pwm_count) WRITE_FAN1(0); #endif #if HAS_FAN2 - if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0); + if (soft_pwm_fan[2] <= pwm_count) WRITE_FAN2(0); #endif #endif @@ -1620,7 +1621,6 @@ void Temperature::isr() { // 4: / 8 = 122.0703 Hz // 5: / 4 = 244.1406 Hz pwm_count += _BV(SOFT_PWM_SCALE); - pwm_count &= 0x7F; #else // SLOW_PWM_HEATERS @@ -1694,7 +1694,8 @@ void Temperature::isr() { #endif #if ENABLED(FAN_SOFT_PWM) - if (pwm_count == 0) { + if (pwm_count >= 127) { + pwm_count = 0; #if HAS_FAN0 soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1; WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW); @@ -1709,13 +1710,13 @@ void Temperature::isr() { #endif } #if HAS_FAN0 - if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0); + if (soft_pwm_fan[0] <= pwm_count) WRITE_FAN(0); #endif #if HAS_FAN1 - if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0); + if (soft_pwm_fan[1] <= pwm_count) WRITE_FAN1(0); #endif #if HAS_FAN2 - if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0); + if (soft_pwm_fan[2] <= pwm_count) WRITE_FAN2(0); #endif #endif //FAN_SOFT_PWM @@ -1728,10 +1729,10 @@ void Temperature::isr() { // 4: / 8 = 122.0703 Hz // 5: / 4 = 244.1406 Hz pwm_count += _BV(SOFT_PWM_SCALE); - pwm_count &= 0x7F; - // increment slow_pwm_count only every 64 pwm_count (e.g., every 8s) - if ((pwm_count % 64) == 0) { + // increment slow_pwm_count only every 64th pwm_count, + // i.e. yielding a PWM frequency of 16/128 Hz (8s). + if (((pwm_count >> SOFT_PWM_SCALE) % 64) == 0) { slow_pwm_count++; slow_pwm_count &= 0x7f;