Electronic – Count PWM Pulses fed into Stepper Motor FET Driver

microcontrollermosfet-driverpwmstepper motor

I am trying to (accurately) count the number of pulses fed to a stepper motor driver TI DRV8711. This driver "converts" one rising edge, depending on the settings, to a full step or microstep. The MCU I am using to generate those PWM pulses is a Freescale MPC5602D. The pulse frequency is going to be less than 30kHz per stepper motor.

The application I am using this device for is position control with a stepper motor. This requires accurate knowledge of the steps taken (given the stepper motor does not stall).

How are those kind of drivers normally driven? Using a regular GPIO pin that is asserted in a timer interrupt routine or via PWM? I want to avoid cramming the main loop with asserting and deasserting a GPIO pin. (I have to control 5+ stepper motors simultaneously)

Counting the PWM pulses sent to the driver is trivial with a regular GPIO pin.

On the other hand, how is one going to approach the problem of accurately counting the number of PWM pulses? Is this done by feeding the PWM output back to the MCU and using a counter to count the rising edges? I guess I have to decrease the PWM frequency before I reach the desired number of pulses in order to disable the PWM before the last pulse and thus guaranteeing not to "overshoot" the setpoint.

Best Answer

I know of three ways to accomplish what you need (and I have used all three). You have mentioned the first two in your comment.

Having an ISR count the step-pulses is the simplest. The ISR need only increment or decrement a position counter. In the 8-bit micros that I use, such an ISR would take less than a microsecond (although I code in assembly language, not C, on that MCU). It shouldn't be much overhead on any MCU.

The second way is to bring the step pulse into a counter. That could be difficult to manage if your motor runs in both directions, as you need to increment sometimes and decrement others (or just know which direction the count is in relation to). I used this method back in the 80's when counter/timer chips were typically used for motion control.

The most efficient way to control a stepper is with a separate rate-generator circuit, controlled by the MCU. A simple way to build one is to use the 7497 rate-multiplier chip. Each 7497 is six-bits, and you cascade them to get your desired resolution. However, their output pulse stream is not very even, which can cause instability in some applications (it can be filtered, however). A better technique is the adder/accumulator method, which gives a very clean output pulse stream, and is easily multiplexed to drive multiple motors (if you need that). I've had some 32-axis systems that used this approach. The Adder/accumulator (and the mux) fits very nicely in an FPGA.

The big advantage of a rate-generator is the simplicity of the software. The rate-generator gives you an interrupt at a fixed rate, which is your update period. In that ISR you simply load the number of steps you want executed in the next period. The update interrupts can be relatively infrequent, so overhead is low. The position is easy to maintain - you just add the value you load into the rate generator to your position counter. The velocity is easily controlled because it is in direct proportion to the number of steps you load into the rate-generator. Acceleration is easy to control as well - just add/subtract a fixed value on each update. If you have multiple motors, you would update them all in the same ISR.

(whew) I'm sorry if that was too long-winded.

Related Topic