2025-02-03 02:01 PM
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
// If the Interrupt is triggered by 1st Channel
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
RPM_captured = 1;
}
if (RPM_captured)
{
indx_RPM = 0;
count_RPM = 0;
RPM_measure_average = 0;
/* increment the index until some useful data shows up */
while (RPM_data[indx_RPM] == 0) indx_RPM++;
/* Again at very high frequencies, sometimes the values don't change
* So we will wait for the update among the values
*/
while ((MIN((RPM_data[indx_RPM+1]-RPM_data[indx_RPM]), (RPM_data[indx_RPM+2]-RPM_data[indx_RPM+1]))) == 0) indx_RPM++;
/* RPM_measure_average is the difference in the 2 consecutive rise Time */
/* Assign a start value to RPM_measure_average */
RPM_measure_average += MIN((RPM_data[indx_RPM+1]-RPM_data[indx_RPM]), (RPM_data[indx_RPM+2]-RPM_data[indx_RPM+1]));
indx_RPM++;
count_RPM++;
/* start adding the values to the RPM_measure_average */
while (indx_RPM < (SamplesBlockSize))
{
RPM_measure_average += MIN((RPM_data[indx_RPM+1]-RPM_data[indx_RPM]), RPM_measure_average/count_RPM);
count_RPM++;
indx_RPM++;
}
/* Find the average RPM_measure_average, the average time between 2 RISE */
RPM_measure_average = RPM_measure_average/count_RPM;
indx_RPM = 0;
//motor_RPM = 60/((float)RPM_measure_average * 0.00000001);
motor_RPM = 60/(RPM_measure_average * 0.00000001);
platter_RPM = motor_RPM / belt_ratio;
RPM_captured = 0;
RPM_measured = 1;
}