Browse Source

Merge pull request #6100 from thinkyhead/rc_soft_pwm_dither

Enhancement of PWM, with dithering
pull/1/head
Scott Lahteine 8 years ago
committed by GitHub
parent
commit
5a2abeca78
  1. 6
      Marlin/Configuration.h
  2. 96
      Marlin/temperature.cpp

6
Marlin/Configuration.h

@ -1425,6 +1425,12 @@
// at zero value, there are 128 effective control positions. // at zero value, there are 128 effective control positions.
#define SOFT_PWM_SCALE 0 #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. // 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. // 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. // Otherwise the RED led is on. There is 1C hysteresis.

96
Marlin/temperature.cpp

@ -1512,6 +1512,8 @@ void Temperature::isr() {
static uint8_t temp_count = 0; static uint8_t temp_count = 0;
static TempState temp_state = StartupDelay; static TempState temp_state = StartupDelay;
static uint8_t pwm_count = _BV(SOFT_PWM_SCALE); static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
// avoid multiple loads of pwm_count
uint8_t pwm_count_tmp = pwm_count;
// Static members for each heater // Static members for each heater
#if ENABLED(SLOW_PWM_HEATERS) #if ENABLED(SLOW_PWM_HEATERS)
@ -1521,7 +1523,7 @@ void Temperature::isr() {
static uint8_t state_heater_ ## n = 0; \ static uint8_t state_heater_ ## n = 0; \
static uint8_t state_timer_heater_ ## n = 0 static uint8_t state_timer_heater_ ## n = 0
#else #else
#define ISR_STATICS(n) static uint8_t soft_pwm_ ## n #define ISR_STATICS(n) static uint8_t soft_pwm_ ## n = 0
#endif #endif
// Statics per heater // Statics per heater
@ -1544,72 +1546,82 @@ void Temperature::isr() {
#endif #endif
#if DISABLED(SLOW_PWM_HEATERS) #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 * Standard PWM modulation
*/ */
if (pwm_count == 0) { if (pwm_count_tmp >= 127) {
soft_pwm_0 = soft_pwm[0]; pwm_count_tmp -= 127;
WRITE_HEATER_0(soft_pwm_0 > 0 ? HIGH : LOW); soft_pwm_0 = (soft_pwm_0 & pwm_mask) + soft_pwm[0];
WRITE_HEATER_0(soft_pwm_0 > pwm_mask ? HIGH : LOW);
#if HOTENDS > 1 #if HOTENDS > 1
soft_pwm_1 = soft_pwm[1]; soft_pwm_1 = (soft_pwm_1 & pwm_mask) + soft_pwm[1];
WRITE_HEATER_1(soft_pwm_1 > 0 ? HIGH : LOW); WRITE_HEATER_1(soft_pwm_1 > pwm_mask ? HIGH : LOW);
#if HOTENDS > 2 #if HOTENDS > 2
soft_pwm_2 = soft_pwm[2]; soft_pwm_2 = (soft_pwm_2 & pwm_mask) + soft_pwm[2];
WRITE_HEATER_2(soft_pwm_2 > 0 ? HIGH : LOW); WRITE_HEATER_2(soft_pwm_2 > pwm_mask ? HIGH : LOW);
#if HOTENDS > 3 #if HOTENDS > 3
soft_pwm_3 = soft_pwm[3]; soft_pwm_3 = (soft_pwm_3 & pwm_mask) + soft_pwm[3];
WRITE_HEATER_3(soft_pwm_3 > 0 ? HIGH : LOW); WRITE_HEATER_3(soft_pwm_3 > pwm_mask ? HIGH : LOW);
#endif #endif
#endif #endif
#endif #endif
#if HAS_HEATER_BED #if HAS_HEATER_BED
soft_pwm_BED = soft_pwm_bed; soft_pwm_BED = (soft_pwm_BED & pwm_mask) + soft_pwm_bed;
WRITE_HEATER_BED(soft_pwm_BED > 0 ? HIGH : LOW); WRITE_HEATER_BED(soft_pwm_BED > pwm_mask ? HIGH : LOW);
#endif #endif
#if ENABLED(FAN_SOFT_PWM) #if ENABLED(FAN_SOFT_PWM)
#if HAS_FAN0 #if HAS_FAN0
soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1; soft_pwm_fan[0] = (soft_pwm_fan[0] & pwm_mask) + fanSpeedSoftPwm[0] >> 1;
WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW); WRITE_FAN(soft_pwm_fan[0] > pwm_mask ? HIGH : LOW);
#endif #endif
#if HAS_FAN1 #if HAS_FAN1
soft_pwm_fan[1] = fanSpeedSoftPwm[1] >> 1; soft_pwm_fan[1] = (soft_pwm_fan[1] & pwm_mask) + fanSpeedSoftPwm[1] >> 1;
WRITE_FAN1(soft_pwm_fan[1] > 0 ? HIGH : LOW); WRITE_FAN1(soft_pwm_fan[1] > pwm_mask ? HIGH : LOW);
#endif #endif
#if HAS_FAN2 #if HAS_FAN2
soft_pwm_fan[2] = fanSpeedSoftPwm[2] >> 1; soft_pwm_fan[2] = (soft_pwm_fan[2] & pwm_mask) + fanSpeedSoftPwm[2] >> 1;
WRITE_FAN2(soft_pwm_fan[2] > 0 ? HIGH : LOW); WRITE_FAN2(soft_pwm_fan[2] > pwm_mask ? HIGH : LOW);
#endif #endif
#endif #endif
} }
else {
if (soft_pwm_0 < pwm_count) WRITE_HEATER_0(0); if (soft_pwm_0 <= pwm_count_tmp) 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_tmp) WRITE_HEATER_1(0);
#if HOTENDS > 2
if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
#if HOTENDS > 3
if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
#endif #endif
#if HOTENDS > 2
if (soft_pwm_2 <= pwm_count_tmp) WRITE_HEATER_2(0);
#endif #endif
#if HOTENDS > 3
if (soft_pwm_3 <= pwm_count_tmp) WRITE_HEATER_3(0);
#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_tmp) 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_tmp) 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_tmp) 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_tmp) WRITE_FAN2(0);
#endif #endif
#endif #endif
}
// SOFT_PWM_SCALE to frequency: // SOFT_PWM_SCALE to frequency:
// //
@ -1619,8 +1631,7 @@ void Temperature::isr() {
// 3: / 16 = 61.0352 Hz // 3: / 16 = 61.0352 Hz
// 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 = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
pwm_count &= 0x7F;
#else // SLOW_PWM_HEATERS #else // SLOW_PWM_HEATERS
@ -1694,7 +1705,8 @@ void Temperature::isr() {
#endif #endif
#if ENABLED(FAN_SOFT_PWM) #if ENABLED(FAN_SOFT_PWM)
if (pwm_count == 0) { if (pwm_count_tmp >= 127) {
pwm_count_tmp = 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,15 +1721,15 @@ 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_tmp) 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_tmp) 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_tmp) WRITE_FAN2(0);
#endif #endif
#endif //FAN_SOFT_PWM #endif // FAN_SOFT_PWM
// SOFT_PWM_SCALE to frequency: // SOFT_PWM_SCALE to frequency:
// //
@ -1727,13 +1739,13 @@ void Temperature::isr() {
// 3: / 16 = 61.0352 Hz // 3: / 16 = 61.0352 Hz
// 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 = pwm_count_tmp + _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) & 0x3F) == 0) {
slow_pwm_count++; slow_pwm_count++;
slow_pwm_count &= 0x7f; slow_pwm_count &= 0x7F;
// EXTRUDER 0 // EXTRUDER 0
if (state_timer_heater_0 > 0) state_timer_heater_0--; if (state_timer_heater_0 > 0) state_timer_heater_0--;
@ -1749,7 +1761,7 @@ void Temperature::isr() {
#if HAS_HEATER_BED #if HAS_HEATER_BED
if (state_timer_heater_BED > 0) state_timer_heater_BED--; if (state_timer_heater_BED > 0) state_timer_heater_BED--;
#endif #endif
} // (pwm_count % 64) == 0 } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
#endif // SLOW_PWM_HEATERS #endif // SLOW_PWM_HEATERS

Loading…
Cancel
Save