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.

  • 1
    \$\begingroup\$ If you are generating the PWM, then you are using some kind of timer peripheral, very well capable of counting the pulses it is producing, or generating an interrupt each N pulses. \$\endgroup\$
    – Eugene Sh.
    May 26, 2016 at 18:56
  • \$\begingroup\$ @EugeneSh. I can only find an option to generate an interrupt every time a pulse is created. Counting these interrupts in an ISR seems like too much processing overhead for this task to me. Manually feeding the PWM output back to a pulse counter input doesn't seem too clean either. I am just wondering how this is normally done. \$\endgroup\$
    – fscheidl
    May 26, 2016 at 19:04
  • \$\begingroup\$ Normally I wouldn't use the PWM module at all, because steppers do not require variable duty cycle. Instead it might require variable frequency (for acceleration/deceleration). So a special peripheral or a special software driven regular GPIO will do. \$\endgroup\$
    – Eugene Sh.
    May 26, 2016 at 19:20
  • \$\begingroup\$ Exactly, this is why I am using a frequency and pulse width modulated output, still, for the sake of convenience, called a PWM output. The way you describe it was actually my first approach. TI is declaring the input as a "PWM" signal though, so I thought there had to be a more elegant solution. \$\endgroup\$
    – fscheidl
    May 26, 2016 at 19:25
  • \$\begingroup\$ The point here is that you are trying to get from a feedback whatever has to be a feedforward. You don't want the MC to tell you how many pulses sent, but you need to tell it how many to send. \$\endgroup\$
    – Eugene Sh.
    May 26, 2016 at 19:31

2 Answers 2


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.

  • \$\begingroup\$ Not too long-winded at all! Just what I was looking for! \$\endgroup\$
    – fscheidl
    May 26, 2016 at 20:09
  • \$\begingroup\$ Do you have further documentation about the "rate-generator"? I'm thinking of example circuits/applications, documentation, tutorials explaining how it works. \$\endgroup\$ Mar 6, 2017 at 8:23
  • \$\begingroup\$ @VictorLamoine - I'm sorry for the delayed reply, but I just received your comment. I will dig up an old schematic or two. I know I have a single-axis version that I had put in an FPGA (very readable), and I may have a multi-axis version. Do you need multi-axis? \$\endgroup\$
    – Mark
    Mar 9, 2017 at 23:39
  • \$\begingroup\$ Yes I need to controll 72 stepper motors synchronously. A single axis schematic would already be a good start for me! \$\endgroup\$ Mar 11, 2017 at 9:56
  • \$\begingroup\$ @VictorLamoine - 72 steppers is quite a bit, but within the capabilities of a multiplexed adder. Do you have an idea for an update rate? A maximum step rate? As those values increase, the clock-rate needs to increase. I can do the numbers for you, but maybe it should be a separate question. \$\endgroup\$
    – Mark
    Mar 13, 2017 at 6:24

Perhaps the simplest method of generating PWM pulses is to use an Omron CP1L-M30DT1-D PLC (This is an example part number as there are several additional models with high speed pulse output). You can not only generate PWM pulses with this device, but any number of predetermined standard pulses at 50% duty cycle for driving a stepper motor to a predetermined position. Acceleration and deceleration is also programmable, eliminating stepper motor driver errors. High speed counters are also available if you wish to feedback the pulses for verification, but usually unnecessary since the device will accurately out the pre-programmed number of pulses. I'm new to this group and don't think I can create a link to the PLC specification so perhaps someone else can create it for me.


High speed counter input and pulse output specifications are on page 3 of this documentation

  • \$\begingroup\$ Advocating the purchase of a modular, no doubt expensive piece of equipment which encapsulates all the functional details in its black-box interior is not really an answer to a question which seeks to understand how to implement this functionality in detail, nor really in the spirit of the site. The question you effectively are answering "what can I buy to solve this problem" is one that would be prohibited here as a shopping/selection question. \$\endgroup\$ May 27, 2016 at 4:46
  • \$\begingroup\$ Thanks @ChrisStratton understood. As mentioned in my comment, I'm new to the site and understood it as being solutions based, not limited to IC level solutions. My intent was to offer a low cost alternative solution, however still requiring an in depth understanding of programming for desired results. But thank you for the information and I'll keep this in mind when replying in future. \$\endgroup\$
    – Ted
    May 27, 2016 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.