cancel
Showing results for 
Search instead for 
Did you mean: 

HAL TIM IC Capturecallback on multiple functions

Giordano
Associate
Hello,
I'm beginner in STM32 and would need help with HAL callback function usage.
When I've started to use STM32s after PICs, one of the reasons why I was happy that the timer module encoder receiver function works really well, robust. I did used other interrupts, but somehow I have not used other timer interrupts ... until now. And now I'm stuck.
I try to use an STM32G474 with an I2C oled on, encoder on timer 4, some spi peripherials and want to measure frequency on timer 5. My problem is, with HAL, how can I separate the two actions? Both encoder and time measurement calls the HAL_TIM_IC_CaptureCallback. Both works fine ... as long as there is no interrupt coming in from the other. At the moment the function looks like what I copy below. Can you please help how to separate the 2 actions, how to refer in in(**) to one or the other or any other, but with using HAL.
Thanks!!
JG

 

 

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;
}

1 REPLY 1

Differentiate which TIM instance is being handled, perhaps move the globals into the instance 

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