Skip to content

Commit 5a2abec

Browse files
authored
Merge pull request #6100 from thinkyhead/rc_soft_pwm_dither
Enhancement of PWM, with dithering
2 parents 4433b63 + 043be28 commit 5a2abec

File tree

2 files changed

+71
-53
lines changed

2 files changed

+71
-53
lines changed

Marlin/Configuration.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,12 @@
14251425
// at zero value, there are 128 effective control positions.
14261426
#define SOFT_PWM_SCALE 0
14271427

1428+
// If SOFT_PWM_SCALE is set to a value higher than 0, dithering can
1429+
// be used to mitigate the associated resolution loss. If enabled,
1430+
// some of the PWM cycles are stretched so on average the wanted
1431+
// duty cycle is attained.
1432+
//#define SOFT_PWM_DITHER
1433+
14281434
// Temperature status LEDs that display the hotend and bed temperature.
14291435
// If all hotends and bed temperature and temperature setpoint are < 54C then the BLUE led is on.
14301436
// Otherwise the RED led is on. There is 1C hysteresis.

Marlin/temperature.cpp

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,8 @@ void Temperature::isr() {
15121512
static uint8_t temp_count = 0;
15131513
static TempState temp_state = StartupDelay;
15141514
static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
1515+
// avoid multiple loads of pwm_count
1516+
uint8_t pwm_count_tmp = pwm_count;
15151517

15161518
// Static members for each heater
15171519
#if ENABLED(SLOW_PWM_HEATERS)
@@ -1521,7 +1523,7 @@ void Temperature::isr() {
15211523
static uint8_t state_heater_ ## n = 0; \
15221524
static uint8_t state_timer_heater_ ## n = 0
15231525
#else
1524-
#define ISR_STATICS(n) static uint8_t soft_pwm_ ## n
1526+
#define ISR_STATICS(n) static uint8_t soft_pwm_ ## n = 0
15251527
#endif
15261528

15271529
// Statics per heater
@@ -1544,72 +1546,82 @@ void Temperature::isr() {
15441546
#endif
15451547

15461548
#if DISABLED(SLOW_PWM_HEATERS)
1549+
constexpr uint8_t pwm_mask =
1550+
#if ENABLED(SOFT_PWM_DITHER)
1551+
_BV(SOFT_PWM_SCALE) - 1
1552+
#else
1553+
0
1554+
#endif
1555+
;
1556+
15471557
/**
15481558
* Standard PWM modulation
15491559
*/
1550-
if (pwm_count == 0) {
1551-
soft_pwm_0 = soft_pwm[0];
1552-
WRITE_HEATER_0(soft_pwm_0 > 0 ? HIGH : LOW);
1560+
if (pwm_count_tmp >= 127) {
1561+
pwm_count_tmp -= 127;
1562+
soft_pwm_0 = (soft_pwm_0 & pwm_mask) + soft_pwm[0];
1563+
WRITE_HEATER_0(soft_pwm_0 > pwm_mask ? HIGH : LOW);
15531564
#if HOTENDS > 1
1554-
soft_pwm_1 = soft_pwm[1];
1555-
WRITE_HEATER_1(soft_pwm_1 > 0 ? HIGH : LOW);
1565+
soft_pwm_1 = (soft_pwm_1 & pwm_mask) + soft_pwm[1];
1566+
WRITE_HEATER_1(soft_pwm_1 > pwm_mask ? HIGH : LOW);
15561567
#if HOTENDS > 2
1557-
soft_pwm_2 = soft_pwm[2];
1558-
WRITE_HEATER_2(soft_pwm_2 > 0 ? HIGH : LOW);
1568+
soft_pwm_2 = (soft_pwm_2 & pwm_mask) + soft_pwm[2];
1569+
WRITE_HEATER_2(soft_pwm_2 > pwm_mask ? HIGH : LOW);
15591570
#if HOTENDS > 3
1560-
soft_pwm_3 = soft_pwm[3];
1561-
WRITE_HEATER_3(soft_pwm_3 > 0 ? HIGH : LOW);
1571+
soft_pwm_3 = (soft_pwm_3 & pwm_mask) + soft_pwm[3];
1572+
WRITE_HEATER_3(soft_pwm_3 > pwm_mask ? HIGH : LOW);
15621573
#endif
15631574
#endif
15641575
#endif
15651576

15661577
#if HAS_HEATER_BED
1567-
soft_pwm_BED = soft_pwm_bed;
1568-
WRITE_HEATER_BED(soft_pwm_BED > 0 ? HIGH : LOW);
1578+
soft_pwm_BED = (soft_pwm_BED & pwm_mask) + soft_pwm_bed;
1579+
WRITE_HEATER_BED(soft_pwm_BED > pwm_mask ? HIGH : LOW);
15691580
#endif
15701581

15711582
#if ENABLED(FAN_SOFT_PWM)
15721583
#if HAS_FAN0
1573-
soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1;
1574-
WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW);
1584+
soft_pwm_fan[0] = (soft_pwm_fan[0] & pwm_mask) + fanSpeedSoftPwm[0] >> 1;
1585+
WRITE_FAN(soft_pwm_fan[0] > pwm_mask ? HIGH : LOW);
15751586
#endif
15761587
#if HAS_FAN1
1577-
soft_pwm_fan[1] = fanSpeedSoftPwm[1] >> 1;
1578-
WRITE_FAN1(soft_pwm_fan[1] > 0 ? HIGH : LOW);
1588+
soft_pwm_fan[1] = (soft_pwm_fan[1] & pwm_mask) + fanSpeedSoftPwm[1] >> 1;
1589+
WRITE_FAN1(soft_pwm_fan[1] > pwm_mask ? HIGH : LOW);
15791590
#endif
15801591
#if HAS_FAN2
1581-
soft_pwm_fan[2] = fanSpeedSoftPwm[2] >> 1;
1582-
WRITE_FAN2(soft_pwm_fan[2] > 0 ? HIGH : LOW);
1592+
soft_pwm_fan[2] = (soft_pwm_fan[2] & pwm_mask) + fanSpeedSoftPwm[2] >> 1;
1593+
WRITE_FAN2(soft_pwm_fan[2] > pwm_mask ? HIGH : LOW);
15831594
#endif
15841595
#endif
15851596
}
1586-
1587-
if (soft_pwm_0 < pwm_count) WRITE_HEATER_0(0);
1588-
#if HOTENDS > 1
1589-
if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0);
1597+
else {
1598+
if (soft_pwm_0 <= pwm_count_tmp) WRITE_HEATER_0(0);
1599+
#if HOTENDS > 1
1600+
if (soft_pwm_1 <= pwm_count_tmp) WRITE_HEATER_1(0);
1601+
#endif
15901602
#if HOTENDS > 2
1591-
if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
1592-
#if HOTENDS > 3
1593-
if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
1594-
#endif
1603+
if (soft_pwm_2 <= pwm_count_tmp) WRITE_HEATER_2(0);
15951604
#endif
1596-
#endif
1597-
1598-
#if HAS_HEATER_BED
1599-
if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0);
1600-
#endif
1601-
1602-
#if ENABLED(FAN_SOFT_PWM)
1603-
#if HAS_FAN0
1604-
if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0);
1605+
#if HOTENDS > 3
1606+
if (soft_pwm_3 <= pwm_count_tmp) WRITE_HEATER_3(0);
16051607
#endif
1606-
#if HAS_FAN1
1607-
if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0);
1608+
1609+
#if HAS_HEATER_BED
1610+
if (soft_pwm_BED <= pwm_count_tmp) WRITE_HEATER_BED(0);
16081611
#endif
1609-
#if HAS_FAN2
1610-
if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0);
1612+
1613+
#if ENABLED(FAN_SOFT_PWM)
1614+
#if HAS_FAN0
1615+
if (soft_pwm_fan[0] <= pwm_count_tmp) WRITE_FAN(0);
1616+
#endif
1617+
#if HAS_FAN1
1618+
if (soft_pwm_fan[1] <= pwm_count_tmp) WRITE_FAN1(0);
1619+
#endif
1620+
#if HAS_FAN2
1621+
if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0);
1622+
#endif
16111623
#endif
1612-
#endif
1624+
}
16131625

