cancel
Showing results for 
Search instead for 
Did you mean: 

Can I make TIM1 or TIM8 generate 4 timer interrupts?

arnold_w
Senior
Posted on October 06, 2016 at 16:00

I am working with the Discovery Development Board (STM32F407) and I would like to write a function that uses one physical timer and allows me to generate arbitrary interrupts at different instances, for example

addTimerInt (100, myCallback1); // Call callback1 100 ms from now

delayMs(5);                                  // Dummy delay to explain what my function should be capable of

add

Timer

Int (50, myCallback2);   // Call callback2 50 ms from now

delayMs(10);                                // Dummy delay to explain what my function should be capable of

add

Timer

Int (100, myCallback3); // Call callback3 100 ms from now

delayMs(100);                              // Dummy delay to explain what my function should be capable of

add

Timer

Int (1, myCallback4);     // Call callback4 1 ms from now

The advanced (TIM1 & TIM8) timers have 4 channels, can these be used in this case somehow? The function will have nothing to do with actual pins on the microcontroller and we don't have spare pins to hook up for looping back clocks to particular pins.

Or is it possible to have a timer run from 0 to 0xFFFF (and then wrap around to 0) and have the timer generate an interrupt at a specific CNT-value without the CNT-value being reset back to 0? Basically, if I schedule an interrupt at CNT = 0x100 then the next CNT-value after the interrupt should be 0x101, not 0. Can I do something clever by setting APRE = 1?
2 REPLIES 2
mark239955_stm1
Associate II
Posted on October 07, 2016 at 03:00

You can set up the timer channels in Output Compare mode, so that they generate TIMx_CHy_TRGO signals that are entirely internal to the MCU (actually, they are in this mode by default iirc - it's OC mode 0 in the TIMx->CCMRy registers).  If you enable the interrupts for each channel in the TIMx->DIER register, then they will fire as the TIMx->CNT value passes the value in the corresponding TIMx->CCRy register.

So for example, if ARR is set to 1000 and you set CCR1,2,3,4 to 100,200,300,400 then:

At t = 0,  the timer update event fires (and triggers an interrupt, if enabled)

At t = 100, the timer CC1 event fires (and interrupts)

At t = 200, the timer CC2 event fires

At t = 300, the timer CC3 event fires

At t = 400, the timer CC4 event fires

At t = 1000, the timer updates and wraps to t = 0

Note that you can't run the CCx events at a different frequency from one another, but you can shift their phases with respect to when the timer wraps.

Walid FTITI_O
Senior II
Posted on October 07, 2016 at 16:23

Hi arnold_w, 

Yes, you can use the Timer output compare in active /inactive or both active inactive sensitive level. When there is a match with the programmed CCRx value , an interrupt is generated ans an active/ incative edge is outputted on output pin.

For that , refer to the one of the example  in

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef4.html

''TIM_OCActive'' or ''TIM_OCIActive'' at this path:  STM32Cube_FW_F4_V1.13.0\Projects\STM324xG_EVAL\Examples\TIM\TIM_OCActive

-Hannibal-