cancel
Showing results for 
Search instead for 
Did you mean: 

How can I measure the period of a wave using a comparator and timer?

ADaxa.1
Associate II

I am using the STM32L476G Discovery board and I understand that the COMP is able to trigger interrupts using the exti line 21 and that can be used to along with the timer to measure the time between consecutive rising edges or falling edges. However what I'm struggling with is with the coding. I've looked at countless tutorials but I don't quite see a pattern that I can use to implement my own code. Are there perhaps any other resources I can use to help me?

1 ACCEPTED SOLUTION

Accepted Solutions

While you can use interrupts, probably better way is to use the connection from comparator to input capture of TIM1, TIM2, TIM3 or TIM8.

For example, look at TIM3_OR1: if you set TIM3_OR1.TI_RMP to 0b01, the input of TIM3_CH1 will come from COMP1; if you set it to 0b10, it will come from COMP2.

Then it's just matter to set up the timer to run, enable capture on CH1 and the period is given by difference between two successively captured values.

JW

View solution in original post

16 REPLIES 16

Which STM32?

You probably don't want to go through EXTI, but use the links between comparator and timer. Refer to the Interconnections chapter in the RM, and of course read the Comparator and Timer chapters.

JW

ADaxa.1
Associate II

STM32L476G Discovery board. My apologies I thought I had included it.

MM..1
Chief II

When you activate and configure COMP and TIM in CubeIDE project wizard CubeMX , then in it file is called interrupt callback and weak empty exist

/**
  * @brief  Comparator trigger callback.
  * @param  hcomp COMP handle
  * @retval None
  */
__weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hcomp);
 
  /* NOTE : This function should not be modified, when the callback is needed,
            the HAL_COMP_TriggerCallback should be implemented in the user file
   */
}

As comment says you need in user code for example in main.c overide this with real code.

In this code simply get timer value and calculate what you need..

While you can use interrupts, probably better way is to use the connection from comparator to input capture of TIM1, TIM2, TIM3 or TIM8.

For example, look at TIM3_OR1: if you set TIM3_OR1.TI_RMP to 0b01, the input of TIM3_CH1 will come from COMP1; if you set it to 0b10, it will come from COMP2.

Then it's just matter to set up the timer to run, enable capture on CH1 and the period is given by difference between two successively captured values.

JW

How about the "External Output to TIM1 BKIN" connections for the output of the comparator? Are they useful in this regard?

P.s. Thank for your reply, I will investigate your solution immediately.

Is that the function that triggers the interrupt? I'm sorry I'm quite new to the stm32 therefore I'm don't quite understand your response. Do you mind clarifying a bit more?

> How about the "External Output to TIM1 BKIN" connections for the output of the comparator?

I don't think so, you don't want to use the break facility (i.e. disconnecting timer output when comparator output changes) of the timer.

JW

All code for you is generated when you click generate in CubeMX ioc file save and close.

__weak functions is generated but you need write own version in main files.

Here for example you write

/* USER CODE BEGIN 4 */
 
 
void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hcomp);
 
  //place your code here
 
}
 
/* USER CODE END 4 */

Ok thanks.