cancel
Showing results for 
Search instead for 
Did you mean: 

Setting up Quadrature Encoder Mode in STM32Cube

Neo M
Associate II
Posted on September 23, 2016 at 19:46

Hardware: STM32F767ZIT6 Nucleo 144 Board

IDE: Atollic TrueSTUDIO with STM32CubeMX

I am trying to interface a quadrature encoder to TIM4 in the Encoder Mode using STM Cube. I set up the timer and configure the counter to counter till 5000. The function HAL_TIM_IC_CaptureCallback() is called on every interrupt by the encoder and not after 5000 counts. 

I am most probably using the wrong callback function. Can someone please point me towards documentation for using the

Encoder Mode

 of timers in STM32F7 ? The complete F7 manual hasn't been that helpful.

#timer #encoder #stm32-cube
17 REPLIES 17
slimen
Senior
Posted on September 23, 2016 at 23:25

Hi,

Have a look to this

http://www.st.com/content/ccc/resource/technical/document/application_note/54/0f/67/eb/47/34/45/40/DM00042534.pdf/files/DM00042534.pdf/jcr:content/translations/en.DM00042534.pdf

, it may help you on TIM configuration in encoder mode.

You can find several working encode mode examples on the forum.

Regards

Neo M
Associate II
Posted on September 27, 2016 at 16:36

Thanks for the link to the Application Note. I am using ST Cube application to configure my timers. I am using TIM4 in Encoder mode. I set the counter period to 1000. TIM4->CNT does give the current value of the counter. Which is the correct callback function that is called once the counter counts up to 1000?

Posted on March 08, 2017 at 16:45

How did you get on with this?

I note from the reference manual that to use TIM2,3,4,5 in 'Encoder mode 3'  - one sets the SMS (Slave mode selection)  bits to 0011. However no such option exits in the drop downs within Cube MX GUI, 

There is only a subset of whats in the reference manual which are :

Disable

External clock mode 1

Reset mode

Gated mode

Trigger mode

Combined Reset Trigger Mode

The full list from the manual is

0000: Slave mode disabled - if CEN = ‘1 then the prescaler is clocked directly by the internal clock.

0001: Encoder mode 1 - Counter counts up/down on TI1FP1 edge depending on TI2FP2 level.

0010: Encoder mode 2 - Counter counts up/down on TI2FP2 edge depending on TI1FP1 level.

0011: Encoder mode 3 - Counter counts up/down on both TI1FP1 and TI2FP2 edges depending on the level of the other input.

0100: Reset Mode - Rising edge of the selected trigger input (TRGI) reinitializes the counter and generates an update of the registers.

0101: Gated Mode - The counter clock is enabled when the trigger input (TRGI) is high. The counter stops (but is not reset) as soon as the trigger becomes low. Both start and stop of the counter are controlled.

0110: Trigger Mode - The counter starts at a rising edge of the trigger TRGI (but it is not reset). Only the start of the counter is controlled.

0111: External Clock Mode 1 - Rising edges of the selected trigger (TRGI) clock the counter.

1000: Combined reset + trigger mode - Rising edge of the selected trigger input (TRGI)

reinitializes the counter, generates an update of the registers and starts the counter.

I thought that maybe the option I need was disabled due to some other dependencies however I have not found what these are.

It seems that the CubeMX GUI is incomplete in this regard?

Posted on March 08, 2017 at 23:48

I don't CubeMX but

http://elastic-notes.blogspot.sk/p/cubemx-stm32-encoder-interface.html

came up with a quick search.

JW

Posted on March 09, 2017 at 10:29

As I've said I don't CubeMX and use an old-fashioned pencil-and-printed-pin-table-from-DS method. For me this avoids the 'non-intuitive' part, but apparently this does not work for everybody either. Actually, my colleague who does the HW, uses CubeMX as a successor of the former pin-selector javoid (I forgot the name, it animated ellipses instead of cube-wireframes in About - I still see this as the most remarkable feature of these softwares ;) ). For new designs we assign pins and features in parallel, he clicking me playing the pen-and-highlighter game, these processes appear to complement themselves. He spends quite a lot of time clicking back and forth each time he encounters a grey; he acquired quite a knack for this during the years; but it must be said that he also does a lot (and by 'a lot' a mean 'a huge lot') of DS reading in the preliminary phases of design, so that's not just some blind clicking.

2 eurocents

JW

Posted on March 09, 2017 at 10:09

Aha nice one thanks

That option had been greyed out for me and I wasn't sure what it was for but having seen that link I set everything else back to disabled and suddenly it is available and then all the other related configuration is buried under a completely different part of the GUI.

Not very intuitive but easy enough when you know how.

Posted on April 15, 2017 at 10:17

I'm using the same setup for the TIM encoder and I added HAL_TIM_PeriodElapsedCallback to catch timer update events (TIM period reached). The timer value is updating as expected and roll back every period but I don't get any interrupt callback.

Any idea why? How did you solve this issue?

Thanks

Posted on April 17, 2017 at 14:37

Did you start the TIM in Interrupt mode?

Like HAL_TIM_Base_Start_IT instead of just HAL_TIM_Base_Start?

Posted on April 17, 2017 at 15:43

Yes, I'm using HAL_TIM_Base_Start_IT but the interrupt does not occurred.

Thanks

On Mon, Apr 17, 2017 at 3:38 PM, Neo M