SOFT_PWM: Implement dithering if SOFT_PWM_SCALE is 1 or more

If dithering is enabled, the remainder of the soft_pwm_X duty value at
turnoff time is added to the next cycle. If e.g. the duty is set to 9 and
SCALE is set to 2, the PWM will be active for 8 counts for 3 cycles and
12 counts on each fourth cycle, i.e. the average is 9 cycles.

This compensates the resolution loss at higher scales and allows running
fans with SOFT_PWM with significantly reduced noise.

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
This commit is contained in:
Stefan Brüns 2017-03-12 06:00:26 +01:00 committed by Scott Lahteine
parent 2aed66a955
commit 35a55d5757
2 changed files with 32 additions and 18 deletions

View file

@ -1424,6 +1424,12 @@
// at zero value, there are 128 effective control positions.
#define SOFT_PWM_SCALE 0
// If SOFT_PWM_SCALE is set to a value higher than 0, dithering can
// be used to mitigate the associated resolution loss. If enabled,
// some of the PWM cycles are stretched so on average the wanted
// duty cycle is attained.
//#define SOFT_PWM_DITHER
// Temperature status LEDs that display the hotend and bed temperature.
// If all hotends and bed temperature and temperature setpoint are < 54C then the BLUE led is on.
// Otherwise the RED led is on. There is 1C hysteresis.

View file

@ -1521,7 +1521,7 @@ void Temperature::isr() {
static uint8_t state_heater_ ## n = 0; \
static uint8_t state_timer_heater_ ## n = 0
#else
#define ISR_STATICS(n) static uint8_t soft_pwm_ ## n
#define ISR_STATICS(n) static uint8_t soft_pwm_ ## n = 0
#endif
// Statics per heater
@ -1544,43 +1544,51 @@ void Temperature::isr() {
#endif
#if DISABLED(SLOW_PWM_HEATERS)
constexpr uint8_t pwm_mask =
#if ENABLED(SOFT_PWM_DITHER)
_BV(SOFT_PWM_SCALE) - 1
#else
0
#endif
;
/**
* Standard PWM modulation
*/
if (pwm_count >= 127) {
pwm_count = 0;
soft_pwm_0 = soft_pwm[0];
WRITE_HEATER_0(soft_pwm_0 > 0 ? HIGH : LOW);
pwm_count -= 127;
soft_pwm_0 = (soft_pwm_0 & pwm_mask) + soft_pwm[0];
WRITE_HEATER_0(soft_pwm_0 > pwm_mask ? HIGH : LOW);
#if HOTENDS > 1
soft_pwm_1 = soft_pwm[1];
WRITE_HEATER_1(soft_pwm_1 > 0 ? HIGH : LOW);
soft_pwm_1 = (soft_pwm_1 & pwm_mask) + soft_pwm[1];
WRITE_HEATER_1(soft_pwm_1 > pwm_mask ? HIGH : LOW);
#if HOTENDS > 2
soft_pwm_2 = soft_pwm[2];
WRITE_HEATER_2(soft_pwm_2 > 0 ? HIGH : LOW);
soft_pwm_2 = (soft_pwm_2 & pwm_mask) + soft_pwm[2];
WRITE_HEATER_2(soft_pwm_2 > pwm_mask ? HIGH : LOW);
#if HOTENDS > 3
soft_pwm_3 = soft_pwm[3];
WRITE_HEATER_3(soft_pwm_3 > 0 ? HIGH : LOW);
soft_pwm_3 = (soft_pwm_3 & pwm_mask) + soft_pwm[3];
WRITE_HEATER_3(soft_pwm_3 > pwm_mask ? HIGH : LOW);
#endif
#endif
#endif
#if HAS_HEATER_BED
soft_pwm_BED = soft_pwm_bed;
WRITE_HEATER_BED(soft_pwm_BED > 0 ? HIGH : LOW);
soft_pwm_BED = (soft_pwm_BED & pwm_mask) + soft_pwm_bed;
WRITE_HEATER_BED(soft_pwm_BED > pwm_mask ? HIGH : LOW);
#endif
#if ENABLED(FAN_SOFT_PWM)
#if HAS_FAN0
soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1;
WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW);
soft_pwm_fan[0] = (soft_pwm_fan[0] & pwm_mask) + fanSpeedSoftPwm[0] >> 1;
WRITE_FAN(soft_pwm_fan[0] > pwm_mask ? HIGH : LOW);
#endif
#if HAS_FAN1
soft_pwm_fan[1] = fanSpeedSoftPwm[1] >> 1;
WRITE_FAN1(soft_pwm_fan[1] > 0 ? HIGH : LOW);
soft_pwm_fan[1] = (soft_pwm_fan[1] & pwm_mask) + fanSpeedSoftPwm[1] >> 1;
WRITE_FAN1(soft_pwm_fan[1] > pwm_mask ? HIGH : LOW);
#endif
#if HAS_FAN2
soft_pwm_fan[2] = fanSpeedSoftPwm[2] >> 1;
WRITE_FAN2(soft_pwm_fan[2] > 0 ? HIGH : LOW);
soft_pwm_fan[2] = (soft_pwm_fan[2] & pwm_mask) + fanSpeedSoftPwm[2] >> 1;
WRITE_FAN2(soft_pwm_fan[2] > pwm_mask ? HIGH : LOW);
#endif
#endif
}