2024-01-17 03:34 AM
Good afternoon,
My quarry regarding Input Capture frequency in STM32F401re micro controller,
I am measuring frequency by Timer1 configuration and Timer2 as an interrupt generator.
in that i was not able to read the capture value from function
"HAL_TIM_ReadCapturedValue(htim1, TIM_CHANNEL_1)"
in this function:
uint32_t HAL_TIM_ReadCapturedValue(TIM_HandleTypeDef *htim, uint32_t Channel)
{
uint32_t tmpreg = 0U;
switch (Channel)
{
case TIM_CHANNEL_1:
{
/* Check the parameters */
assert_param(IS_TIM_CC1_INSTANCE (htim->Instance));
/* Return the capture 1 value */
tmpreg = htim->Instance->CCR1;
break;
}
case TIM_CHANNEL_2:
{
/* Check the parameters */
assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
/* Return the capture 2 value */
tmpreg = htim->Instance->CCR2;
break;
}
case TIM_CHANNEL_3:
{
/* Check the parameters */
assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
/* Return the capture 3 value */
tmpreg = htim->Instance->CCR3;
break;
}
case TIM_CHANNEL_4:
{
/* Check the parameters */
assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
/* Return the capture 4 value */
tmpreg = htim->Instance->CCR4;
break;
}
default:
break;
}
return tmpreg;
}
and my main code is
please guide me for that and give your kind response.
Here is my project file for your reference hope that will help you.
Thank you,
Community.
2024-01-17 05:51 AM
> i was not able to read the capture value
Why not? What happened when you tried? What result did you get vs what result did you expect?
There are unlikely to be any bugs in the HAL_TIM_ReadCapturedValue function.
2024-01-17 09:27 PM
#define TIMCLOCK 84000000 #define PRESCALAR 84 uint32_t IC_Val1 = 0; uint32_t IC_Val2 = 0; uint32_t Difference = 0; int Is_First_Captured = 0; /* Measure Frequency */ float frequency = 0; void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) { if (Is_First_Captured==0) // if the first rising edge is not captured { IC_Val1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); // read the first value Is_First_Captured = 1; // set the first captured as true } else // If the first rising edge is captured, now we will capture the second edge { IC_Val2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); // read second value if (IC_Val2 > IC_Val1) { Difference = IC_Val2-IC_Val1; } else if (IC_Val1 > IC_Val2) { Difference = (0xffffffff - IC_Val1) + IC_Val2; } float refClock = TIMCLOCK/(PRESCALAR); frequency = refClock/Difference; __HAL_TIM_SET_COUNTER(htim, 0); // reset the counter Is_First_Captured = 0; // set it back to false } } }
The above code value of "IC_Val1" is not update,
because in that function (as shown upside), tempreg value is not read from CCR1 register and I can see '0' value I receive after callback function.
So, garbage value is print on consol.
What is expected output:
The expecting output is that value is read by function which we are gave by function generator via pin.
So updated value of that frequency variable.
So, my concern is that should I use other function in my IDE or should i change in function or my code(main.c file)?
Please, guide me.
And other thing is that should I put this function or not?
TIM1->CCR1 = 50;
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
in above three lines which are required for Input capture?
2024-01-18 06:12 AM
Perhaps go off of an existing example:
What does your code do if IC_Val2 == IC_Val1?
Step through, set breakpoints, ensure clock is actually running, see