16141626
// SOFT_PWM_SCALE to frequency:
16151627
//
@@ -1619,8 +1631,7 @@ void Temperature::isr() {
16191631
// 3: / 16 = 61.0352 Hz
16201632
// 4: / 8 = 122.0703 Hz
16211633
// 5: / 4 = 244.1406 Hz
1622-
pwm_count += _BV(SOFT_PWM_SCALE);
1623-
pwm_count &= 0x7F;
1634+
pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
16241635

16251636
#else // SLOW_PWM_HEATERS
16261637

@@ -1694,7 +1705,8 @@ void Temperature::isr() {
16941705
#endif
16951706

16961707
#if ENABLED(FAN_SOFT_PWM)
1697-
if (pwm_count == 0) {
1708+
if (pwm_count_tmp >= 127) {
1709+
pwm_count_tmp = 0;
16981710
#if HAS_FAN0
16991711
soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1;
17001712
WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW);
@@ -1709,15 +1721,15 @@ void Temperature::isr() {
17091721
#endif
17101722
}
17111723
#if HAS_FAN0
1712-
if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0);
1724+
if (soft_pwm_fan[0] <= pwm_count_tmp) WRITE_FAN(0);
17131725
#endif
17141726
#if HAS_FAN1
1715-
if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0);
1727+
if (soft_pwm_fan[1] <= pwm_count_tmp) WRITE_FAN1(0);
17161728
#endif
17171729
#if HAS_FAN2
1718-
if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0);
1730+
if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0);
17191731
#endif
1720-
#endif //FAN_SOFT_PWM
1732+
#endif // FAN_SOFT_PWM
17211733

17221734
// SOFT_PWM_SCALE to frequency:
17231735
//
@@ -1727,13 +1739,13 @@ void Temperature::isr() {
17271739
// 3: / 16 = 61.0352 Hz
17281740
// 4: / 8 = 122.0703 Hz
17291741
// 5: / 4 = 244.1406 Hz
1730-
pwm_count += _BV(SOFT_PWM_SCALE);
1731-
pwm_count &= 0x7F;
1742+
pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
17321743

1733-
// increment slow_pwm_count only every 64 pwm_count (e.g., every 8s)
1734-
if ((pwm_count % 64) == 0) {
1744+
// increment slow_pwm_count only every 64th pwm_count,
1745+
// i.e. yielding a PWM frequency of 16/128 Hz (8s).
1746+
if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
17351747
slow_pwm_count++;
1736-
slow_pwm_count &= 0x7f;
1748+
slow_pwm_count &= 0x7F;
17371749

17381750
// EXTRUDER 0
17391751
if (state_timer_heater_0 > 0) state_timer_heater_0--;
@@ -1749,7 +1761,7 @@ void Temperature::isr() {
17491761
#if HAS_HEATER_BED
17501762
if (state_timer_heater_BED > 0) state_timer_heater_BED--;
17511763
#endif
1752-
} // (pwm_count % 64) == 0
1764+
} // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
17531765

17541766
#endif // SLOW_PWM_HEATERS
17551767

0 commit comments

Comments
 (0)