Browse Source

temperature: Fix SOFT_PWM off by one

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 <stefan.bruens@rwth-aachen.de>
pull/1/head
Stefan Brüns 8 years ago
committed by Scott Lahteine
parent
commit
2aed66a955
  1. 35
      Marlin/temperature.cpp

35
Marlin/temperature.cpp

@ -1547,7 +1547,8 @@ void Temperature::isr() {
/** /**
* Standard PWM modulation * Standard PWM modulation
*/ */
if (pwm_count == 0) { if (pwm_count >= 127) {
pwm_count = 0;
soft_pwm_0 = soft_pwm[0]; soft_pwm_0 = soft_pwm[0];
WRITE_HEATER_0(soft_pwm_0 > 0 ? HIGH : LOW); WRITE_HEATER_0(soft_pwm_0 > 0 ? HIGH : LOW);
#if HOTENDS > 1 #if HOTENDS > 1
@ -1584,30 +1585,30 @@ void Temperature::isr() {
#endif #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 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 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 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 #endif
#endif #endif
#if HAS_HEATER_BED #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 #endif
#if ENABLED(FAN_SOFT_PWM) #if ENABLED(FAN_SOFT_PWM)
#if HAS_FAN0 #if HAS_FAN0
if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0); if (soft_pwm_fan[0] <= pwm_count) WRITE_FAN(0);
#endif #endif
#if HAS_FAN1 #if HAS_FAN1
if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0); if (soft_pwm_fan[1] <= pwm_count) WRITE_FAN1(0);
#endif #endif
#if HAS_FAN2 #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
#endif #endif
@ -1620,7 +1621,6 @@ void Temperature::isr() {
// 4: / 8 = 122.0703 Hz // 4: / 8 = 122.0703 Hz
// 5: / 4 = 244.1406 Hz // 5: / 4 = 244.1406 Hz
pwm_count += _BV(SOFT_PWM_SCALE); pwm_count += _BV(SOFT_PWM_SCALE);
pwm_count &= 0x7F;
#else // SLOW_PWM_HEATERS #else // SLOW_PWM_HEATERS
@ -1694,7 +1694,8 @@ void Temperature::isr() {
#endif #endif
#if ENABLED(FAN_SOFT_PWM) #if ENABLED(FAN_SOFT_PWM)
if (pwm_count == 0) { if (pwm_count >= 127) {
pwm_count = 0;
#if HAS_FAN0 #if HAS_FAN0
soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1; soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1;
WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW); WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW);
@ -1709,13 +1710,13 @@ void Temperature::isr() {
#endif #endif
} }
#if HAS_FAN0 #if HAS_FAN0
if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0); if (soft_pwm_fan[0] <= pwm_count) WRITE_FAN(0);
#endif #endif
#if HAS_FAN1 #if HAS_FAN1
if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0); if (soft_pwm_fan[1] <= pwm_count) WRITE_FAN1(0);
#endif #endif
#if HAS_FAN2 #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
#endif //FAN_SOFT_PWM #endif //FAN_SOFT_PWM
@ -1728,10 +1729,10 @@ void Temperature::isr() {
// 4: / 8 = 122.0703 Hz // 4: / 8 = 122.0703 Hz
// 5: / 4 = 244.1406 Hz // 5: / 4 = 244.1406 Hz
pwm_count += _BV(SOFT_PWM_SCALE); pwm_count += _BV(SOFT_PWM_SCALE);
pwm_count &= 0x7F;
// increment slow_pwm_count only every 64 pwm_count (e.g., every 8s) // increment slow_pwm_count only every 64th pwm_count,
if ((pwm_count % 64) == 0) { // 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++;
slow_pwm_count &= 0x7f; slow_pwm_count &= 0x7f;

Loading…
Cancel
Save