cancel
Showing results for 
Search instead for 
Did you mean: 

Encoder mode both edges

pivafrancy
Associate II
Posted on March 31, 2016 at 17:02

Hi to everybody!

I need some help to correct this code. It seems to be correct, but it counts only 8 edges per cycle, while I need to take into account all the 16 edges that are avaiable (rising and falling, 4 sensors per cycle, quadrature encoder)

What is my error?

void MX_TIM1_Init(void)

{

  TIM_Encoder_InitTypeDef sConfig;

  TIM_MasterConfigTypeDef sMasterConfig;

  htim1.Instance = TIM1;

  htim1.Init.Prescaler = 1;

  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim1.Init.Period = 0xFFFF;

  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  htim1.Init.RepetitionCounter = 0;

  sConfig.EncoderMode = TIM_ENCODERMODE_TI12;

  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; //TIM_ICPOLARITY_RISING

  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;

  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;

  sConfig.IC1Filter = 0;

  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; //;TIM_ICPOLARITY_BOTHEDGE

  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;

  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;

  sConfig.IC2Filter = 0;

  HAL_TIM_Encoder_Init(&htim1, &sConfig);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);

}

Thank you

#tim #stm32 #encoder
9 REPLIES 9
mark239955_stm1
Associate II
Posted on April 03, 2016 at 07:04

You haven't stated which STM32 your question applies to, so I've drawn the following from the STM32F407 reference manual v11, p569-571.

For TIM1/8, in the CCER register, when TIM1/8 CH2 is configured as an input, the CCxNP:CCxP bits are used to select which edges are input triggers; if you set both bits, then TIM1/8 will trigger on both rising and falling edges.  It's a bit confusing because the bits aren't contiguous in the register.

pivafrancy
Associate II
Posted on April 04, 2016 at 18:09

OK, my board is an STM32Nucleo F411RE.

So I have only to set TIM1 register CCER CC1P to 1 and CCER CC1NP to 1? It is sufficient to set this two conditions?

Thank you

Posted on April 05, 2016 at 08:54

> For TIM1/8, in the CCER register, when TIM1/8 CH2 is configured as an input, the CCxNP:CCxP bits are used to select which edges are input triggers;

> if you set both bits, then TIM1/8 will trigger on both rising and falling edges. 

Did you try it together with encoder mode? RM appears to say it won't work in that way:

''This configuration must not be used in encoder mode.'' (RM0090 rev.11 p.571, for CC1NP/CC1P = 0b11)

JW

pivafrancy
Associate II
Posted on April 05, 2016 at 09:59

Yes, I used encoder mode, and (as written in RM) it doesn't work with ''both edges'' activated. It only works with rising edge, but with half of resolution... so to have 100% of resolution I have not to use encoder mode?

Posted on April 05, 2016 at 10:33

Encoder mode 3 (TIMx_SMCR.SMS = 0b011) should work as you expect, with TIMx_CCER left at all zero.

Try with very slow input signals, way lower than the internal clock, and make sure the two signals don't overlap. Ideally, manipulate inputs ''manually'' (you still need clean edges) and watch the counter and/or DIR bit e.g. in debugger.

JW

pivafrancy
Associate II
Posted on April 07, 2016 at 12:36

My values are equal to yours' but it still work with only 50% of maximal resolution :( you can check in the image below...

________________

Attachments :

foto_forum.png : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I1GC&d=%2Fa%2F0X0000000bkO%2F4Wd24sDFYBRonMKHVRQqmyhEb1sv.x6KS4OtZc8C8Ks&asPdf=false
acacio Marques
Associate II
Posted on December 06, 2017 at 12:40

Hi, 

I have the same problem. Something succeed in having 100 % of resolution ( *4 ).

Posted on December 06, 2017 at 23:07

I didn't notice previously in the original post, now I've spotted it: prescaler should be left at 0.

marques.

post the readout of your timer's registers.

JW

Posted on December 07, 2017 at 10:02

 ,

 ,

OK. It’s work very well when PSC = 0 and not 1.

Thanks, Regards.

De : waclawek.jan

Envoyé : mercredi 6 décembre 2017 23:09

À : acacio Marques <,marques@emse.fr>,

Objet : Re: - Re: Encoder mode both edges

<,https://community.st.com/?et=watches.email.thread>, STMicroelectronics Community

Re: Encoder mode both edges

reply from waclawek.jan <,https://community.st.com/0D70X000006SrOwSAK