2525
2626#ifdef VARIABLE_SPINDLE
2727 static float pwm_gradient ; // Precalulated value to speed up rpm to PWM conversions.
28+ float spindle_pwm_period ;
29+ float spindle_pwm_off_value ;
30+ float spindle_pwm_min_value ;
31+ float spindle_pwm_max_value ;
2832#endif
2933
3034
3135void spindle_init ()
3236{
3337 #ifdef VARIABLE_SPINDLE
34- pwm_init (& SPINDLE_PWM_CHANNEL , SPINDLE_PWM_USE_PRIMARY_PIN , SPINDLE_PWM_USE_SECONDARY_PIN , SPINDLE_PWM_PERIOD , 0 );
38+ spindle_pwm_period = (SystemCoreClock / settings .spindle_pwm_freq );
39+ spindle_pwm_off_value = (spindle_pwm_period * settings .spindle_pwm_off_value / 100 );
40+ spindle_pwm_min_value = (spindle_pwm_period * settings .spindle_pwm_min_value / 100 );
41+ spindle_pwm_max_value = (spindle_pwm_period * settings .spindle_pwm_max_value / 100 );
42+ pwm_init (& SPINDLE_PWM_CHANNEL , SPINDLE_PWM_USE_PRIMARY_PIN , SPINDLE_PWM_USE_SECONDARY_PIN , spindle_pwm_period , 0 );
3543 pwm_enable (& SPINDLE_PWM_CHANNEL );
3644
3745 /* not ported
@@ -44,7 +52,7 @@ void spindle_init()
4452 #endif
4553 */
4654
47- pwm_gradient = (SPINDLE_PWM_MAX_VALUE - SPINDLE_PWM_MIN_VALUE )/(settings .rpm_max - settings .rpm_min );
55+ pwm_gradient = (spindle_pwm_max_value - spindle_pwm_min_value )/(settings .rpm_max - settings .rpm_min );
4856
4957 #else
5058 /* not ported
@@ -134,21 +142,21 @@ void spindle_stop()
134142 rpm *= (0.010 * sys .spindle_speed_ovr ); // Scale by spindle speed override value.
135143 if (rpm <= 0 ) {
136144 sys .spindle_speed = 0 ;
137- pwm_value = SPINDLE_PWM_OFF_VALUE ;
145+ pwm_value = spindle_pwm_off_value ;
138146 }
139147 else if (rpm <= settings .rpm_min ) {
140148 sys .spindle_speed = settings .rpm_min ;
141- pwm_value = SPINDLE_PWM_MIN_VALUE ;
149+ pwm_value = spindle_pwm_min_value ;
142150 }
143151 else if (rpm >= settings .rpm_max ) {
144152 sys .spindle_speed = settings .rpm_max ;
145- pwm_value = SPINDLE_PWM_MAX_VALUE - 1 ;
153+ pwm_value = spindle_pwm_max_value - 1 ;
146154 }
147155 else {
148156 sys .spindle_speed = rpm ;
149- pwm_value = floor ((rpm - settings .rpm_min ) * pwm_gradient ) + SPINDLE_PWM_MIN_VALUE ;
150- if (pwm_value >= SPINDLE_PWM_MAX_VALUE )
151- pwm_value = SPINDLE_PWM_MAX_VALUE - 1 ;
157+ pwm_value = floor ((rpm - settings .rpm_min ) * pwm_gradient ) + spindle_pwm_min_value ;
158+ if (pwm_value >= spindle_pwm_max_value )
159+ pwm_value = spindle_pwm_max_value - 1 ;
152160 }
153161 return (pwm_value );
154162 }
0 commit comments