Skip to main content
Neo M
Associate III
September 23, 2016
Question

Setting up Quadrature Encoder Mode in STM32Cube

  • September 23, 2016
  • 8 replies
  • 19225 views
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
This topic has been closed for replies.

8 replies

slimen
Visitor II
September 23, 2016
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
Neo MAuthor
Associate III
September 27, 2016
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?

Daniel Squires
Associate II
March 8, 2017
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?

waclawek.jan
Super User
March 8, 2017
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

waclawek.jan
Super User
March 9, 2017
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

Vineeth CN
Associate
June 20, 2017
Posted on June 20, 2017 at 06:29

Hi All ,

   I am interfacing a quadrature encoder to TIM4 in the Encoder Mode using STM Cube with STM32F103. I set up the timer and configure the counter to counter till 1000. The function HAL_TIM_IC_CaptureCallback() is called on every interrupt. I am excepting a call back on when the counter overflow happen ie, when the count reaches to 1000 pulses in this case.

I my application i know for 1mm travel i will be getting suppose 1000 pulses, so i will set 1000 as the pre load count value and if the encoder count reaches to 1000 count i should get an interrupt.

Please help me with a solution.

Thanks and Regards,

Vineeth

Brian D
Associate III
August 31, 2017

Posted on September 01, 2017 at 00:25

First, I made a mistake of posting my question as comment, which will not be seen as a new reply with question. So here it goes.

Hi JW,

Board: NucleoF722 with STM32F722

I'm using TIM5 for quadrature encoder counter. Following you suggestion to configure the timer, I have question with reference to capture below. Since the encoder channel A and B are mapped to TI1 and TI2 and the encoder Mode is TI12, and the counter is counting on both rising and falling edges of TI1 and TI2 inputs.

Question: Why selecting Rising Edge instead of Both Edge in the polarity selection? (In fact the chip User Manual says 'This configuration (Both edge) must not be used for encoder.' and I don't understand why not?) 

Anyone know why the polarity must be set to Rising Edge (instead of Both edge)?

Best,

Brian

0690X000006CKA2QAO.png

waclawek.jan
Super User
September 4, 2017
Posted on September 04, 2017 at 10:41

I don't Cube so I don't know what exactly is meant by 'Both edge', but if it's setting of  TIMx_CCER.CC1P (which you've read) then it's because that is a setting for the 'edge detector circuit', in the channel input, which in encoder mode (and a few other modes named therein) does not work as an edge detector but as a selectable invertor, thus has only two settings (and the 'both edges' is the third). The encoder decoding circuit in the slave module does take levels from the input channels (TI1FP1 and TI2FP2 signals, see TIM block schematics) rather than edges - it then detects the edges in itself.

JW

Brian D
Associate III
September 6, 2017
Posted on September 06, 2017 at 03:51

In other words the CubeMX is wrong under its Parameter Setting\Encoder\Polarity to have the selection of Rising Edge, Falling Edge, Both Edges. Because in encoder mode, regardless of TI1 Mode, TI2 Mode, or both TI1 and TI2 Mode, the counter should be triggered on both edges of each channel; it is not a choice. Therefore, the wording should be Inverted or non-inverted under Polarity selection.

Brian

waclawek.jan
Super User
September 6, 2017
Posted on September 06, 2017 at 10:59

Therefore, the wording should be Inverted or non-inverted under Polarity selection.

Yes, but that's only in encoder mode (and a few other modes), so those settings interact, that makes programming CubeMX far less trivial than only directly mapping  few register bits to some single-liners.

[rant mode on]This is why I am not interested in cuboids and kin. At the end of the day, if you really want to use the chip and not only a subset of its features you need to go down and understand the working of it, so cuboids are only an unnecessary intermediary. Sparkling and fuzzing and technicolor, yes, I understand the selling points. I'm just sad ST spends money/time/people on this fluff rather than the right stuff. [rant mode off]

JW

Brian D
Associate III
September 6, 2017
Posted on September 06, 2017 at 19:58

JW,

'

[rant mode on]This is why I am not interested in cuboids and kin. At the end of the day, if you really want to use the chip and not only a subset of its features you need to go down and understand the working of it, so cuboids are only an unnecessary intermediary. Sparkling and fuzzing and technicolor, yes, I understand the selling points. I'm just sad ST spends money/time/people on this fluff rather than the right stuff. [rant mode off]'

I'm a newbie to STM32 tool sets and so I found Cube is helpful in understanding how the registers are acessed, and a quick way to see generated codes that initializes the registers -- in a strict sequential order sometime -- for the peripherals to work properly. I total agree about reading the registers and their bits meaning. In fact I write my own code since the Cube generated code is too long; i.e. Cube uses over 50 lines of C code to send SPI data, while I need only two lines of code:

while((SPI2->SR)& 0x0080); // if busy

SPI2->DR=0x0A; // TX data 0x0A to Canon EF lens.

As about the Cube wording, there is nothing wrong about using a proper wording when the timer is in Encoder  Mode. The GUI screen should be adapted to each mode of the timer usage. Why the encoder mode has to share the same description with other modes?

Best,

Brian