cancel
Showing results for 
Search instead for 
Did you mean: 

How can i configure external timer as counter,Below is my code snippet: But it is not working?

sankeerthan reddy
Associate II

Hi Team, 

I am working on STM32F100VC for a flow sensor project. 

Here i am planning to use a Timer as Counter to read the pulses of flow meter. If i give 50 count, after sensing(counting) 50 pulses, i should get an interrupt. Then i can do the required functionality. I can do this by simple External trigger, and count inside the ISR. But as i have multiple sensors i can't keep this logic and i always need to handle ISRs. 

A timer as counter will help me here. Below is my code snippet: But it is not working.

// I have 3 pins used for 3 flow meter sensors

void ConfigureFlowMeterTimers(void)

{

 GPIO_InitTypeDef GPIO_InitStructure;

 GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_9; 

GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPD; 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_Init(GPIOC, &GPIO_InitStructure);

}

// GPIOC - configured properly in RCC_APB2PeriphClockCmd()

Using Timer 3, Timer 13 and Timer 14 for these pins.

void StartTimer13ForFlowMeter1(uint16_t PulseCount)

{

 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 

TIM_ICInitTypeDef TIM5_ICInitStructure;   

TIM_TimeBaseStructure.TIM_Period = PulseCount; 

TIM_TimeBaseStructure.TIM_Prescaler = 0; 

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 

TIM_TimeBaseStructure.TIM_CounterMode  = TIM_CounterMode_Up;

 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

 TIM_TimeBaseInit(TIM13, &TIM_TimeBaseStructure);

  TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; //Select the input IC1 is mapped to TI1

  TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //Rising along the capture

  TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //Mapping to the TI1

  TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //Configure input frequency, regardless of the frequency 

  TIM5_ICInitStructure.TIM_ICFilter = 0x00;//The IC1F=0000 configuration input filter without filter

  TIM_ICInit(TIM13, &TIM5_ICInitStructure);

 TIM_TIxExternalClockConfig(TIM13,TIM_TIxExternalCLK1Source_TI2,TIM_ICPolarity_Rising,0); 

 TIM_ITConfig(TIM13, TIM_IT_CC1, ENABLE);  

 TIM_Cmd(TIM13, ENABLE);

}

////// In main.c

In main ()

{

 StartTimer13ForFlowMeter1(50);

}

/////  In stm32f1xx_it.c :

void FLOW_METER_1_TIMER_IRQ_HANDLER(void)

{

  if(TIM_GetITStatus(TIM13, TIM_IT_CC1) != RESET) 

  {  

    TIM_ClearITPendingBit(TIM13, TIM_IT_CC1);

    DispStatus = TRUE; 

  }

}

My intention is, when 50 pulses counted, it should generate an interrupt. But interrupt is coming every time instead of 50 counts.

Your suggestion will help me to come over it. 

Thanks,

Raj

2 REPLIES 2
T J
Lead

Input capture will interrupt on every Edge.

it is usually meant to capture the current counter, for timing calculations.

in your case you want a counter, not input capture.

look for the Timer Counter clock input pin,

that will be able to count pulses,

then set your Output compare to 50,

you will get that interrupt after 50 edges on the clock input pin.

As TJ said above, the input capture interrupt will throw interrupt on every edge. I don't use the "library" but if I properly understand what that code is intended to do, you will get an Update (a timer overflow/reload) every 50 pulses, so you want to use the Update interrupt instead of the Capture interrupt.

JW