cancel
Showing results for 
Search instead for 
Did you mean: 

Measuring the input signal frequncy with stm32f4

naren
Associate II
Posted on January 27, 2015 at 18:27

Hello everyone,

I am new to STM32f4 board. I would like to measure speed of a dc motor(in terms of input signal frequency which varies from 0 Hz to 50KHz).

Actually I have only one signal (so I think I can't use encoder mode of stm32 timer.??). Can someone help me..??

Is there any way by which  I can use timer in encoder mode..??

5 REPLIES 5
Posted on January 27, 2015 at 19:23

Input Capture or Input PWM might be more effective by measuring period/duty. You can also count external pulses and integrate over time.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
naren
Associate II
Posted on January 28, 2015 at 09:43

thanks clive1 for reply,

is there a way to measure the time between two edges (between rising and falling edge), I am asking this because my input frequency is changing too fast ??

Posted on January 28, 2015 at 10:05

You can do it using two channels, one for each edge.

See the ''PWM input mode'' chapter in manual - the principle is the same, except you don't reset the counter this time.

JW

naren
Associate II
Posted on January 28, 2015 at 12:10

hi waclawek.jan,

I tried with PWM input mode, but the problem is it can not measure the freq below

1280 Hz. But in my application i need to measure the frequencies upto 20 Hz.?

So can u please tell me correction in the following code.??

code:

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;

  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 = 0x0;

  TIM_PWMIConfig(TIM4, &TIM_ICInitStructure);

  /* Select the TIM4 Input Trigger: TI2FP2 */

  TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */

  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);

  TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */

  TIM_Cmd(TIM4, ENABLE);

  /* Enable the CC2 Interrupt Request */

  TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);

void TIM4_IRQHandler(void)

{

  RCC_ClocksTypeDef RCC_Clocks;

  RCC_GetClocksFreq(&RCC_Clocks);

  /* Clear TIM4 Capture compare interrupt pending bit */

  TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);

  /* Get the Input Capture value */

  IC2Value = TIM_GetCapture2(TIM4);

  if (IC2Value != 0)

  {

    /* Duty cycle computation */

    DutyCycle = (TIM_GetCapture1(TIM4) * 100) / IC2Value;

    /* Frequency computation

       TIM4 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */

    Frequency = (RCC_Clocks.HCLK_Frequency)/2 / IC2Value;

  }

  else

  {

    DutyCycle = 0;

    Frequency = 0;

  }

}

In above code I tried changing prescalar value. but that was of no use.

Posted on January 28, 2015 at 13:34

I tried with PWM input mode, but the problem is it can not measure the freq below

 

1280 Hz. But in my application i need to measure the frequencies upto 20 Hz.?

 

So can u please tell me correction in the following code.??

Certainly should be capable of 20 Hz, but thought you said 50 KHz earlier? For 16-bit timers you'd need to be conscious of the timebase configuration, and the wrap time of the counter.

Code fragments really aren't helpful, post complete and concise example code that can be compiled. Use the Format Code Block tool (Paintbrush [<>] icon)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..