Stepper routine using interrupts

Hi everyone!

I see the signals coming from one of the pins of an Arduino Nano with GRBL, it looks like GRBL uses fixed 10us pulses (that can be adjusted of course) and rest periods that varies in duration according to acceleration and deceleration needs, that means u have fixed positive pulse times and variable OFF times, obtaining a variable frequency system.

I have search and I know that this is implemented with interrupts and much of that is in the stepper.c file that I do not know how to use, SO how can this be done manually using timers and so on?

I mean I want to produce 10us pulses with variable periods that I can change in real time, NOT a normal PWM where u have a fixed period or frequency and variable positive pulses according to the duty cycle.

I have already done this using DelayMicroseconds in the loop BUT I want to know how to do it with interrupts and/or timers.

Thanks for any help.

SO how can this be done manually using timers and so on?

You need to tell us what you want to do - why not just use GRBL ?

Stepper Motor Basics
Simple Stepper Code

The 10us pulse duration is a spec from the driver manufacturer. The pulse must remain HIGH for that length of time so as to register with the driver hardware, almost like a de-bounce for a switch. Most modern drivers require a pulse of at least 1us. Some of the older chips (TB6560) spec a pulse duration of 30us however I have used these with less. The use of 10us is a good compromise. If you find yourself losing steps though, and you've got an older chip, look at increasing it.

As far as using interrupts, say for example you use timer1. Set for CTC and use OCR1A to set the frequency. You then place your pulse generator in ISR(TIMER1_COMPA) which simply turns HIGH the pin you use for clk. What I do then is set OCR1B to be TCNT1 + pulse_duration and enable the COMPB interrupt. In the ISR(TIMER1_COMPB) you then simply switch LOW the clk pin and disable it's own interrupt. This method allows for setting the pulse_duration independent of any other calculation and you can get on with speed control by manipulating OCR1A without concern.

Im NOT planning to use GRBL, I just want to use the same procedure to produce the pulses.
DKWatson when u say " OCR1A to set the frequency" u mean use Timer 1 to set the frecuency with for example:

TCCR1A = 0; //reset the register
TCCR1B = 0; //reset the register
TCCR1A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM00); //Phase-correct
TCCR1B = (TCCR1B & 0b11111000) | 0x03; //Prescaler

OCR1A is the duty cycle of the pin A from timer 1 right?

So as I understand u OCR1B can be use to "cut" the pulse initiated on Timer1 pin A?
can u write a sample code?

You need to read the datasheet and understand how the timer functions along with the interrupts. How does the pre-scaler affect the clock timing and how many ticks before you hit OCR1A and trigger a reset and interrupt. How can you use the interrupt service routine to cause OCR1B to activate and so on. You write the code and I and others will help sort out issues.

Im NOT planning to use GRBL,

I figured that out. I was wondering why? Knowing that will make it easier to help you.