cancel
Showing results for 
Search instead for 
Did you mean: 

Some confusion about the configuration of output compare

MasterLu
Associate II

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

12 REPLIES 12
TDK
Guru

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

 

If you feel a post has answered your question, please click "Accept as Solution".
Sarra.S
ST Employee

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.

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

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.

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.

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

If you feel a post has answered your question, please click "Accept as Solution".

Because PWM mode compares the values of pulse and timer count, the level of the channel output of pwm is related to the relative size of their values. When I use pwm mode, the timer interrupt can only be generated when the value of timer count is greater than the value of ARR. If I want the timer interrupt to be generated at the same time as the pwm output, then the pulse value should be the same as the ARR value. But if that happens, then the level of the pwm output will no longer change, and the output level of the pwm I see on the oscilloscope will be a straight line. I need the timer to generate an interrupt request and output a flipped level at the same moment (the flipped level makes it possible for me to observe on the oscilloscope at what time the mcu enters the ISR)

Output PWM on one channel and generate interrupts on another channel if you want the interrupts to be offset from the PWM pulses.

Note that generating interrupts at 1 MHz isn't ideal, computationally, and there will be some lag between interrupt flag being set and the first instruction in the interrupt actually being executed.

If you feel a post has answered your question, please click "Accept as Solution".

Thanks for the reply, but it doesn't solve my problem. I'm actually trying to test the interrupt latency of the ARM Cortex M3 chip using the output compare function, and I referenced this Application Note from NXP: https://www.nxp.com/docs/en/application-note/AN12078. pdf?cid=nxpic

You're moving the goalpost here. In your original post, you are clearly not trying to test latency. Heck, even in your previous reply that's not a concern. If you stick to one problem, maybe I can help. If you change it every post, I'm not interested in trying.

If you feel a post has answered your question, please click "Accept as Solution".