Skip to main content
Elliott Li
Associate
Jul 25, 2017 4:04 PM UTC
Solved

STM32F407VG discovery kit read multiple encoder

Posted on July 25, 2017 at 18:04

Hi all,

I am try to use

STM32F407VG discovery kit to read 5 encoder. I use TIM_HandleTypeDef and TIM_Encoder_InitTypeDef lib to read one encoder successfully. When I use the same code to read two encoders, I found that it doesn't work. Do anyone know that can I use this lib to read multiple encoder? Do anyone have any example code. 

Thank you.

    This topic has been closed for replies.
    Vangelis Fortounas
    Associate II
    Jul 26, 2017 9:24 AM UTC
    Posted on July 26, 2017 at 11:24

    Hello!

    The encoders use TIM1, TIM2, TIM3, TIM4, TIM5 timers to work.

    Every timer is separate inialised as encoder.

    So when you want to use each one encoder , must call the same function ofcourse  with the appropriate previously initialised TIM_HandleTypeDef as  parameter!

    TIM_HandleTypeDef htim1, htim2...;// at global scope

    //inside your funtion (main or other)

    TIM_Encoder_InitTypeDef

    enci;//

    //fill the structures wiith proper data

    htim1.Instance = TIM1;

    .....

    enci.EncoderMode = TIM_ENCODERMODE_TI1;

    .......

    HAL_TIM_Encoder_Init(&htim1,  &enci);//initialisation..

    htim2.Instance = TIM2;

    ...

    enci.EncoderMode = TIM_ENCODERMODE_TI1;

    ....

    HAL_TIM_Encoder_Init(&htim2,  &enci);//initialisation..

    //after initialisation , start all encoders

    HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_1) ;//start each encoder

    HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_1) ;

    ....

    3 replies

    waclawek.jan
    Super User
    Jul 26, 2017 9:09 AM UTC
    Posted on July 26, 2017 at 11:09

    When I use the same code to read two encoders, I found that it doesn't work.

    Post details.

    Note that not all timers support the encoder mode.

    JW

    Vangelis Fortounas
    Associate II
    Jul 26, 2017 9:24 AM UTC
    Posted on July 26, 2017 at 11:24

    Hello!

    The encoders use TIM1, TIM2, TIM3, TIM4, TIM5 timers to work.

    Every timer is separate inialised as encoder.

    So when you want to use each one encoder , must call the same function ofcourse  with the appropriate previously initialised TIM_HandleTypeDef as  parameter!

    TIM_HandleTypeDef htim1, htim2...;// at global scope

    //inside your funtion (main or other)

    TIM_Encoder_InitTypeDef

    enci;//

    //fill the structures wiith proper data

    htim1.Instance = TIM1;

    .....

    enci.EncoderMode = TIM_ENCODERMODE_TI1;

    .......

    HAL_TIM_Encoder_Init(&htim1,  &enci);//initialisation..

    htim2.Instance = TIM2;

    ...

    enci.EncoderMode = TIM_ENCODERMODE_TI1;

    ....

    HAL_TIM_Encoder_Init(&htim2,  &enci);//initialisation..

    //after initialisation , start all encoders

    HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_1) ;//start each encoder

    HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_1) ;

    ....

    Elliott Li
    Associate
    Jul 26, 2017 3:55 PM UTC
    Posted on July 26, 2017 at 15:55

    Thank you