2013-05-23 08:11 AM
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 !Jean2013-05-23 08:29 AM
http://www.ganssle.com/debouncing.htm
http://www.ganssle.com/debouncing-pt2.htm2013-05-23 09:55 AM
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.2013-05-23 03:26 PM
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 subject2013-05-24 02:55 AM
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++;
}
2016-04-15 08:25 AM
2016-04-15 11:44 AM
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 thehttp://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 inhttp://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-