cancel
Showing results for 
Search instead for 
Did you mean: 

How to get interrupt on counter overflow with Quadrature Encoder with Cube and HAL?

Stefan Wozniak
Associate III
Posted on March 26, 2017 at 14:09

Hi,

I want to use Timer with Quadrature Encoder. I'm new on STM32 so I'm trying to start with Cube and HAL.

I'm able to setup STM32F411 to count pulses but there is an only 16bit counter. I need to measure pulses up to 400k.

Can I rise interrupt on counter overflow to store revolutions? I want to do this using Cube and HAL. I was trying a lot of settings and interrupts but I'm unable to get interrupt from an encoder.

Can you help me?

Thanks in advance.

Steve

#cube #cube-mx #quadrature-encoder #stm32-cube #stm32f411 #quadrature
1 ACCEPTED SOLUTION

Accepted Solutions
Uwe Bonnes
Principal II
Posted on March 27, 2017 at 13:24

There is no such interrupt. However there also is no need for that interrupt and a Jitter at the position of that interrupt would be harmful. Depending on the maximum count rate, determine the time needed to count 2^16 pulses. Mostly this will be several ten or hundreds of Milliseconds. Be sure to call a  function every less than half of that time that calculates the difference between that last and the presen counter value and add these differences up to a 32 bit value. Done!

View solution in original post

21 REPLIES 21
Uwe Bonnes
Principal II
Posted on March 27, 2017 at 13:24

There is no such interrupt. However there also is no need for that interrupt and a Jitter at the position of that interrupt would be harmful. Depending on the maximum count rate, determine the time needed to count 2^16 pulses. Mostly this will be several ten or hundreds of Milliseconds. Be sure to call a  function every less than half of that time that calculates the difference between that last and the presen counter value and add these differences up to a 32 bit value. Done!

Posted on March 27, 2017 at 13:34

Or,  use TIM2 or TIM5, they are 32-bit counters.

Done, easier and faster! 😉

JW

Posted on March 27, 2017 at 13:43

But that needs thinking before tjhe layout 🙂

Posted on March 27, 2017 at 13:50

This could be tricky because encoder speed is changing.

But this could work.

Thanks

Posted on March 27, 2017 at 13:51

I need to check it. I was thinking that only one 32bit counter supports encoder on F411.

Posted on March 27, 2017 at 14:04

But that needs thinking before the layout 🙂

How true. (No smiley. Or maybe a sad one?)

This could be tricky because encoder speed is changing.

You use the minimum time of overflow, of course. Also, if instantaneous readouts are required, this might get a bit tricky.

I was thinking that only one 32bit counter supports encoder on F411.

Maybe. You didn't ask for more and I didn't check.

JW

Posted on March 27, 2017 at 14:47

Yes, you are right with 32bit counters. Because of that, I'm thinking about moving to F7 with two 32bit counters handling Encoder.

But my intention was to find a way to easily count pulses above counter capability. I was thinking that interruption approach would be the simplest way and efficient one. There are only a few 32bit counters on a microprocessor. So this could be limiting.

 SW

Posted on March 27, 2017 at 15:49

 I was thinking that interruption approach would be the simplest way and efficient one.

No it is not, Uwe told you above: when the input jitters around the over/underflow, and your ISR is not fast enough to capture all of them, you are in trouble.  Listen to what he recommends if you desperately want to extend the 16-bit counters.

There are only a few 32bit counters on a microprocessor. So this could be limiting.

First you asked for any, then you asked for two. Here you are:

0690X00000606drQAA.png

JW

Posted on March 28, 2017 at 07:27

Thanks a lot for your help.