cancel
Showing results for 
Search instead for 
Did you mean: 

Using the encoder mode with TIM4

martinmartin9130
Associate II
Posted on March 04, 2013 at 21:06

Hi all, I'm using an STM32F4Discovery (it's and STM32F407VGT6 on it).

I'm trying to use the TIM4 timer to read a quadratic encoder (plugged on PB6-PB7), but somehow, it doesn't work.

I'm using exactly the same code with TIM8 and it does work (plugged on PC6-PC7). I validate the working code by reading the counter in GDB after turning the encoder by hand.

void

configureEncoder

()

{

RCC_AHB1PeriphClockCmd

(

RCC_AHB1Periph_GPIOB

,

ENABLE

);

RCC_APB1PeriphClockCmd

(

RCC_APB1Periph_TIM4

,

ENABLE

);

GPIO_InitTypeDef

GPIO_InitStructure

;

GPIO_InitStructure

.

GPIO_Mode

=

GPIO_Mode_AF

;

GPIO_InitStructure

.

GPIO_Speed

=

GPIO_Speed_100MHz

;

GPIO_InitStructure

.

GPIO_OType

=

GPIO_OType_PP

;

GPIO_InitStructure

.

GPIO_PuPd

=

GPIO_PuPd_NOPULL

;

GPIO_InitStructure

.

GPIO_Pin

=

GPIO_Pin_6

|

GPIO_Pin_7

;

GPIO_Init

(

GPIOB

,

&

GPIO_InitStructure

);

GPIO_PinAFConfig

(

GPIOB

,

GPIO_PinSource6

,

GPIO_AF_TIM4

);

GPIO_PinAFConfig

(

GPIOB

,

GPIO_PinSource7

,

GPIO_AF_TIM4

);

/* Configure the timer */

TIM_EncoderInterfaceConfig

(

TIM4

,

TIM_EncoderMode_TI12

,

TIM_ICPolarity_Rising

,

TIM_ICPolarity_Rising

);

/* TIM4 counter enable */

TIM_Cmd

(

TIM4

,

ENABLE

);

}

I expect my error to be simple, but I'm blind. I did not forget to move the physical wires between TIM8 and TIM4.

Both full codes are here:

https://gist.github.com/nraynaud/5082298

If you want some Stackoverflow love, here is the question:

http://stackoverflow.com/questions/15203069/stm32-rotary-encoder-config-on-tim4

Thanks for your help,

Nico

#stm32-timer-counter
4 REPLIES 4
Posted on March 04, 2013 at 22:07

Maybe initialize the time base?

  TIM_TimeBaseStructure.TIM_Period = 0xffff;

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
martinmartin9130
Associate II
Posted on March 04, 2013 at 22:25

It works, thanks!

Can you explain me the diference between TIM8 and TIM4 w/r time base initialization please ?

here is my working code for googlers :

void

configureEncoder

()

{

RCC_AHB1PeriphClockCmd

(

RCC_AHB1Periph_GPIOB

,

ENABLE

);

RCC_APB1PeriphClockCmd

(

RCC_APB1Periph_TIM4

,

ENABLE

);

GPIO_InitTypeDef

GPIO_InitStructure

;

GPIO_InitStructure

.

GPIO_Mode

=

GPIO_Mode_AF

;

GPIO_InitStructure

.

GPIO_Speed

=

GPIO_Speed_100MHz

;

GPIO_InitStructure

.

GPIO_OType

=

GPIO_OType_PP

;

GPIO_InitStructure

.

GPIO_PuPd

=

GPIO_PuPd_NOPULL

;

GPIO_InitStructure

.

GPIO_Pin

=

GPIO_Pin_6

|

GPIO_Pin_7

;

GPIO_Init

(

GPIOB

,

&

GPIO_InitStructure

);

GPIO_PinAFConfig

(

GPIOB

,

GPIO_PinSource6

,

GPIO_AF_TIM4

);

GPIO_PinAFConfig

(

GPIOB

,

GPIO_PinSource7

,

GPIO_AF_TIM4

);

TIM_TimeBaseInitTypeDef

TIM_TimeBaseStructure

;

TIM_TimeBaseStructure

.

TIM_Period

=

0xffff

;

TIM_TimeBaseStructure

.

TIM_Prescaler

=

0

;

TIM_TimeBaseStructure

.

TIM_ClockDivision

=

0

;

TIM_TimeBaseStructure

.

TIM_CounterMode

=

TIM_CounterMode_Up

;

TIM_TimeBaseInit

(

TIM4

,

&

TIM_TimeBaseStructure

);

/* Configure the timer */

TIM_EncoderInterfaceConfig

(

TIM4

,

TIM_EncoderMode_TI12

,

TIM_ICPolarity_Rising

,

TIM_ICPolarity_Rising

);

/* TIM4 counter enable */

TIM_Cmd

(

TIM4

,

ENABLE

);

}

Posted on March 04, 2013 at 22:55

TIM1 and TIM8 are ''special'', advanced I guess is the documented term, but there might just be something about how they are initialized, an ARR of zero basically disables the counter. My general rule is to not assume anything is in a reset/default state, and initialize it explicitly the way I want it to work.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
martinmartin9130
Associate II
Posted on March 04, 2013 at 22:59

Thanks, I'm at such a basic level of understanding of those beasts that I don't even know yet what is the minimum initialization required.