I’ve been experimenting driving a bipolar stepper motor for a robotics project I’m working on. I am using an Alegro A3980 translator/driver chip. This is a very simple device that only requires a step pulse and direction as well as a few discrete components.For my application I need to be able to drive the stepper motor between 50 and 1000 steps per second. Ideally these pulses should at a regular frequency. I’ve tried a number of methods using a 28X1 chip but so far I don’t have an ideal solution.
Method 1
The simplest solution is to generate step pulses from the main program loop, however, it is difficult to control the speed as this depends on the amount of code in the main program loop.
Method 2
I then thought about using the timer interrupt. By over clocking the 28x1 to 16mHz. I’ve been able to get the timer interrupt working at about 500Hz. At this frequency ½ the processing time is spent in the interrupt loop and the other half can be used by the main program loop. This works reasonably well but there is significant timing jitter with interrupt handling. I’ve also noticed that the pause command no longer works in the main program loop which is causing a number of other problems. I would have expected the pause delay to be longer than defined, but the pause command just drops straight out when using fast timer interrupts. I really need to try and get this stepper motor working faster and with less jitter – As far as I can see It can’t be done well using interrupts.
Method 3
The third solution is to use the pwmout command, however, this works too quickly. Even operating with the 4mhz clock, the frequency range seems to be 4KHz to 1Mhz and is far faster than the 50-1000Hz range I need. Is there any way of making the pwmout command work at lower frequencies? Looking at the microchip data sheet it looks like it may be possible to put a pre-scalar counter onto the pwm timer but as far as I can see this only offers a factor of 16. Also I can’t see any way of programming this feature. If all else fails I guess I could add a divide by 1000 counter onto the pwm output but it would be nice to try and keep the design to a single chip.
Method 4
At the moment I’m still working on this one. Any ideas on producing a constant (but programmable) frequency in the range 50 to 1000Hz whilst still allowing a main program thread to run would be appreciated.
Method 1
The simplest solution is to generate step pulses from the main program loop, however, it is difficult to control the speed as this depends on the amount of code in the main program loop.
Method 2
I then thought about using the timer interrupt. By over clocking the 28x1 to 16mHz. I’ve been able to get the timer interrupt working at about 500Hz. At this frequency ½ the processing time is spent in the interrupt loop and the other half can be used by the main program loop. This works reasonably well but there is significant timing jitter with interrupt handling. I’ve also noticed that the pause command no longer works in the main program loop which is causing a number of other problems. I would have expected the pause delay to be longer than defined, but the pause command just drops straight out when using fast timer interrupts. I really need to try and get this stepper motor working faster and with less jitter – As far as I can see It can’t be done well using interrupts.
Method 3
The third solution is to use the pwmout command, however, this works too quickly. Even operating with the 4mhz clock, the frequency range seems to be 4KHz to 1Mhz and is far faster than the 50-1000Hz range I need. Is there any way of making the pwmout command work at lower frequencies? Looking at the microchip data sheet it looks like it may be possible to put a pre-scalar counter onto the pwm timer but as far as I can see this only offers a factor of 16. Also I can’t see any way of programming this feature. If all else fails I guess I could add a divide by 1000 counter onto the pwm output but it would be nice to try and keep the design to a single chip.
Method 4
At the moment I’m still working on this one. Any ideas on producing a constant (but programmable) frequency in the range 50 to 1000Hz whilst still allowing a main program thread to run would be appreciated.