Skip to main content
ADaxa.1
Associate II
Jan 2, 2021 2:16 PM UTC
Solved

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

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?

This topic has been closed for replies.
waclawek.jan
waclawek.janBest answer
Super User
Jan 2, 2021 7:01 PM UTC

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

16 replies

waclawek.jan
Super User
Jan 2, 2021 2:26 PM UTC

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
ADaxa.1Author
Associate II
Jan 2, 2021 2:27 PM UTC

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

MM..1
Super User
Jan 2, 2021 5:06 PM UTC

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..

ADaxa.1
ADaxa.1Author
Associate II
Jan 3, 2021 4:43 PM UTC

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?

MM..1
Super User
Jan 3, 2021 4:56 PM UTC

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 */

waclawek.jan
waclawek.janBest answer
Super User
Jan 2, 2021 7:01 PM UTC

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

ADaxa.1
ADaxa.1Author
Associate II
Jan 3, 2021 4:32 PM UTC

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.

waclawek.jan
Super User
Jan 3, 2021 4:55 PM UTC

> 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

ADaxa.1
ADaxa.1Author
Associate II
Jan 3, 2021 4:59 PM UTC

Ok thanks.

Piranha
Principal III
Jan 7, 2021 12:50 AM UTC

To actually learn microcontrollers, drop the useless "how to click Cube" tutorials and start from the beginning:

https://www.embeddedrelated.com/showarticle/453.php

P.S. And before that learn C programming on a computer.

ADaxa.1
ADaxa.1Author
Associate II
Jan 7, 2021 1:03 PM UTC

Thanks Piranha, I will make sure to do that.