Skip to main content
Explorer
December 15, 2023
Question

Some confusion about the configuration of output compare

  • December 15, 2023
  • 7 replies
  • 12327 views

Some confusion about the configuration of output compare

Hello, I am a beginner in STM32 MCUs. The MCU I am using is STM32F103ZET6.

I'm trying to use the output compare function of the timer.

But I came across something that confused me.

I use STM32CubeMX to configure my MCU.

I configured TIM3's output cmpare function with the Channel1 enabled.

I configured the parameters as follows:

PSC=0,

ARR=39,

Output Compare Channel1: Mode = Toogle on match, Pulse=19

The frequency of TIM3 is 40MHZ.

As I understand it, when I configure the output compare function of TIM3 according to the above parameters, the output pin of channel 1 should generate a square wave with a period of 1us. Because Pulse/40MHZ = 20/40MHZ = 0.5us.

But actually, I get a square wave with a period of 2us. After my troubleshooting, I realized that this value is calculated based on the value of ARR. Because ARR/40MHZ = (39+1)/40MHZ = 1us.

In fact, I found that the parameter Pulse didn't do anything at all, and even if I set the Pulse parameter to 0, the Channel1 pins would still generate a square wave based on the value of the ARR, as well as generating an interrupt.

Can anyone explain this?  Why doesn't the parameter Pulse play any role, instead ARR determines the period of the output square wave? As I understand it, it should be that ARR plays no role and Pulse determines the period of the output square wave.

My clock configuration, and part of the code is shown below:

TIM3.pngTIM3_0.pngTIM3_1.pngTIM3_2.pngTIM3_3.pngTIM3_4.pngTIM3_5.pngTIM3_6.png

Following the configuration above, the waveform I got from the oscilloscope is shown below. Where the yellow waveform is representing Cnannel1 of TIM3 and the blue waveform is representing GPIO PB5 which is flipped in the ISR of TIM3.

TEK00000.JPG

    This topic has been closed for replies.

    7 replies

    Super User
    December 15, 2023

    You have it set to toggle on match, so it toggles once per period, when the count matches.

    TDK_0-1702654794522.png

    Instead, set it to PWM mode 1.

    TDK_1-1702654888331.png

     

    MasterLuAuthor
    Explorer
    December 16, 2023

    Thanks for the reply, but the pwm model doesn't work for my needs. I need timer to output a level signal on the GPIO while generating an interrupt. It seems that only output compare mode can satisfy my needs.

    Super User
    December 16, 2023

    You want a PWM with variable duty cycle. How does PWM not satisfy that? You can still enable interrupts.

    ST Employee
    December 15, 2023

    Hello @MasterLu, welcome to ST Community, 

    Just to make things more clear for you regarding the ARR and pulse values...

    This is how the output compare function works; ARR sets the top value of the timer (When the timer's counter reaches this value, it resets to 0), whereas, the pulse value you set for the OC channel is compared with the counter value (when the counter reaches the pulse value, the output pin is toggled) 

    For example, in toggle on match, if counter=pulse, the output is toggled but the counter doesn't reset until it reaches the ARR value.

     

    MasterLuAuthor
    Explorer
    December 16, 2023

    Yeah, you're right. However, the problem I'm currently having is that it doesn't work no matter what my pulse parameter is set to. And the ARR parameter seems to do what the pulse parameter is supposed to do.

    As an example, whether my pulse parameter is set to 20 or 0, the period of the square wave I observe on the oscilloscope doesn't change in any way. When I set the ARR parameter to 39 or 65535, the period of the square wave I observe on the oscilloscope changes.

    As I understand it, the period of the square wave I observe on the oscilloscope should change when I set different values of pulse.

    Super User
    December 16, 2023

    > output compare mode instead of pwm mode

    There is no "output compare mode" which would be in opposite of "pwm mode".

    Each timer channel can be used either as Input Capture (i.e. its respective pin is input to that channel), or as Output Compare (i.e. it outputs something to its respective pin - or not, if you don't enable the output itself).

    When used as Output Compare, you can - by setting TIMx_CCMRx.OCxM field - select, how should the output circuitry behave upon Compare Event, i.e. when TIMx_CNT matches TIMx_CCRx.

    One of those options is Toggle, when the output changes its polarity. If you select that, it will change polarity once per timer period.

    Other of those options is one of two PWM variants, when the output changes its polarity twice per period: once upon Update i.e. when TIMx_CNT reaches TIMx_ARR; and second time when the Capture event happens.

    Read the TIM chapter in Reference Manual.

    JW

    Visitor II
    March 26, 2024

    Hello, this is a late reply but for anyone looking for an answer about this matter:

    What you are doing here is setting the ARR to a certain value, the counter will start incrementing for each clock cycle until it reaches the ARR value, resets to zero and generate an overflow. Along the way, when the counter value matches the "Pulse" value, the output pin will toggle. The counter will continue counting up until it reaches ARR and then resets to zero, starting to count up gain. If you think about it, the Pulse value will match with the counter value once each ARR clock cycles. To overcome that, you need to add to update the "Pulse" value after each match.

    Visitor II
    October 3, 2024

    Hello, I'm a beginner in this but I've been studying it these days and I think I found the answer, which is simpler than everything said. I'll try to explain my opinion in this matter, as simple as I can:

    Problem: The wave generated has a period of 2us, instead of the one you want (1us).
    Data: 


    PSC=0,

    ARR=39,

    The frequency of TIM3 is 40MHZ.

    Output Compare Channel1: Mode = Toogle on match, Pulse=19   

    The formula is:

    Tout = (ARR+1)(PSC+1)/Fclk
    Tout (s): Period of the interruption given by the timer
    Fclk: Frequency of the timer

    ARRAuto-Reload Register. When the counter reaches this value, it triggers the interruption. It must not be greater than the bit register size (16 bit usually, so 2^16 = 65536).

    PSC: Prescaler. Used for dividing the timer clock. Normally for better resolution, it is preferred to have the pair of the lowest PSC and the highest ARR for the desired Tout. And vice versa for better power consumption.


    Using this formula in your case, Tout is 1us. This means the interruptions will happen every 1us.

    So, why do you have a square wave of 2us?
    Because you have the Output Compare in Toggle Mode, so when the counter reaches ARR, which is in every interruption (1us), it toggles the output pin. One toggle every 1us means a square wave of 2us of period. If you want a wave with a period of 1us, you need the interruptions to happen every 0.5us, so use the formula with Tout=0.5*10^6 to obtain the pair of PSC and ARR (e.g. asume ARR=39 --> PSC= -0.5 Invalid!;  asume PSC=0 --> ARR=19 Ok!)

    Why does the Pulse setting does nothing?
    As user FadiEid said, "when the counter value matches the 'Pulse' value, the output pin will toggle. The counter will continue counting up until it reaches ARR".
    So Pulse, in this particular case of toggling, just shifts the wave. The interruption period will still be 1us and the wave period 2us. I uploaded a diagram in order to explain myself better.
     
    I hope I am correct in my assumptions. If not, I'll be very pleased to be corrected.

    Super User
    October 4, 2024

    > I hope I am correct in my assumptions,

    Yes, you are.

    JW

    Visitor II
    February 13, 2025

    Thank you for this question. I have the same problem. As far as I understand the OC creates channel signals with different phases (with pulse value), not different frequencies. To create different signals with different frequencies it must be used timer interruption and different variables