2023-12-15 07:10 AM
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:
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.
2023-12-16 06:18 AM
I apologize for the confusion my question caused you. But my question has always been very clear and that is, why the parameter pulse is not working as expected in output compare mode. I told you that I was testing the interrupt latency to show you why I insisted on using output compare mode instead of pwm mode. Again, I want to apologize for the unpleasantness I caused you.
2023-12-16 08:09 AM
> 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
2024-03-25 11:39 PM
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.
2024-10-03 10:13 AM
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
ARR: Auto-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.
2024-10-03 10:34 PM
> I hope I am correct in my assumptions,
Yes, you are.
JW