Skip to main content
Graduate
May 10, 2024
Solved

Continuous relative frequency measurement

  • May 10, 2024
  • 1 reply
  • 831 views

I want to continuously measure the frequency of signal1 relative to signal2.

Signal1 can vary between 1-80 MHz, signal2 between 5-15 MHz.

My approach is:

Timer1 counts signal1.

Timer2 counts signal2 with a counter period of 200. This gives me an event roughly at 10 MHz / 200 = 50 kHz. When this happens, timer1 should be saved to memory (old values can be overwritten) and reset to 0.

How do I implement this in STM32CubeIDE? Especially the "one timer triggers reading of the other timer" is not clear to me.

    This topic has been closed for replies.
    Best answer by Banana123

    Sorry for the late reply @VK2SID 

    If this is still relevant, here is something to get you started:

    Create project for Nucleo H753 board in the STM32CubeIDE

    Change these settings

    at least one of these is necessary, not sure which:

    USART3 Disable
    USB_OTG_FS Disable
    System Core RCC HSE, LSE Disable
    Clock Configuration PLL Source Mux CSI

     

    Tim5 Clock Source Internal:

    For Overflow every second, set Parameter Settings Prescaler to 64000-1 and Counter Period 1000-1
    Parameter Settings Trigger Event Selection TRGO Update Event

     

    Tim8 Clock Source Internal:

    Trigger Source ITR3 (which Tims a Tim can trigger is listed in the interconnect matrix/table in the reference manual)
    Channel1 Input capture TRC
    Counter period 65535
    NVIC Enable Tim8 capture compare interrupt

    Add this code:

     

     /* USER CODE BEGIN PV */
    uint32_t uwICValue = 0;
    uint32_t uwICValuePrev = 0;
    int32_t uwICValueDiff = 0;
    /* USER CODE END PV */
    
    
     /* USER CODE BEGIN 2 */
     HAL_TIM_Base_Start_IT(&htim5);
     HAL_TIM_Base_Start_IT(&htim8);
    
     HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_1);
     /* USER CODE END 2 */
    
    /* USER CODE BEGIN 4 */
    void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim){
    	if(htim->Instance == TIM8){
    		HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
    		uwICValuePrev = uwICValue;
    		uwICValue = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
    		uwICValueDiff = uwICValue - uwICValuePrev;
    		if(uwICValueDiff < 0){
    			uwICValueDiff += 65535 + 1; //has to match overflow value of Tim8
    		}
    
    	}
    }
    /* USER CODE END 4 */

     

    This now reads the internal generated frequency. For external frequency, set Tim8 to ETR2, prescaler 1. With this PA0 becomes the input pin. Maybe in the system core GPIO settings configure a pull up/down resistor.

    1 reply

    Visitor II
    July 12, 2024

    I am also looking for a solution for this where I need to measure RF up to 54MHz. Did you find a solution to that, which input pin do you use for that and is there a code available or you would have written to count the frequency? Thanks, Sid

    Banana123AuthorAnswer
    Graduate
    February 19, 2025

    Sorry for the late reply @VK2SID 

    If this is still relevant, here is something to get you started:

    Create project for Nucleo H753 board in the STM32CubeIDE

    Change these settings

    at least one of these is necessary, not sure which:

    USART3 Disable
    USB_OTG_FS Disable
    System Core RCC HSE, LSE Disable
    Clock Configuration PLL Source Mux CSI

     

    Tim5 Clock Source Internal:

    For Overflow every second, set Parameter Settings Prescaler to 64000-1 and Counter Period 1000-1
    Parameter Settings Trigger Event Selection TRGO Update Event

     

    Tim8 Clock Source Internal:

    Trigger Source ITR3 (which Tims a Tim can trigger is listed in the interconnect matrix/table in the reference manual)
    Channel1 Input capture TRC
    Counter period 65535
    NVIC Enable Tim8 capture compare interrupt

    Add this code:

     

     /* USER CODE BEGIN PV */
    uint32_t uwICValue = 0;
    uint32_t uwICValuePrev = 0;
    int32_t uwICValueDiff = 0;
    /* USER CODE END PV */
    
    
     /* USER CODE BEGIN 2 */
     HAL_TIM_Base_Start_IT(&htim5);
     HAL_TIM_Base_Start_IT(&htim8);
    
     HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_1);
     /* USER CODE END 2 */
    
    /* USER CODE BEGIN 4 */
    void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim){
    	if(htim->Instance == TIM8){
    		HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
    		uwICValuePrev = uwICValue;
    		uwICValue = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
    		uwICValueDiff = uwICValue - uwICValuePrev;
    		if(uwICValueDiff < 0){
    			uwICValueDiff += 65535 + 1; //has to match overflow value of Tim8
    		}
    
    	}
    }
    /* USER CODE END 4 */

     

    This now reads the internal generated frequency. For external frequency, set Tim8 to ETR2, prescaler 1. With this PA0 becomes the input pin. Maybe in the system core GPIO settings configure a pull up/down resistor.