Skip to main content
Visitor II
January 17, 2024
Question

Problem in Input Capture Frequency in STM32F401RE Nucleo-64.

  • January 17, 2024
  • 3 replies
  • 1108 views

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.

 

    This topic has been closed for replies.

    3 replies

    Super User
    January 17, 2024

    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.

    MG1Author
    Visitor II
    January 18, 2024
    #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
    		}
    	}
    }
    What I receive:

    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?

    Super User
    January 18, 2024

    Perhaps go off of an existing example:

    https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM32446E-Nucleo/Examples/TIM/TIM_PWMInput

     

    What does your code do if IC_Val2 == IC_Val1?

    Step through, set breakpoints, ensure clock is actually running, see