cancel
Showing results for 
Search instead for 
Did you mean: 

Quadrature encoder integration

FEke
Associate

Hi there

I​ am trying to integrate a quadrature encoder using STM32 cortex m mcus.

I could not get enough clearance reading the manual several times. I don't have an oscilloscope, which is why I need to rely on specifications rather than trial and error.

So far, i have been able to run the Tim/encoder example code to read the direction of turn (tim3 on stm32f401re).

What I want to do is to

1) read the relative position since beginning​ (tim3 or timx /= tim3?)

2) use a secondary timer to read frequency of the square wave to derive speed with hardware (tim4) counting. I have read the example on input capture for pwm input, but I only need the frequency and the master slave is done inside the same timer there. I want to make tim4 slave to tim3

OR

Should I simply attach the same encoder signal additionally to a Tim 4 pin directly​ and get rid of master slave conf?

3) similarly derive acceleration (tim4 or tim5)

4) do all that as precise as possible with the given hardware

Here are my questions..

1) Can the timer which uses the encoder mode to get direction (which disables use of slave/master conf for other uses) be also used to get the relative ​position?

OR

​do I need to use a separate second timer for position as well?

2) how can I setup a timer as a slave in depicted cases?

Any help is much appreciated!

Best regards

1 REPLY 1

1) > Can the timer which uses the encoder mode to get direction (which disables use of slave/master conf for other uses) be also used to get the relative ​position?

Yes. You should have the two signals connected to CH1 and CH2 of given timer, enabled them for input by setting both respective TIM3_CCMR1.CCxS to 0b01, setting the slave-mode controller to encoder i.e. TIM3_SMCR.SMS=0b001, or 0b010, or 0b011; and finally enabling the counter by TIM3_CR1.CEN=1.

2) > how can I setup a timer as a slave

You want to output the waveform coming into TIM3_CH1 as TRGO (trigger out) (CH2 or any other channel set as input can't be used as TRGO), i.e. set TIM3_CR2.MMS to 0b011. Then in chapter for TIM4, look at table at the end of description of TIMx_SMCR to find out which ITR input corresponds to TIM3->TIM4, for 'F407 it's ITR2 and I guess it's the same for 'F401; so set TIM4_SMCR.TS to 0b010. Then, by setting any TIM4 channel's TIM4_CCMRx.CCxS to 0b11 (and enabling it in TIM4_CCER.CCxE), that channel will capture at the pulses incoming through the TIM3->TIM4 link, i.e. at those coming to TIM3_CH1. Enable the counter (TIM4_CR1.CEN=1) with internal clock (ie. with TIM4_SMCR.SMS=0b000), and in the respective TIM4_CCRx you will find the timestamp for incoming pulses; you can set an interrupt for that channel and read it out there, or use DMA if you expect higher input frequencies.

JW