Teensy 4.1 Generating stepper motor pulses using interrupts

yellow-beak

New member
I want to control multiple stepper motors with STEP/DIR pulsemode drivers using a Teensy 4.1. To better understand the problem, I've been studying the source code of projects such as https://github.com/luni64/TeensyStep4, which gave me a rough idea of how this can be implemented. However, the library hasn't been actively updated in the last two years and is marked as experimental, so I'm unsure if it's a good reference.

At a high level, I set the STEP pin HIGH, wait briefly, and then set it LOW again. However, I'm unsure how to schedule this cycle properly. It's easy to implement using delay(), but that obviously blocks execution.

I'm wondering whether I should use a separate timer just to pull the pin LOW after n cycles, or instead introduce some state logic. For example, if I need 800 pulses per second, I could effectively double that rate and toggle the pin on every interrupt: setting it HIGH on one call and LOW on the next.

I haven't been able to figure this out from other people's source code. From what I can tell, they often take a different approach, using a fixed pulse width where the pin stays HIGH for a defined time before being pulled LOW again. That's different from my idea, where the HIGH and LOW phases would be symmetric in duration. But I can't figure out how they achieve this!
 
There's a thread where I made some updates to TeensyStep4 that was enough to get some useful examples working. It was not enough to satisfy the desire of users of TeensyStep(3), but it would be worth reading through. Just search for TeensyStep4 and messages from me.
 
There's a thread where I made some updates to TeensyStep4 that was enough to get some useful examples working. It was not enough to satisfy the desire of users of TeensyStep(3), but it would be worth reading through. Just search for TeensyStep4 and messages from me.
I actually looked at the source code of TeensyStep4 and was able to follow along but I'm still not sure how the pulsing is done.

Say there's a routine that figures out a pulse should take place every interval . The TMR timer triggers based on interval and sets the pin HIGH, but I'm not sure how it's set to low again. The driver I'm using requires 1.0 μs to requires the HIGH pin, after that I can pull it down again. How did you do it? Do you have a state linked to the channel, overwrite the value for the timer and let it trigger in 1.0 μs again to pull the pin low, reset the state and restore the old value for the timer?
 
The TMR is used in PWM mode. The high time is updated on each PWM period, so the signal goes high at the start of the period and then goes low at the end of the high time.
 
Back
Top