Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added inverted PWM signal (see #22) #1040

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions grbl/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@
// uncomment the config option USE_SPINDLE_DIR_AS_ENABLE_PIN below.
// #define INVERT_SPINDLE_ENABLE_PIN // Default disabled. Uncomment to enable.

// Inverts the PWM signal of the spindle. This can be used, if the inverter of the spindle requires
// inverted speed signals. Another example are some power supplies of the K40 lasers which fire the laser
// at 0V and disable them at 5V.
// #define INVERT_SPINDLE_PWM // Default disabled. Uncomment to enable.

// Inverts the selected coolant pin from low-disabled/high-enabled to low-enabled/high-disabled. Useful
// for some pre-built electronic boards.
// #define INVERT_COOLANT_FLOOD_PIN // Default disabled. Uncomment to enable.
Expand Down
37 changes: 32 additions & 5 deletions grbl/spindle_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
static float pwm_gradient; // Precalulated value to speed up rpm to PWM conversions.
#endif

static void enable_pwm() {
SPINDLE_TCCRA_REGISTER |= (1<<SPINDLE_COMB_BIT);
}

static void disable_pwm() {
SPINDLE_TCCRA_REGISTER &= ~(1<<SPINDLE_COMB_BIT);
}


void spindle_init()
{
Expand Down Expand Up @@ -98,7 +106,12 @@ uint8_t spindle_get_state()
void spindle_stop()
{
#ifdef VARIABLE_SPINDLE
SPINDLE_TCCRA_REGISTER &= ~(1<<SPINDLE_COMB_BIT); // Disable PWM. Output voltage is zero.
#ifdef INVERT_SPINDLE_PWM
enable_pwm(); // Enable PWM with full force. Output voltage is 5V.
SPINDLE_OCR_REGISTER = SPINDLE_PWM_MAX_VALUE; // Set PWM output level.
#else
disable_pwm(); // Disable PWM. Output voltage is zero.
#endif
#ifdef USE_SPINDLE_DIR_AS_ENABLE_PIN
#ifdef INVERT_SPINDLE_ENABLE_PIN
SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT); // Set pin to high
Expand All @@ -121,24 +134,38 @@ void spindle_stop()
// and stepper ISR. Keep routine small and efficient.
void spindle_set_speed(uint8_t pwm_value)
{
SPINDLE_OCR_REGISTER = pwm_value; // Set PWM output level.
uint8_t corr_pwm_value;
#ifdef INVERT_SPINDLE_PWM
corr_pwm_value = 255-pwm_value;
#else
corr_pwm_value = = pwm_value;
#endif
SPINDLE_OCR_REGISTER = corr_pwm_value;
#ifdef SPINDLE_ENABLE_OFF_WITH_ZERO_SPEED
if (pwm_value == SPINDLE_PWM_OFF_VALUE) {
spindle_stop();
} else {
SPINDLE_TCCRA_REGISTER |= (1<<SPINDLE_COMB_BIT); // Ensure PWM output is enabled.
enable_pwm(); // Ensure PWM output is enabled.
#ifdef INVERT_SPINDLE_ENABLE_PIN
SPINDLE_ENABLE_PORT &= ~(1<<SPINDLE_ENABLE_BIT);
#else
SPINDLE_ENABLE_PORT |= (1<<SPINDLE_ENABLE_BIT);
#endif
}
#else
#ifdef INVERT_SPINDLE_PWM
if (pwm_value == SPINDLE_PWM_MAX_VALUE) {
disable_pwm(); // Disable PWM. Output voltage is zero.
} else {
enable_pwm(); // Ensure PWM output is enabled.
}
#else
if (pwm_value == SPINDLE_PWM_OFF_VALUE) {
SPINDLE_TCCRA_REGISTER &= ~(1<<SPINDLE_COMB_BIT); // Disable PWM. Output voltage is zero.
disable_pwm(); // Disable PWM. Output voltage is zero.
} else {
SPINDLE_TCCRA_REGISTER |= (1<<SPINDLE_COMB_BIT); // Ensure PWM output is enabled.
enable_pwm(); // Ensure PWM output is enabled.
}
#endif
#endif
}

Expand Down