AnsweredAssumed Answered

Frequency capture in STM32F7

Question asked by Edwin kaus on Oct 30, 2017
Latest reply on Nov 3, 2017 by Edwin kaus

I am working with STM32F765BIT MCU in our custom board, In that I need to measure some external signal frequency.

I am using timer in input capture mode to measure the frequency. I have taken some reference example and made same changes in my code. With that i am able to get capture complete call back interrupt ,but getting zero frequency. 

 

I am running MCU at 216MHz clock and using channel1 of Timer5 as input capture mode.

I am not able to get what could be the problem, Can anyone please help me in resolving this issue.

 

/**************************************************Init ************************************************************/

static void MX_TIM5_Init(void)
{

TIM_MasterConfigTypeDef sMasterConfig;
TIM_IC_InitTypeDef sConfigIC;

htim5.Instance = TIM5;
htim5.Init.Prescaler = 0;
htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
htim5.Init.Period = 0;
htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_IC_Init(&htim5) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 15;
if (HAL_TIM_IC_ConfigChannel(&htim5, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

}

/**********************************************call back function****************************************************************/

 

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim != &htim5 )
return;

if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
if(uhCaptureIndex == 0)
{
/* Get the 1st Input Capture value */
uwIC2Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
uhCaptureIndex = 1;
}
else if(uhCaptureIndex == 1)
{
/* Get the 2nd Input Capture value */
uwIC2Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);

/* Capture computation */
if (uwIC2Value2 > uwIC2Value1)
{
uwDiffCapture = (uwIC2Value2 - uwIC2Value1);
}
else if (uwIC2Value2 < uwIC2Value1)
{
/* 0xFFFF is max TIM3_CCRx value */
uwDiffCapture = ((0xFFFF - uwIC2Value1) + uwIC2Value2) + 1;
}
else
{
/* If capture values are equal, we have reached the limit of frequency
measures */
Error_Handler();
}
/* Frequency computation: for this example TIMx (TIM3) is clocked by
2xAPB1Clk */
uwFrequency = (2*HAL_RCC_GetPCLK1Freq()) / uwDiffCapture;
    printf("Frequency = %d\r\n",uwFrequency);

  uhCaptureIndex = 0;
}
}
}

/**************************************************************************************************************/

 The control is coming to this call back function but I am getting uwDiffCaputre as zero.

 

Thank you.,

Attachments

Outcomes