cancel
Showing results for 
Search instead for 
Did you mean: 

TIM5 counter

jampino
Associate II
Posted on September 02, 2016 at 12:41

Hi all,

I want to count pulses from an input, using TIM5 channel 3, I have tried with channel 1 and it is working but I cannot make it works in channel 3 , can someone let me know what can be wrong.

This is the code:

 

   GPIO_InitTypeDef GPIO_InitStructure;

   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

   /* GPIOH clock enable */

   RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOH, ENABLE);

   /* TIM5 clock enable */

   RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM5, ENABLE);

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;

   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_UP; 

   GPIO_Init(GPIOH, &GPIO_InitStructure);

   GPIO_PinAFConfig(GPIOH, GPIO_PinSource12, GPIO_AF_TIM5);

  TIM_TimeBaseStructure.TIM_Period = 65535;

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit (TIM5, &TIM_TimeBaseStructure);

  TIM_TIxExternalClockConfig (TIM5, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);

  TIM_Cmd (TIM5, ENABLE);

Thanks
8 REPLIES 8
Posted on September 02, 2016 at 14:04

You can use Channel 1 (TI1) or Channel 2 (TI2)

Be sure to state the specific STM32 part being discussed.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jampino
Associate II
Posted on January 06, 2017 at 12:25

Hi Clive,

Thanks for the response, I stopped working on that and I started again now.

I can see on the datasheet  pin PH12 use TIM5_CH3, need I change something to use channel 3 instead of channel 1 on TIM5?

Using PH10 (TIM5_CH1) works fine. 

Thanks

Posted on January 09, 2017 at 16:31

I have try to do this:

TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;

TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

TIM_ICInitStructure.TIM_ICFilter = 0x00;

TIM_ICInit( TIM5, &TIM_ICInitStructure );

but still having problems to count the pulses.

I have checked the signal in the input PH12 and it is correct, I can see an square signal.

Posted on January 09, 2017 at 23:14

As Clive told you above,

You can use Channel 1 (TI1) or Channel 2 (TI2)

ie. you can't use CH3 or CH4 as an external source for counting.

JW

S.Ma
Principal
Posted on January 10, 2017 at 04:56

If it is too late to move to channel 1 or 2 (see timer block diagram in reference manual), I would use timer input capture interrupt to count events manually. Shall the frequency be too high, could use dma to backup input captures in a say 256 cyclic buffer which would prescale by 256... With dma interrupt and reading dma register, something could probably be cooked. Make sense?

Posted on January 10, 2017 at 05:30

DMA's NDTR itself can be used as a counter, too.

JW

Posted on January 10, 2017 at 10:02

Thanks for your response, I can't change the channels, I need to use those pins PH10(Channel 1) and PH12(Channel 3).

The frequency can be around 7 KHz. I will try your suggestions.

Thanks

Khouloud GARSI
Lead II
Posted on January 11, 2017 at 18:38

Hi

Martinez_Pino.Jose_A

,

What STM32 are you using? and do you have to use the Timer 5 in your application?

Since you are using channel1, as the other users have already told you, you can not use the channel3 as a pulse counter (even if you have thought about the XOR function).

If it is possible to change the timer, you can use TIM1,TIM2,TIM3,TIM4 or TIM8which have an ETR pin. Then you can configure this pin as an input for your pulse counter.

Khouloud.