cancel
Showing results for 
Search instead for 
Did you mean: 

How Can I measure the input frequency by Timer Input Capture?

dKim.1
Associate II

Hi

I measure the frequency from sensor using the Timer Input Capture mode.

0693W00000Ka2LTQAZ.png 0693W00000Ka2MkQAJ.pngI set the timer can read minimum 1Hz.

72M / 7200/65535 = 0.15Hz

This is my code.

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
  HAL_GPIO_WritePin(LED_PORT, IN_LED_PIN, GPIO_PIN_SET);
 
  if (Is_First_Captured == 0)
  {
    IC_Value1 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_2);
    //TIM2->CNT = 0;
    tim2_ovc = 0;
    Is_First_Captured = 1;
  }
  else if (Is_First_Captured)
  {
    IC_Value2 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_2);
  
    if (IC_Value2 > IC_Value1)
    	Period = IC_Value2 - IC_Value1;
    else if (IC_Value2 < IC_Value1)
    {
    	Period =  (htim2.Instance->ARR - IC_Value1) + IC_Value2; 
    }
 
    freq = (HAL_RCC_GetPCLK2Freq() * 2)/(htim2.Instance->PSC + 1);
    Frequency = freq/Period;
    Is_First_Captured = 0;
  }  
 }

But It can't measure the frequency. (When input frequency 1Hz,..)

What's wrong my code and setting?

Please help me..:sad_but_relieved_face:

0 REPLIES 0