2016-03-31 08:02 AM
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 #encoder2016-04-02 10:04 PM
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.2016-04-04 09:09 AM
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 you2016-04-04 11:54 PM
> 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) JW2016-04-05 12:59 AM
2016-04-05 01:33 AM
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. JW2016-04-07 03:36 AM
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=false2017-12-06 03:40 AM
Hi,
I have the same problem. Something succeed in having 100 % of resolution ( *4 ).
2017-12-06 02:07 PM
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
2017-12-07 02:02 AM
,
,
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