@@ -750,6 +750,9 @@ void loadEEpromSettings()
750750// if (!eepromBuffer.comp_pwm) {
751751// eepromBuffer.bi_direction = 0;
752752// }
753+ if (eepromBuffer .pwm_dithering > 128 ) {
754+ eepromBuffer .pwm_dithering = 0 ;
755+ }
753756}
754757
755758void saveEEpromSettings ()
@@ -1240,6 +1243,18 @@ if (!stepper_sine && armed) {
12401243#endif
12411244}
12421245
1246+ /*
1247+ calculate a PWM dithering value
1248+ */
1249+ static uint16_t pwmDither (uint16_t reload , uint16_t dithering )
1250+ {
1251+ // simple LCG random number generator
1252+ static uint8_t seed ;
1253+ seed = (((uint32_t )seed ) * 1103515245U + 12345U );
1254+ int16_t r = (seed % (dithering * 2 )) - dithering ;
1255+ return reload + r ;
1256+ }
1257+
12431258void tenKhzRoutine ()
12441259{ // 20khz as of 2.00 to be renamed
12451260 duty_cycle = duty_cycle_setpoint ;
@@ -1408,10 +1423,19 @@ void tenKhzRoutine()
14081423 fast_accel = 0 ;
14091424 }
14101425 }
1426+
1427+ /*
1428+ possibly add PWM dithering to reduce impact of harmonics
1429+ */
1430+ uint16_t tim1_arr_used = tim1_arr ;
1431+ if (eepromBuffer .pwm_dithering > 0 ) {
1432+ tim1_arr_used = pwmDither (tim1_arr , eepromBuffer .pwm_dithering );
1433+ }
1434+
14111435 if ((armed && running ) && input > 47 ) {
14121436 if (eepromBuffer .variable_pwm ) {
14131437 }
1414- adjusted_duty_cycle = ((duty_cycle * tim1_arr ) / 2000 ) + 1 ;
1438+ adjusted_duty_cycle = ((duty_cycle * tim1_arr_used ) / 2000 ) + 1 ;
14151439
14161440 } else {
14171441
@@ -1422,7 +1446,7 @@ void tenKhzRoutine()
14221446 }
14231447 }
14241448 last_duty_cycle = duty_cycle ;
1425- SET_AUTO_RELOAD_PWM (tim1_arr );
1449+ SET_AUTO_RELOAD_PWM (tim1_arr_used );
14261450 SET_DUTY_CYCLE_ALL (adjusted_duty_cycle );
14271451 }
14281452#endif // ndef brushed_mode
0 commit comments