0
\$\begingroup\$

I want to count the pulses generated by Picoscope, my signal frequency is (20Hz)

I used the timer2, the problem is that the counter increment to 1 and came back to 0, I don't understand why ??? since it must increment every 50ms.

enter image description here

I devellopped the code show bellow :

 #include "stm32f4xx.h"
 #include "stm32f4xx_rcc.h"
 #include "stm32f4xx_gpio.h"
 #include "stm32f4xx_tim.h"

 void GPIO_Init_TM(void);

 int variable = 0;

 int main(void)
 {
  GPIO_Init_TM();
  while(1)
  {
    variable = TIM_GetCounter(TIM2);
  }
 }

 void GPIO_Init_TM(void)

 {

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

GPIO_InitTypeDef gpioStructure;
//TIM_TimeBaseInitTypeDef timerInitStructure;

gpioStructure.GPIO_Pin = GPIO_Pin_15 ;
gpioStructure.GPIO_Mode = GPIO_Mode_AF;
gpioStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOA, &gpioStructure);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_TIM2);

TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI1,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising );
TIM_SetAutoreload (TIM2, 0xffff);

TIM_Cmd(TIM2, ENABLE);

}

and this is the result I get :

enter image description here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Any questions here? \$\endgroup\$
    – Batuu
    Jul 18, 2017 at 8:56
  • \$\begingroup\$ The counter must increment every 50ms since the Frequency is at 50 20Hz, but it increments just to 1 and came back to 0, I don't understand why ? \$\endgroup\$ Jul 18, 2017 at 9:01

1 Answer 1

2
\$\begingroup\$

Your problem is, that you configured your TIM in encoder mode. An quatrature encoder uses two inputs with 90° phase shifted pulse signals to detect up or down counting. See the red path in the picture below.

So delete :

TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI1,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising );

enter image description here

If you just want a single input pulse counter, you have to use the external trigger (TIMx_ETR) as timer trigger. (green path in the pic)

Add a complete timer initialization and something like:

TIM_SelectInputTrigger(TIM2, TIM_TS_ETRF);
\$\endgroup\$
1
  • \$\begingroup\$ For Timer 9 I used this function TIM_TIxExternalClockConfig(TIM9, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0x0F); and it works, the external signal is on the pin PA3 channel 2 for the TImer 9 \$\endgroup\$ Jul 21, 2017 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.