cancel
Showing results for 
Search instead for 
Did you mean: 

How to measure frequency using Timers

Arman Ilmak
Senior
Posted on June 05, 2018 at 22:30

Hello guys.

When I used LPC17xx series in my last experience of ARM MCUs I used 2 timers one for generating interrupt every 1 second and one for counting the rising edges of the input CAP.

So I started reading the Reference menu of the stm32F2xx ,but there were a lot of timer functions described in timer section.I was confused which one to use for my purpose.

Now I know that how to set the timer to work as a TIME BASE,but for input I didn't know which one to use.One section described external clock mode 1 and the other external clock mode 2 and the other was input capture mode .

Which one should I use?

#stm32f2 #cube-hal
11 REPLIES 11
henry.dick
Senior II
Posted on June 08, 2018 at 02:22

'

Which one should I use?'

it depends on what you are trying to do.

The basic form of measuring frequency is to compare / gate two pulse trains, one of unknown frequency, and another of known frequency. 

1) the typical way is to use the known pulse train to generate a time base that turns on / off the counting of the unknown pulse train; this works for high frequency signal;

2) it can also be done the other way around: using the unknown pulse train to gate the known pulse train -> aka a reciprocal counter; it works for lower frequency measurement.

3) for cases where you don't need super high precision and the input pulse train isn't too fast / slow, you can use input capture to measure the frequency as well.

there are other ways too. all depends on what kind of signal you are trying to measure.

Posted on June 08, 2018 at 01:33

Sorry don't use CubeMX

The PWMInput examples should use CH1 and CH2 to report period and duty, and reset

STM32Cube_FW_F4_V1.21.0\Projects\STM32F4-Discovery\Examples\TIM\TIM_PWMInput\Src\main.c

  /*♯♯-1- Configure the TIM peripheral ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

  /* Set TIMx instance */

  TimHandle.Instance = TIM2;

  /* Initialize TIMx peripheral as follow:

       + Period = 0xFFFFFFFF

       + Prescaler = 0

       + ClockDivision = 0

       + Counter direction = Up

  */

  TimHandle.Init.Period = 0xFFFFFFFF;

  TimHandle.Init.Prescaler = 0;

  TimHandle.Init.ClockDivision = 0;

  TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;

  if(HAL_TIM_IC_Init(&TimHandle) != HAL_OK)

  {

    /* Initialization Error */

    Error_Handler();

  }

  /*♯♯-2- Configure the Input Capture channels ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

  /* Common configuration */

  sConfig.ICPrescaler = TIM_ICPSC_DIV1;

  sConfig.ICFilter = 0;

  /* Configure the Input Capture of channel 1 */

  sConfig.ICPolarity = TIM_ICPOLARITY_FALLING;

  sConfig.ICSelection = TIM_ICSELECTION_INDIRECTTI;

  if(HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)

  {

    /* Configuration Error */

    Error_Handler();

  }

  /* Configure the Input Capture of channel 2 */

  sConfig.ICPolarity = TIM_ICPOLARITY_RISING;

  sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;

  if(HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)

  {

    /* Configuration Error */

    Error_Handler();

  }

  /*♯♯-3- Configure the slave mode ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

  /* Select the slave Mode: Reset Mode */

  sSlaveConfig.SlaveMode     = TIM_SLAVEMODE_RESET;

  sSlaveConfig.InputTrigger  = TIM_TS_TI2FP2;

  if(HAL_TIM_SlaveConfigSynchronization(&TimHandle, &sSlaveConfig) != HAL_OK)

  {

    /* Configuration Error */

    Error_Handler();

  }

  /*♯♯-4- Start the Input Capture in interrupt mode ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

  if(HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_2) != HAL_OK)

  {

    /* Starting Error */

    Error_Handler();

  }

  /*♯♯-5- Start the Input Capture in interrupt mode ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

  if(HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_1) != HAL_OK)

  {

    /* Starting Error */

    Error_Handler();

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