2022-11-03 07:41 AM
Hello!
I use STM32F103 MCU Timer 2 in ecnoder mode.
Ticks from an encoder go at approximately 1 MHz, so I use prescaler value 25 to decrease the frequency.
Sometimes it works fine, but sometimes timer begins to count encoder ticks at double speed - it looks like they arrive 2x faster than usual, but they actually aren't.
I suppose that is a Timer setup problem.
Please help to find a reason.
Many thanks for help.
With regards,
Maksim
2022-11-03 08:58 AM
I am having a hard time trying to understand your issue.
>Ticks from an encoder go at approximately 1 MHz, so I use prescaler value 25 to decease the frequency.
You use a preeescaler where? if youre using the TIM1 clock preescaler i would use no preeescaler at all at 1Mhz so you get most precission out of your TIM.
>Sometimes it works fine, but sometimes timer begins to count encoder ticks at double speed - it looks like they arrive 2x faster than usual, but they actually aren't.
Show us some code and a picture of your setup (1Mhz is high frecuency , did you look if there is any noise?)
>I suppose that is a Timer setup problem.
Maybe, show us your code
2022-11-03 09:56 AM
Single pin, or Dual/Quadrature ? For single, perhaps External Clock mode would be better.
Sure the signal isn't bouncing? Any scope traces of the signal(s) when you see this?
The Input Prescaler (1, 2, 4, 8) is different to the Counter's Prescaler
The ClockDiv setting relates to how the signals are filtered/resynchronized.
2022-11-03 01:35 PM
Hi, @Javier Muñoz !
First of all, many thanks for answer and sorry for English (and typos).
> I use prescaler value 25 to decease the
I mean decrease, of course.
> You use a preeescaler where?
> if youre using the TIM1 clock preescaler
I use TIM2, but I suppose there is no difference between TIM1 and TIM2 in my case.
> you get most precission out of your TIM
I really don't need such precision, but the encoder is a part of external equipment, which I'm not able to change.
> i would use no preeescaler at all at 1Mhz
Are you sure the MCU is fast enough to process encoder ticks (I use interrupt on overflow to count 32 bit value up/down), while I also use UART and CAN? I'm not sure about it, I need to test.
> Show us some code and a picture of your setup
I'll do it later from office.
The MCU should perform some action on every second encoder count, for example. Encoder ticks goes at 1 Mhz, so we have 40 kHz after 25 prescaler.
So, I see the action with oscilloscope, it's OK (action appears every 50 us). But occassionally
action becomes to appear every 25 us (approx)! And after random time period returns to normal 50 us.
I check encoder inputs with scope, both signals are OK all the time.
My suggestions now are:
Many thanks for help.
With regards,
Maksim
2022-11-03 01:42 PM
Hi @Community member !
> Single pin, or Dual/Quadrature ?
Quadrature
> Sure the signal isn't bouncing? Any scope traces of the signal(s) when you see this?
Yes, I check it with scope. It looks good, but I'm not sure it's always good, of course.
> The Input Prescaler (1, 2, 4, 8) is different to the Counter's Prescaler
The prescaler used is shown in my previous answer. I'm not sure if it is a good solution or not.
Many thanks for help.
With regards,
Maksim
2022-11-03 02:27 PM
2022-11-04 01:25 PM
Today I removed prescaler and tried to use native quadrature encoder pulses at approximately 1 Mhz.
So, MCU counts them nicely with both directions, but that did not solve my issue.
As I see now, timer itself may be not a reason at all. I use separate thread for CAN bus interaction, so it could produce unexpected delays. Motor (and encoder) can make many rotation during those delays, so my counter-depending routine gets counter value too late.
I'll continue to debug it and will share any usefull info.
With regards,
Maksim