Shop Learn
DC motor speed control: NCO vs DUTY? — Parallax Forums

DC motor speed control: NCO vs DUTY?

According to the application note on P1 counters, NCO mode is for PWM motor control. I assume this means controlling a motor's speed by controlling the fraction of time that the motor is on.

Is there some reason DUTY mode couldn't do something similar? Modulating not pulse width per se, but rather the ratio of on time vs off time?


  • JonnyMacJonnyMac Posts: 7,568
    edited 2021-04-19 21:51

    For DC motors you really want to control PWM frequency and Duty Cycle. Unfortunately, there is no set-and-forget configuration of the counters to do this in the P1. In the P1, DUTY mode will let you change the duty cycle, but you have no control over frequency. In FREQ mode, you can control the frequency, but the duty cycle is fixed at 50%

    I've done DC motor control for camera platforms using the attached object. With two counters per cog you can easily and precisely control two motors. This code uses a synchronized loop to set the PWM frequency, and a counter mode to generate the pulse the corresponds to duty cycle.

  • mparkmpark Posts: 1,248

    I see. Thanks, @JonnyMac !

  • mparkmpark Posts: 1,248

    I have a different way to phrase my question. Suppose we have a 4-bit PWM counter and a 4-bit DUTY counter running off the same clock, and we want our motor to run at 5/16 power (about 33%). The PWM output would be
    1111100000000000 (repeating)
    and the DUTY output would be
    0001001001001001 (repeating)

    Five ons and 11 offs, but PWM mushes the ons together and DUTY spreads them out. Now, is there a reason to prefer one over the other? Spreading them out feels like a good thing to me, but maybe DC motors prefer them in a bunch?

  • JonnyMacJonnyMac Posts: 7,568
    edited 2021-04-23 14:52

    Theoretically, you're applying the same energy; when it comes to physics, though, the spread out pulses may not be able to get the motor moving at low duty cycles. I also think that, from a code standpoint, spreading those pulses evenly across your period might be messy. I reserve the right to be wrong. Show me I am and I'll try it.

    As I said before, the code I shared with you runs in several commercial products, including the a camera platform that provided some of the aerial shots from the 2020 Super Bowl.

  • AribaAriba Posts: 2,468

    The problem is that the pulses in duty mode are way too short for driving motors. For 50% duty with 80MHz clock you get a PWM frequency of 40 MHz. The FETs/Transistors in a motor driver IC can not switch that fast, they see such a signal more like a DC voltage and stay somwhere between on and off and get very hot. So the advantage of the PWM control get lost. PWM frequency for driving motors should be something like 2...20 kHz.


  • mparkmpark Posts: 1,248

    @JonnyMac said:
    ... I also think that, from a code standpoint, spreading those pulses evenly across your period might be messy. I reserve the right to be wrong. Show me I am and I'll try it.

    I think it's actually pretty straightforward. You have an n-bit counter (in my example, n = 4) and you want m pulses per period (m = 5 in my example); just repeatedly increment the counter by m and watch the carry out of the nth bit:

    0 <- 0101 (5, no carry)
    0 <- 1010 (10)
    0 <- 1111 (15)
    1 <- 0100 (carry!)
    0 <- 1001
    0 <- 1110
    1 <- 0011
    0 <- 1000
    0 <- 1101
    1 <- 0010
    0 <- 0111
    0 <- 1100
    1 <- 0001
    0 <- 0110
    0 <- 1011
    1 <- 0000 and repeat

    The carry bits are 0001001001001001; five ones spread out among 16 bits.

    Of course, "messy" is in the eye of the beholder, but if you do give it a try, I'd love to hear about it!

  • JonnyMacJonnyMac Posts: 7,568

    Yeah, I've seen that -- I refer to it as "accumulator rollover." It's fine for charging an RC circuit to create an analog output, but not for driving a motor.

Sign In or Register to comment.