Attached is the code I ended up with.
I also attached a note with what I did on which line.
The code follows the MACH3 output very well.
I modified some lines here and there.
Great… here’s my notes as I go along thru the code…
- Ah, you swapped some pins around
Ok, modified the comment to match the actual pin numbers you have set.
Hmm, you moved A_Dri_Pin to pin 4
I Should have mentioned this I suppose…
Because the usual digitalRead() function takes quite a long time to execute, I ‘cheated’ and used direct Port I/O to handle the DIR pin.
the line #define DIR_PIN_BITMASK 0x08 // bit 3 of PORTD was used for this
now changed to:
#define A_DIR_PIN 4 // A Axis Dir connected to digital pin 4
#define DIR_PIN_BITMASK (0x01 << A_DIR_PIN) // A Axis Dir port pin Bit mask
The interrupt routine handles increment and decrement of the step count by testing the port with this bit mask:
if( PIND & DIR_PIN_BITMASK ) // test A_DIR_PIN
++isr_step_count; // HIGH= increment count
--isr_step_count; // LOW= decrement count
Override PWM if spindle stopped… Ok, I’ll put this at the top of loop()
The PWM value written to OCR1AL does continue to be output until told otherwise
it is done by hardware counters in the chip, once it’s running it just runs.
Ah… the Direction thing.
because the pin was changed, the interrupt routine was looking at the wrong pin
so you had to add something in here.
I think that can be taken out now.
Now that we want to completely disable PWM if the Spindle == LOW, I’ve added a couple of functions:
PWM_Output_On() and PWM_Output_Off() - which do what they say.
Note that PWM_Output_On() takes care of itself, it only resets and turns on the PWM if it is currently off.
PWM_Output_Off() turns off PWM and sets the output pin LOW
Added a couple of Serial.print()'s for testing… you can remove 'em later.
But when the Spindle_Pin returns to HIGH the PWM must return to the accumulated Step_Count_Val
…ok, PWM_Output_On() now restores the value
Right… tested that a bit and I’m gonna call it golden
Interesting little problem
MACH3_PWM.ino (4.85 KB)