Changing frequency when using analogWrite()

Posted on
  • I was using analogWrite() with two outputs (C7 and C8) to create two analog control signals and noticed that if I change the frequency (or forget to specify the frequency) on one output, the duty cycle of the other output is affected.
    So if I want to change the frequency, I have to specify the same frequency for both outputs, otherwise I will not get the correct duty cycle on both outputs..
    This may be worth mentioning, as it can save someone else from spending time troubleshooting.
    (I use LTC2645-12 to convert the PWM signal to a dc value, no low-pass filter is needed).

  • You can have different frequencies as long as they're on a different timer. But if you try to change frequency of one timer, it'll screw up any other PWM channels from the same timer.

    C6-9 are on the same timer (TIM8). You can see the timers on the reference page, by hovering over the PWM labels - but where there are more than one timer options, it won't tell you which one is being used. I think when there's a nice row of pins on one timer, that's the timer it uses.

    How about A0-3 (TIM5) or A8 (TIM1) or B6-9 (TIM2) or A6~B1 (TIM3 - I think this is all TIM3, though some of the pins are pretty messy with multiple supported timer).

  • Yes, it's a hardware issue - TIMx can only run at one speed, and if you modify the speed then it starts counting from 0 again - so messed up the duty cycle.

    I guess a nice addition (it was always the intention) would be to intelligently use a different timer if it avoided a conflict - however you'd still have cases where it just wasn't possible to get two different timers on certain pin combinations.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Changing frequency when using analogWrite()

Posted by Avatar for tage @tage

Actions