cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407VG discovery kit read multiple encoder

Elliott Li
Associate II
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.

1 ACCEPTED SOLUTION

Accepted Solutions
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) ;

....

View solution in original post

3 REPLIES 3
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

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) ;

....

Posted on July 26, 2017 at 15:55

Thank you