cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 occasionally doubles counting speed in encoder mode

MPopo.1
Associate II

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

6 REPLIES 6
Javier1
Principal

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

Did you checked this out?

we dont need to firmware by ourselves, lets talk

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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?

0693W00000VOqucQAD.png>  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:

  • MCU is not fast enough to process 1 MHz ticks (not able even to correctly devide it to 40 MHz), so ticks become unstable and sometimes timer treat them at double frequency (80 kHz instead of 40), and my action from MCU appears with two times smaller period.
  • encoder signal's form is not good enough to be always "tracked" by MCU timer. I have FPGA, DDR3 and USB3 on the same board with the STM32 MCU, so the environment is potentially noisy.

Many thanks for help.

With regards,

Maksim

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

MPopo.1
Associate II

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