cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 HAL - Encoder Interrupt

Azadfalah
Associate

hi friends,

In STM32F412 microcontroller with HAL Library
I put timer 2 in encoder mode and everything works fine

I want, for example, if the counter reaches 1500, an interruption will occur, but I don't want the counter value to be reset after this interruption.
How can I do this?
I want the number 1500 to be recognized very quickly. When I check this value in the Super loop, every time the CPU may be involved in small delays (like LCD Update) and exceed the set value.


The things I did -> I thought it was possible to use timer 3 as a slave so that timer 3's clock is supplied from counter timer 2 But I was not successful



Thanks,

2 REPLIES 2
TDK
Guru

Interrupts don't reset the counter. Perhaps if it hits ARR, it will roll over. That is expected and avoidable by setting ARR higher than 1500.

Your handling of the encoder shouldn't rely on hitting any one particular value exactly.

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

> Your handling of the encoder shouldn't rely on hitting any one particular value exactly.

+1

However, if you set ARR higher than 1500 as you should, and want to have an interrupt when the counter is 1500, just set up one of the channels to Compare (any mode, may be PWM, you don't need to set up any pin in GPIO to be at this channel) and its CCRx to 1500, and enable its interrupt by setting corresponding TIMx_DIER.CCxIE, enable in NVIC and write a proper interrupt handler.

This probably can be clicked somehow in CubeMX, I don't use it.

JW