Skip to main content
Graduate
May 13, 2024
Solved

Long Period Timer PWM Setup

  • May 13, 2024
  • 2 replies
  • 1512 views

Hello,

I am attempting to set up a timer PWM output to have a set 600s period with a variable duty cycle based on a potentiometer input using an STM32G030J6 MCU.  I've setup PWM output timers before will little difficulty but this time I'm having a difficult time calculating the Prescaler and ARR values to accomplish this long of a period.  Are the timers just not capable of doing this long of a period and I need to us a different method to create this signal?

    This topic has been closed for replies.
    Best answer by Nicholas Yunker_2

    Uwe,

    I was able to figure this out before you got back to me using these equations:

    PWM Freq = Fclk/((PSC + 1) (ARR + 1))

    DC = CCR/ARR

    I'm running the MCU at 125kHz and setting ARR at the full value of 65535, this gives me a prescaler of 1143...this results in a Period of 599.8s when using an internal oscillator.

    Duty Cycle will vary with a potentiometer. I'm going to to do further testing with this tomorrow to see how good of resolution I can get out of this.

    2 replies

    Graduate II
    May 13, 2024

    What resolution do you need? What amount of Jitter do you allow? You get e.g. Millisecond resolution if you set up a 1 Millisecond interrupt and set up up a GPIO Pin accordingly.  A 16 bit timer  could only run at ~100 Hz. A 32 bit time at about 7 Mhz, if  a 32 bit timer is available on GO

    Nicholas Yunker_2AuthorAnswer
    Graduate
    May 13, 2024

    Uwe,

    I was able to figure this out before you got back to me using these equations:

    PWM Freq = Fclk/((PSC + 1) (ARR + 1))

    DC = CCR/ARR

    I'm running the MCU at 125kHz and setting ARR at the full value of 65535, this gives me a prescaler of 1143...this results in a Period of 599.8s when using an internal oscillator.

    Duty Cycle will vary with a potentiometer. I'm going to to do further testing with this tomorrow to see how good of resolution I can get out of this.

    Graduate II
    May 13, 2024

    The solution with the 1 ms ticker interrupt will have less jitter againt the 16 bit timer.

    Graduate
    May 14, 2024

    Jitter isn't a concern in my application.  I looked into the resolution and I was able to calculate it to be 16bits...the max for this MCU

    Resolution(bits) = log (ARR + 1) / log(2) = log(65536) / log(2) = 16 bits.

    I think this will be a good solution.  If jitter becomes an issue I'll go the 1ms ticker interrupt route.