cancel
Showing results for 
Search instead for 
Did you mean: 

Timer OC Mode

Vch.1
Associate II

Hi.

I need help understanding how the timer works, because I'm faced with a situation that I can't understand.

I use F091RC (board: nucleo)
I created a new project in the CubeMX, timer 2 (CH1) configured in OC mode.
Prescaler = 0
ARR = 5000-1
CCR1 = 2500-1
Timer clocked from 48 MHz

In callback (HAL_TIM_OC_DelayElapsedCallback) i create variable "tim_count++" for count.

In the main while I check if the variable has reached 10, I stop the timer, wait 300µs and start the timer again repeating the procedure.

I am attaching a screenshot of the analyzer, it shows that everything works well

f0_tim2_timnig_even.png

the first time interval is 54 µs, then several 104µs, and then a pause of 300µs, then the procedure is repeated, 54, 104, 300.

 

But I came across a small nuance that was strange to me: if you stop the timer when the value of the variable count is "ODD", (for example: 9) then the time intervals get confused and work in a strange way, this can be seen in the screenshot

f0_tim2_timnig_odd.png

I would be glad if someone explains why this is and how to fix it?

To start and stop the timer, I use HAL functions (but I also tried it on registers)

12 REPLIES 12
GwenoleB
ST Employee

Hello @Vch.1,

First of all, please make sure you doesn't have another interrupt that can disturb the execution of Timer callback.

In addition, it seems that you repeat a specific sequence 54, 104, 300µs. Probably, updating the CCR/ARR on-the-fly using DMA should fix your issue. 
Please refer to AN4776, section 5.3 Application example: arbitrary waveform generation using
timer DMA-burst feature

Kind Regards,

Gwénolé

 

Thanks for the answer
Let me try to rephrase what I wrote in my first post.

300μs is just the time interval between (let's call them a frame) frames.

Now let's imagine, I want to create a timer (TIM2) that will work in Output Compare (CH1), ok?
The timer is clocked from 48 MHz, now I set the prescaler to 0, ARR is 5000-1, and CCR = 2500-1, and the mode: toggle on match.

With these settings, I get a time interval in the first interrupt of approximately 54 µs, and the next 104 µs (as in the first screen) Ok?

Now, if I want to stop the timer after a not certain amount of time and start the timer in the same mode also after a not certain amount of time

When next start the timer, I would like to get the same parameters, 54µs first interrupt, and then all the rest 104µs, ok ?)

It seems that everything is ok, why not, start the timer and everything will be fine, but there is a nuance here: if stop the timer with an odd number of interrupts, then the subsequent start occurs with an incorrect time interval, namely, there is no interval of 54µs (You can see this on the second screenshot.)

How can I stop the timer at "absolutely any moment" so that the subsequent start will be correct?

Vch.1
Associate II

I created a new project by CubeMX, no other interrupts.

GwenoleB
ST Employee

Dear @Vch.1,
I built your application on my side. However, I'm not able to reproduce your issue.
If you agree, feel free to share with me your project. I can debug it on my side.

Kind Regards,

Gwénolé

How do you stop (pause) the timer?

Yes, of course, thank you.

 

HAL_TIM_OC_Stop_IT(&htim2, TIM_CHANNEL_1);

This stops only the interrupt, but the timer keeps running, debug into HAL sources.

hth

KnarfB

You think so ?)
inside the function I see this:

/* Disable the Output compare channel */
TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);

/* Disable the Peripheral */
__HAL_TIM_DISABLE(htim);