cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with prescaler TIMER in ENCODER mode

jsi
Associate II
Posted on April 18, 2014 at 22:04

Hi

I configured the timer in encoder mode with prescaler:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8 | GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;

GPIO_Init(GPIOA,&GPIO_InitStructure); 

  

GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_TIM1);

TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);  

TIM_TimeBaseStructure.TIM_Period = PERIOD;

TIM_TimeBaseStructure.TIM_Prescaler = 0x0001;  //PROBLEM!!!!

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000; 

TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

TIM_EncoderInterfaceConfig(TIM1, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);

TIM1->CNT = 0;              

TIM_Cmd(TIM1, ENABLE);

and found a strange thing...

Encoder does not work properly if prescaler = 1 or 3 or 5 or 7 and ect...

and Encoder does work properly if prescaler = 0 or 2 or 4 or 6 and ect...

what I mean correct:

I connected to STM32f405 only channel A of encoder and channel B is not connected

if prescaler = 1 or 3 or 5 or 7 and ect... timer make increment count, but it is not correct

if prescaler = 0 or 2 or 4 or 6 and ect... timer not make increment count, this is correct, becouse channel B is not connected

How solved this problem?

#hee-be-dragons #prescaler
8 REPLIES 8
jsi
Associate II
Posted on April 24, 2014 at 21:56

I can't see problem in my code. So maybe this is bug in TM32f405?

In normal CNT of TIMER must not make increment count, when only one phase (A) is connect and (B) phase of encoder is not connected.

But if prescaler = 1 or 3 or 5 or 7 and ect... timer make increment count.

Thus if the divisor is an odd number, then counter accumulates the error position, when encoder make direct rotations and reverse rotations.

In Errata I can't find information about this problem. Maybe it is new bug? Can somebody who also work with encoder now, check this problem?
Posted on April 24, 2014 at 22:15

Perhaps the use of the prescaler is inappropriate in encoder mode? The content of the divider is indeterminate, and probably does play well with the up/down concept.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jsi
Associate II
Posted on April 29, 2014 at 10:54

Hi clive1

What mean content of the divider is indeterminate?

 Encoder does work properly if prescaler = 0 or 2 or 4 or 6 and ect.. and not work correct if 1,3,5,7... ect.  I think divider for encoder it is standart thing, and it must work properly anyway. Maybe I have mistake in code? or it's bug in chip?
Posted on April 29, 2014 at 15:55

What mean content of the divider is indeterminate?

You have no visibility into it (content or function), and none of the diagrams suggest it supports UP/DOWN counting, only that it prescales a clock source. I don't think it's supposed to work in encoder mode, but persist if you must.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
arsalan
Associate
Posted on May 29, 2014 at 08:09

Hi dear Clive

I have also some problems with my prescaler in input capture mode.

The prescaler does not work when I give the TIMx -> PSC a value, in my code.

But it does work when I give this register a value manually in debug mode.

How can I initialize the prescaler in my code?

jpeacock2399
Associate II
Posted on May 29, 2014 at 16:11

Since the counter clock is generated by combining the two phase input I'm not sure just what you expect to prescale.  Are you trying to divide the counter by 2?  Do you want to count one step (phase A) for every two direction pulses (phase B), or vice versa?  What happens if you reverse direction half way through a prescale divide?  How does the edge jitter suppression work if one phase is prescaled but not the other?

It would be much easier to just shift the value read from the counter register.

  Jack Peacock
Posted on May 29, 2014 at 16:33

Yeah, I don't know if this is an encoder question, of if the thread has been hijacked for an Input Capture question.

The prescaler on the timebase would control the speed at which the TIM->CNT ticks in Input Capture. The prescaler on the input capture side (not PSC, rather ICxPSC) will allow for multiple transitions on the channel pin before the value of TIM->CNT is latched into TIM->CCRx

Using a prescaler in encoder/decoder mode remains asinine in my opinion. The behaviour of the design is not adequately described, and it's not a good idea.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 29, 2014 at 16:56

> The behaviour of the design is not adequately described

Oh yes, but this remains true for the whole timer as such... :|

JW