cancel
Showing results for 
Search instead for 
Did you mean: 

Getting an interrupt at specific encoder value

Krix N
Associate II
Posted on July 31, 2017 at 14:41

Beginner working with STM32L452:

I have TIM2 set up as encoder-based timer. Works perfect.

Now I want an interrupt whenever the TIM2 reaches a certain value (position).

I tried to enable timer interrupt through HAL_TIM_Encoder_Start_IT, and hoped to be able to get an interrupt when the encoder passed value 0. But I only managed to get an interrupt on every single encoder tick.

Reading a bit more on timers, i wonder if setting up another timer as slave to TIM2 would be the way forward ?

If yes, then how should the master/slave output/input be configured to have the slave follow TIM2 ? I have read the timer overview in 'en.DM00042534.pdf' but I don't understand all of it.

if no, what are my options then?

If a solution can be achieved through CubeMX configurator, i I would very much appreciate that, but it is not a must.

Thanks.

#interrupt #timer #hal_tim_encoder_start_it #interrupts #encoder
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on July 31, 2017 at 15:15

I want an interrupt whenever the TIM2 reaches a certain value (position).

That is, you want to use one of the capture-compare units in *compare*  configuration, i.e. you want to throw an interrupt when timer's counter CNT matches the value set in CCRn.

The encoder mode uses CH1 and CH2 as input. While it won't necessarily propagate beyond the input filters, it precludes using those channels as *compare* which is an output rather than input feature.

Thus, set up CH3 or CH4 for the output compare (you don't need to actually enable it as output, nor set up a GPIO pin for it) and bind the interrupt to the chosen channel in DIER.

JW

View solution in original post

3 REPLIES 3
Posted on July 31, 2017 at 15:15

I want an interrupt whenever the TIM2 reaches a certain value (position).

That is, you want to use one of the capture-compare units in *compare*  configuration, i.e. you want to throw an interrupt when timer's counter CNT matches the value set in CCRn.

The encoder mode uses CH1 and CH2 as input. While it won't necessarily propagate beyond the input filters, it precludes using those channels as *compare* which is an output rather than input feature.

Thus, set up CH3 or CH4 for the output compare (you don't need to actually enable it as output, nor set up a GPIO pin for it) and bind the interrupt to the chosen channel in DIER.

JW

Krix N
Associate II
Posted on August 03, 2017 at 09:49

@waclawek_jan :

Thanks for pointing me the direction of output compare.

I will have to see next week if I can make this work, at least at can see that googling for 'stm32 timer

output compare

' gives some new interesting leads.

Posted on August 03, 2017 at 10:18

Read the Timer chapter in RM first.

0690X00000607qmQAA.png

JW