I need to drive a stepper motor with a very specific acceleration/deceleration profile. The way I'd like to do it is to use a 16-bit timer with compare to generate the pulses to the external stepper driver. I'm hoping someone here knows the Arduino timers well enough to tell me if what I'd like to do is possible, before I take the time to study the datasheet to understand the specifics.
Here's what I have in mind:
The acceleration profile is stored in a 225-entry lookup table, with each entry being a single step period, in uSec. The profile provides a step-by-step acceleration ramp from 0 RPM up to the motors maximum RPM. To start motion, the timer would be cleared, then started counting at a rate of 1uSec/count. The compare register would be set to the value in the first table entry, which is the longest delay.
I am assuming the timer can be programmed to generate a rising edge on an output pin when the counter reaches the compare value. This will trigger a step from the stepper driver hardware. I assume I can also generate an interrupt when the compare match occurs. The interrupt handler will then reset the output pin, load the NEXT (shorter) entry from the table, add that value to the previous compare value, and write that value into the compare register. In this way, the active edge of each step pulse is created by hardware (so very low jitter), the counter counts up continuously, and the compare value always stays ahead of the counter (assuming interrupts are handled promptly), except for the brief period between the compare and the associated interrupt. Is that all do-able?
The above repeats until the last entry from the table is loaded into the compare register, at which time the motor is moving at maximum speed, so that last table entry is added to the compare register on each update. The motor therefore continues to move at maximum speed until a stop is commanded.
When a stop is commanded, the compare register is updated using values read from the table in reverse order, decelerating the motor using the acceleration profile in reverse.
Is there any reason this will not work? I am hoping to prototype it using a 328P Timer1, but hope it can eventually run on an ATTiny-1604 TimerB.
Any comments or problems with this approach?
Regards,
Ray L.