cancel
Showing results for 
Search instead for 
Did you mean: 

Measuring the time duration between two pulses

jean_prieur
Associate III
Posted on May 23, 2013 at 17:11

Hello,

I want to do something ''simple'': measuring the time duration between two button presses. In my application, the user can press a button repetitively and the STM32F4 will calculate the time between two pulses to determinate the average frequency. The range of time will go from 200ms to 2s.

I think I should create an interrupt on a GPIO input, and count with a timer the time passed between two interruptions ? Is it the best way to achieve that ? Or do you have some code for me 🙂

Thanks !

Jean
6 REPLIES 6
Posted on May 23, 2013 at 18:55

For fast signals you have the Input Capture mode in the TIM units.

For the kind of signal here, you'd be better off just using SysTick to generate a 1 ms software counter, and then time stamp event(s) as you service interrupts or poll the keys.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
emalund
Associate III
Posted on May 24, 2013 at 00:26

I would not overcomplicate this.

no interrupt for key pressed, you do not need it

read the blasted keys, say, every 10ms, when you have two identical reads the read is valid.

The only case where the above is not valid is if you are battery powered and need to go to sleep between operations.

the 10ms above should be increased if your keys bounce longer than that, but I have found 10ms to work most times

Erik

PS I agree with the 'use matrix' suggestion in the other thread on this subject
jean_prieur
Associate III
Posted on May 24, 2013 at 11:55

Thanks for the tip, with SysTick it was very easy to achieve my function !

This is my code for those who are interrested, for the moment I don't use interuption, I just read the key in continuous...

uint32_t Time = 0, average_time = 600, tempo = 120;
int
main(
void
)
{
// Configure SysTick to interrupt each 1ms (SystemCoreClock=168MHZ)
SysTick_Config(SystemCoreClock/1000);
while
(1)
{
// If the key is pressed
if
(GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_0) == 1)
{ 
// If the Time elapsed since the last press is >3seconds, 
// the time and the average time need to be reset
if
(Time > 3000)
{
Time = 0;
average_time = 120;
tempo = 0;
}
// If the Time elapsed since the last press is >200ms
// we can calculate the average time between two button presses
else
if
(Time > 200)
{
average_time = (average_time + Time)/2;
Time=0;
tempo = 60000/average_time;
}
}
}
return
(1);
}
// SysTick interrupt each 1ms
void
SysTick_Handler(
void
)
{
// if the variable Time (in ms, useful to save the time elapsed between two presses)
// is <4seconds, increment Time
if
(Time < 4000) Time++;
}

bendkhilarwa
Associate III
Posted on April 15, 2016 at 17:25

Hello jean, I wanna ask you , if you have solved your problem, about measuring duration between two pulses. can you help me please I have the some problem now. thanks

Walid FTITI_O
Senior II
Posted on April 15, 2016 at 20:44

Hi ben_dkhil.arwa,

You can do that by two method :

-  You can create an interrupt on a GPIO input, and count with a timer/Systick timer the time passed between two interruptions . You can start by GPIO_EXTI example in the

http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/LN1897/PF259243

library and use Systick ( try to get inspired from HAL_Delay function in stm32F4xx_hal.c ) to calculate timer elapsed between two pulses (some example use this delay)

-You can proceed by input capture on that I/O , refer to TIM_inputCapture in

http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/LN1897/PF259243

for example at this path stm32cubef4_1.11\STM32Cube_FW_F4_V1.11.0\Projects\STM324xG_EVAL\Examples\TIM\TIM_InputCapture

-Hannibal-