Skip to main content
Stefan Wozniak
Associate III
April 17, 2019
Solved

Act on encoder pulses count - interrupt on specific CNT

  • April 17, 2019
  • 4 replies
  • 1324 views

Hi,

I need to match motor speed with the speed of the measured device.

My idea is to toggle pin when encoder pulses count reaches some value.

I was unable to generate an interrupt on a specific count, so I decide to react on each pulse and check count.

I use interrupt: HAL_TIM_IC_CaptureCallback

Interrupt body is quite simple:

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
 count1= TIM1 -> CNT;
 
 if ((TIM1 -> CNT) >=1000) {
 TIM1 -> CNT=0;
 }
}

But when I set a breakpoint on TIM1 -> CNT=0;

I read from registry CNT and it's always more than 1000. I was expecting to be equal 1000 on interrupt.

Perhaps I chose the wrong interrupt but it was the only one that I was able to fire on encoder work.

Also when I'm checking CNT in main program loop I hit breakpoint on CNT larger than 2000. 

while(1)
{
 counter = TIM1 ->CNT;
 
 if (counter >= 2000) {
 counter =0;
 TIM1 -> CNT =0;
 }
}

I'm working now on True studio and I have a feeling that on AC6 it was working better.

Encoder has 2000 point on revolution so counter should get 8000 on full revolution. It looks like it works OK. Speed is slow because at this moment I'm rotating it slowly with fingers.

Thanks for any help.

Best Regards,

Steve

This topic has been closed for replies.
Best answer by waclawek.jan

> Encoder has 2000 point on revolution so counter should get 8000 on full revolution.

> I read from registry CNT and it's always more than 1000.

Even if you turn such encoder with fingers, there's no way you stop it exactly at 1000. The timer continues to work while in breakpoint., Turn the encoder and refresh the registers view to see how it works.

> I was unable to generate an interrupt on a specific count

Use one of the remaining channels as compare. Read Output compare mode subchapter of the timer chapter in RM.

Btw. it's a bad idea to clear or in any other way manipulate TIMx_CNT.

JW

4 replies

Stefan Wozniak
Associate III
April 17, 2019

It looks like counter works in right way, becasue I was able to match rotations. It looks like registry prewiev on true studio was not from breakepoint hit time.

But I can be wrong.

So does anyone know how to trigger interrupt on a specific count?

waclawek.jan
waclawek.janBest answer
Super User
April 17, 2019

> Encoder has 2000 point on revolution so counter should get 8000 on full revolution.

> I read from registry CNT and it's always more than 1000.

Even if you turn such encoder with fingers, there's no way you stop it exactly at 1000. The timer continues to work while in breakpoint., Turn the encoder and refresh the registers view to see how it works.

> I was unable to generate an interrupt on a specific count

Use one of the remaining channels as compare. Read Output compare mode subchapter of the timer chapter in RM.

Btw. it's a bad idea to clear or in any other way manipulate TIMx_CNT.

JW

Stefan Wozniak
Associate III
April 17, 2019

Thanks.

It's a good idea. 

I was trying to get interrupt from encoder combined channels and completly forgot about other channels. I will try to setup interrupt on other OC channel.

And in such case, there will be no need to modify encoder CNT.

SW

Stefan Wozniak
Associate III
April 17, 2019

OK.

I forgot to start TIMer for OC IT.

Now it's working.

SW