cancel
Showing results for 
Search instead for 
Did you mean: 

How to config my timer to count external pulses

moran
Associate II
Posted on July 02, 2014 at 13:39

Hello everybody,

How can i config my timer ( i work on stm32f100 ) to count external pulses

this is my code but it doesn't work:

void

ConfigTimer17(void)

{

 

 GPIO_InitTypeDef GPIO_InitStructure;

  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

 

 

  /* TIM17 clock enable */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE);

 

  /* GPIOB Configuration: TIM17 CH1 (PB9) */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 

  GPIO_Init(GPIOB, &GPIO_InitStructure);

 

    /* Connect TIM17 pins to AF */

  GPIO_PinRemapConfig(GPIO_Remap_TIM17,DISABLE); //<--> PB9

 

 TIM_TimeBaseStructure.TIM_Period = 65535;

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM17, &TIM_TimeBaseStructure);

 

  TIM_TIxExternalClockConfig(TIM17, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);

 

  TIM_Cmd(TIM17, ENABLE);

///////////////////////////////////////////

}

when i read capture1 ( TIM_GetCapture1(TIM17)) i get random numbers ( also negative ).

thanks in advance
6 REPLIES 6
Posted on July 02, 2014 at 14:02

Do you have the GPIOB and AFIO clocks enabled?

If you get ''negative'' numbers you should perhaps be using unsigned variables, and routines to print them.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
moran
Associate II
Posted on July 02, 2014 at 15:12

Hi Clive....Thanks for helping me.

Yes the gated clk are all enable.

I've more specific question.

1) should it count only the external pulses (on every rising )..... or it an trigger to run TIM17 counter that run at 24MHz i couldn't understand it.

Posted on July 02, 2014 at 16:23

In the External Clock context you should use TIM_GetCounter() to get the TIMx->CNT value that's ticked by your external input. If you sample this periodically (1Hz if the frequency is low, and 16-bit counters don't wrap) you can get the frequency out.

With the Input Capture context, you have the counter free run at the TIMCLK frequency, and the value of the Counter is latched into the Capture Register, effectively time stamping it against the time base. Getting subsequent values out allows you to measure the period, and consequently the frequency.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
moran
Associate II
Posted on July 02, 2014 at 16:56

So the TIM17->CNT is number that i need to calc ( multiple with counter clk , for example 0.041uSec ) in order to get the external pulse counting.

I thought that the increment of each bit in the TIM17->CNT is eqaul to each rising edge input.

let me know if i finnaly understand this input counter.

moran
Associate II
Posted on July 02, 2014 at 22:22

..... I'm looking for rising edge count not timer count.

my app is dc motor feedback ,and when i read the TIM_CNT while running the motor very slowely ... ~1 cycle per second it looks like timer count because i get big number instead small one.... only few cycles.

moran
Associate II
Posted on July 03, 2014 at 08:47

I just notice something

In the STM library ( attached below ) its written that i cant'nt config Timer 17 to count external clock only as trigger to timer : ( i've marked in function comment )

/**

  * @brief  Selects the Input Trigger source

  * @param  TIMx: where x can be 

1, 2, 3, 4, 5, 8, 9, 12 or 15

to select the TIM peripheral.

  * @param  TIM_InputTriggerSource: The Input Trigger source.

  *   This parameter can be one of the following values:

  *     @arg TIM_TS_ITR0: Internal Trigger 0

  *     @arg TIM_TS_ITR1: Internal Trigger 1

  *     @arg TIM_TS_ITR2: Internal Trigger 2

  *     @arg TIM_TS_ITR3: Internal Trigger 3

  *     @arg TIM_TS_TI1F_ED: TI1 Edge Detector

  *     @arg TIM_TS_TI1FP1: Filtered Timer Input 1

  *     @arg TIM_TS_TI2FP2: Filtered Timer Input 2

  *     @arg TIM_TS_ETRF: External Trigger input

  * @retval None

  */

void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource)

Unforthenetly i must to work with timer 17 beacause my external input hard wire ( on PCB ) to PB9.