cancel
Showing results for 
Search instead for 
Did you mean: 

Motor Encoder read - please help

ondrej
Associate II
Posted on April 12, 2013 at 20:45

Hello, I have my own developed board for motor driving. MCU is STM32F407VET6. The board has inputs from 4 motor encoders. I need to know an actual speed of each motor. ENC1_A, ENC2_A, ENC3_A, ENC4_A pins are connected to TIM8(CH1 - 4). Every 100ms i want to check motors speed. So i have TIM7 initialized for timing but i dont know how to read TIM8 CH1-CH4 Counters. 

In TIM8 init:

        TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;

TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

TIM_ICInitStructure.TIM_ICFilter = 0xF;

TIM_ICInit(TIM8, &TIM_ICInitStructure);

TIM_Cmd(TIM8, ENABLE);

In TIM7 interrupt routine:

                if (TIM_GetITStatus(TIM7, TIM_IT_Update) != RESET){

    TIM_ClearITPendingBit(TIM7, TIM_IT_Update);

    M1_CurrentSpeed = TIM_GetCapture4(TIM8);

    TIM8->CCR4 = 0;

    TIM8->CNT = 0;

    LED_Toggle(LED_COMM);

}

But if i turn manually with motor shaft and encoder will give ONE pulse, TIM_GetCapture4(TIM8) gives values like 500, 12221, 5300, 42000 etc.

It seems that TIM dont react on Rising edge on input ?

Please help.

#motor-encoder #stm32 #timer
3 REPLIES 3
Posted on April 12, 2013 at 20:55

What's the time base of the counter?

The CCRx register just latch the CNT value at the trigger point, they do not count.

The timebase would need to be clocked from an external source for CNT to count external pulses, either as an external clock, or encoder mode.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ondrej
Associate II
Posted on April 12, 2013 at 21:26

Hello clive1, thanks for your answer and time.

I didnt set timebase because i thought that i dont need that. In example from ST is not initialized timebase for pulse counting:

STM32F4xx_DSP_StdPeriph_Lib_V1.1.0/Project/STM32F4xx_StdPeriph_Examples/TIM/TIM_InputCapture/main.c .

I thought in Input capture mode is counting based on Rising/falling edge.

Posted on April 12, 2013 at 22:01

Input capture allows you to time stamp input signals with respect to each other (multi channel, phase relationship), or to itself (period, delta measurement). CNT -> CCRx for capture event on Channel x

With a time base of 1MHz, the first rising edge measured on CCR2 = 1000, the second CCR2 = 2000. The delta is 1000 x 1us, or 1ms, thus a frequency of 1 KHz

You'd pick a time base to allow for a 16-bit count (65536) to capture both events. ie a period of less than 65.536 ms (faster than 15.26 Hz)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..