1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

STM32 PWM output accuracy issue.

Discussion in 'Embedded' started by microe, Jul 22, 2011.

  1. microe

    microe Guest

    Hello,

    I've got an STM32F103Z part and I'm trying to generate a PWM output betwee
    10Hz and 20Hz from the timer 1 peripheral.

    Depending on how I set the timer 1 prescale value (timer 1 is on the 72Mh
    high-speed bus), I get more or less accurate output results which can var
    by up to 1Hz from the target frequency.

    The only thing I can think of is that the external oscillator (8Mhz) i
    off, but I find this hard to believe since these are typically ver
    accurate over temperature.

    Any help on this would be much appreciated!



    ---------------------------------------
    Posted through http://www.EmbeddedRelated.com
    microe, Jul 22, 2011
    #1
    1. Advertising

  2. microe

    Rich Webb Guest

    On Fri, 22 Jul 2011 09:47:42 -0500, "microe"
    <ecusson@n_o_s_p_a_m.insight-tek.com> wrote:

    >Hello,
    >
    >I've got an STM32F103Z part and I'm trying to generate a PWM output between
    >10Hz and 20Hz from the timer 1 peripheral.
    >
    >Depending on how I set the timer 1 prescale value (timer 1 is on the 72Mhz
    >high-speed bus), I get more or less accurate output results which can vary
    >by up to 1Hz from the target frequency.


    An off-by-one error in setting one of the registers?

    I can play with this on a dev board later today or over the weekend. To
    check my understanding of the problem, it is that the base PWM period
    should be set to a pre-determined value between 50 and 100 msec but when
    you aim for, say 15 Hz (66.667 msec), the actual base period is
    somewhere between 71.4 msec (14 Hz) and 62.5 msec (16 Hz), sometimes on
    the high side and sometimes on the low. Correct?

    >The only thing I can think of is that the external oscillator (8Mhz) is
    >off, but I find this hard to believe since these are typically very
    >accurate over temperature.


    Are there also temperature extremes involved, or is this observed at
    room temperature with only self-heating occurring?

    --
    Rich Webb Norfolk, VA
    Rich Webb, Jul 22, 2011
    #2
    1. Advertising

  3. microe

    microe Guest

    >On Fri, 22 Jul 2011 09:47:42 -0500, "microe"
    ><ecusson@n_o_s_p_a_m.insight-tek.com> wrote:
    >
    >>Hello,
    >>
    >>I've got an STM32F103Z part and I'm trying to generate a PWM outpu

    between
    >>10Hz and 20Hz from the timer 1 peripheral.
    >>
    >>Depending on how I set the timer 1 prescale value (timer 1 is on th

    72Mhz
    >>high-speed bus), I get more or less accurate output results which ca

    vary
    >>by up to 1Hz from the target frequency.

    >
    >An off-by-one error in setting one of the registers?
    >
    >I can play with this on a dev board later today or over the weekend. To
    >check my understanding of the problem, it is that the base PWM period
    >should be set to a pre-determined value between 50 and 100 msec but when
    >you aim for, say 15 Hz (66.667 msec), the actual base period is
    >somewhere between 71.4 msec (14 Hz) and 62.5 msec (16 Hz), sometimes on
    >the high side and sometimes on the low. Correct?
    >
    >>The only thing I can think of is that the external oscillator (8Mhz) is
    >>off, but I find this hard to believe since these are typically very
    >>accurate over temperature.

    >
    >Are there also temperature extremes involved, or is this observed at
    >room temperature with only self-heating occurring?
    >
    >--
    >Rich Webb Norfolk, VA
    >


    Rich,

    Thanks for the reply.

    You are correct, I am comparing my expected vs. measured period for a give
    target frequency.

    Testing for this has been done at a regulated room temperature (70 deg F).

    I've tried two prescale values:
    1) 72 (1 million high-speed clock counts per Timer 1 clock tick).
    2) 720 (100K high-speed clock counts per Timer 1 clock tick).

    Using a prescale value of 72 results in noticably less accuracy over th
    10-20Hz range than using the 720 prescale value.

    I've also used other prescale counts with varying degrees of accuracy.
    see no trend towards better accuracy as I increase or decrease the Timer
    prescale value.


    ---------------------------------------
    Posted through http://www.EmbeddedRelated.com
    microe, Jul 23, 2011
    #3
  4. On Jul 23, 11:43 pm, "microe" <ecusson@n_o_s_p_a_m.insight-tek.com>
    wrote:
    > I've tried two prescale values:
    > 1) 72  (1 million high-speed clock counts per Timer 1 clock tick).
    > 2) 720 (100K high-speed clock counts per Timer 1 clock tick).
    >
    > Using a prescale value of 72 results in noticably less accuracy over the
    > 10-20Hz range than using the 720 prescale value.


    Errors of 1Hz are errors of 5-10%

    So this is not making much sense. Check your measurements & setups for
    noise.

    The classic uC timer reloads, and if you have 1MHz that should be a
    value from 100,000 to 50,000 for 10-20Hz, and your Frequency step
    size should be
    1/50001u = 19.999600007999840003Hz

    The 720 prescale will be 10x worse, 10,000..5000 and first Freq below
    20Hz is now 1/(5001*10u) = 19.996000Hz
    At the 10Hz end, you are better off, first step above 10.000Hz is
    1/((10000-1)*10u) = 10.001000100010001Hz
    Jim Granville, Jul 24, 2011
    #4
  5. microe

    dp Guest

    On Jul 24, 7:27 am, Jim Granville <> wrote:
    > On Jul 23, 11:43 pm, "microe" <ecusson@n_o_s_p_a_m.insight-tek.com>
    > wrote:
    >
    > > I've tried two prescale values:
    > > 1) 72  (1 million high-speed clock counts per Timer 1 clock tick).
    > > 2) 720 (100K high-speed clock counts per Timer 1 clock tick).

    >
    > > Using a prescale value of 72 results in noticably less accuracy over the
    > > 10-20Hz range than using the 720 prescale value.

    >
    > Errors of 1Hz are errors of 5-10%
    >
    > So this is not making much sense. Check your measurements & setups for
    > noise.


    Hi Jim,
    a guy in a local (Bulgarian) forum complained that the ADC of some
    STM family (likely the same) began doing erroneous conversions
    from time to time (rarely) when he switches on peripherals above
    a certain number. That is, if he keeps some of them off the error
    does not occur. Sounded like a ground bounce or whatever logic
    error on chip to me. May be this is the same issue.
    The guy here also said the new version of the chip had some external
    cap pins which the old version did not have, could be if they
    really had the issue they are throwing capacitors at it in the
    new version (he did not know if it worked, had not tested the new
    ones).

    But I have never used nor seen nor read any data about this chip,
    nor have I controlled the measurements, so this may well be totally
    wrong. Just communicating some (not mine) experience which might
    turn out to be relevant - or just wrong, however.

    Dimiter

    ------------------------------------------------------
    Dimiter Popoff Transgalactic Instruments

    http://www.tgi-sci.com
    ------------------------------------------------------
    http://www.flickr.com/photos/didi_tgi/sets/72157600228621276/
    dp, Jul 24, 2011
    #5
  6. microe

    Rich Webb Guest

    On Sat, 23 Jul 2011 06:43:47 -0500, "microe"
    <ecusson@n_o_s_p_a_m.insight-tek.com> wrote:

    >>On Fri, 22 Jul 2011 09:47:42 -0500, "microe"
    >><ecusson@n_o_s_p_a_m.insight-tek.com> wrote:
    >>
    >>>Hello,
    >>>
    >>>I've got an STM32F103Z part and I'm trying to generate a PWM output

    >between
    >>>10Hz and 20Hz from the timer 1 peripheral.
    >>>
    >>>Depending on how I set the timer 1 prescale value (timer 1 is on the

    >72Mhz
    >>>high-speed bus), I get more or less accurate output results which can

    >vary
    >>>by up to 1Hz from the target frequency.

    >>
    >>An off-by-one error in setting one of the registers?
    >>
    >>I can play with this on a dev board later today or over the weekend. To
    >>check my understanding of the problem, it is that the base PWM period
    >>should be set to a pre-determined value between 50 and 100 msec but when
    >>you aim for, say 15 Hz (66.667 msec), the actual base period is
    >>somewhere between 71.4 msec (14 Hz) and 62.5 msec (16 Hz), sometimes on
    >>the high side and sometimes on the low. Correct?
    >>
    >>>The only thing I can think of is that the external oscillator (8Mhz) is
    >>>off, but I find this hard to believe since these are typically very
    >>>accurate over temperature.

    >>
    >>Are there also temperature extremes involved, or is this observed at
    >>room temperature with only self-heating occurring?
    >>
    >>--
    >>Rich Webb Norfolk, VA
    >>

    >
    >Rich,
    >
    >Thanks for the reply.
    >
    >You are correct, I am comparing my expected vs. measured period for a given
    >target frequency.
    >
    >Testing for this has been done at a regulated room temperature (70 deg F).
    >
    >I've tried two prescale values:
    >1) 72 (1 million high-speed clock counts per Timer 1 clock tick).
    >2) 720 (100K high-speed clock counts per Timer 1 clock tick).
    >
    >Using a prescale value of 72 results in noticably less accuracy over the
    >10-20Hz range than using the 720 prescale value.


    72 shouldn't work at all (or at least, not as intended) since the
    auto-reload register (TIM1->ARR) is only 16 bits wide and you'd need to
    count to 100,000 for a 100 msec period with that prescale value.

    I set up the dev board with a prescale of 720 (for 100,000 ticks per
    second) and set the ARR and CCR to 10,000 and 1,000 respectively to aim
    for a 10 Hz PWM frequency and a 10% duty cycle. The results were pretty
    rock solid, even with the JTAG running. It hit right on 10 msec pulses
    with a 100 msec period; no cycle-to-cycle variation.

    Is it possible that you have UART1 also initialized? The STM32 chips do
    not allow two initialized peripherals to be connected to the same GPIO.
    They're a little less flexible in that regard as compared to some other
    processors that have registers to select "this" peripheral or "that"
    peripheral rather than just "the" peripheral.

    Here's how I got there:

    // Setup Timer1 for a PWM with a 10 Hz base period.
    // Aim the output at TIM1_CH1 on PA8.
    // We'll need to skip the setup of UART1, since they share pins, so...

    #define USING_TIMER1

    RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // give timer 1 a clock

    RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; // give port A a clock
    GPIOA->CRH &= ~GPIO_CRH_MODE8;
    GPIOA->CRH |= GPIO_CRH_MODE8_1; // use low speed output mode
    GPIOA->CRH &= ~GPIO_CRH_CNF8;
    GPIOA->CRH |= GPIO_CRH_CNF8_1; // alternate function, push-pull

    TIM1->PSC = 720; // 1e5 cps so 10 Hz is 10,000 counts
    TIM1->ARR = 10000; // start at 10 Hz
    TIM1->CCR1 = 1000; // and a 10% duty cycle
    TIM1->CCMR1 &= ~TIM_CCMR1_OC1M; // setup for PWM mode 1
    TIM1->CCMR1 |= (TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1);
    TIM1->CCMR1 |= TIM_CCMR1_OC1PE; // preload enable
    TIM1->CR1 |= TIM_CR1_ARPE; // auto reload preload enabled
    TIM1->CCER |= TIM_CCER_CC1E; // channel 1 output enabled
    TIM1->BDTR |= TIM_BDTR_MOE; // timer 1 main output enable
    TIM1->EGR |= TIM_EGR_UG; // update registers
    TIM1->CR1 |= TIM_CR1_CEN; // and enable the counter

    --
    Rich Webb Norfolk, VA
    Rich Webb, Jul 24, 2011
    #6
  7. On 23.07.2011 13:43, microe wrote:

    > You are correct, I am comparing my expected vs. measured period for a given
    > target frequency.
    >
    > Testing for this has been done at a regulated room temperature (70 deg F).
    >
    > I've tried two prescale values:
    > 1) 72 (1 million high-speed clock counts per Timer 1 clock tick).
    > 2) 720 (100K high-speed clock counts per Timer 1 clock tick).
    >
    > Using a prescale value of 72 results in noticably less accuracy over the
    > 10-20Hz range than using the 720 prescale value.


    That makes no sense whatsoever. If there's a difference at all, the
    higher timer base frequency timer should improve your accuracy, not
    impede it. So something must be fundamentally wrong with how you're
    doing this.

    I think we've reached a point here where there's no point in going on
    with the discussion if you don't show more details of what you're
    actually doing. As Linus T. is so fond of putting it: talk is cheap ---
    let's see source code.
    Hans-Bernhard Bröker, Jul 24, 2011
    #7
  8. microe

    microe Guest

    >On 23.07.2011 13:43, microe wrote:
    >
    >> You are correct, I am comparing my expected vs. measured period for

    given
    >> target frequency.
    >>
    >> Testing for this has been done at a regulated room temperature (70 de

    F).
    >>
    >> I've tried two prescale values:
    >> 1) 72 (1 million high-speed clock counts per Timer 1 clock tick).
    >> 2) 720 (100K high-speed clock counts per Timer 1 clock tick).
    >>
    >> Using a prescale value of 72 results in noticably less accuracy ove

    the
    >> 10-20Hz range than using the 720 prescale value.

    >
    >That makes no sense whatsoever. If there's a difference at all, the
    >higher timer base frequency timer should improve your accuracy, not
    >impede it. So something must be fundamentally wrong with how you're
    >doing this.
    >
    >I think we've reached a point here where there's no point in going on
    >with the discussion if you don't show more details of what you're
    >actually doing. As Linus T. is so fond of putting it: talk is cheap ---
    >let's see source code.
    >
    >


    ---------------------------------------
    Posted through http://www.EmbeddedRelated.com
    microe, Jul 24, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vic

    PIC 18Fxx2 PWM output

    Vic, Aug 27, 2003, in forum: Embedded
    Replies:
    4
    Views:
    501
  2. Bill

    Code to invert PWM output?

    Bill, Oct 10, 2004, in forum: Embedded
    Replies:
    5
    Views:
    1,013
    David Powell
    Oct 11, 2004
  3. Jim Stewart

    Re: PWM -> Audio Output

    Jim Stewart, Jun 11, 2009, in forum: Embedded
    Replies:
    3
    Views:
    346
    CBFalconer
    Jun 14, 2009
  4. Vladimir Vassilevsky

    Re: PWM -> Audio Output

    Vladimir Vassilevsky, Jun 11, 2009, in forum: Embedded
    Replies:
    0
    Views:
    325
    Vladimir Vassilevsky
    Jun 11, 2009
  5. Ben Bradley

    Re: PWM -> Audio Output

    Ben Bradley, Jun 12, 2009, in forum: Embedded
    Replies:
    1
    Views:
    1,677
Loading...

Share This Page