Is it possible to measure the time elapsed between two interrupts triggerings? - Page 1
Forum section: electronics / general

author: XaviPacheco
author_rank: Regular Contributor
timestamp: September 19, 2018, 01:12:13 am
content: Hi,In my nucleo L432KC, I've set an external interrupt every falling edge to measure the speed using a reed switch, which gives a pulse per revolution. Every time the magnet passes the switch, a falling edge is produced. I've read that for low PPR (anything less than 60 PPR), we should use the Period Measurement Method. I haven't found a documentation to read about how to measure the time elapsed between every interrupt triggering. If I have that information, I can compute the speed in RPM. I would appreciate any suggestions.

author: XaviPacheco
author_rank: Regular Contributor
timestamp: September 19, 2018, 01:32:11 am
content: I've come up with incrementing a counter at each triggering and check it every second. But, how effective could it be at low and medium speeds?

author: Rerouter
author_rank: Super Contributor
timestamp: September 19, 2018, 01:54:19 am
content: I cannot pull up the datasheet. But your device should have atleast more than 1 timer peripheral. So rather than servicing it with an interrupt you can handle it in 2 ways.The first is to set up and feed in to the timers clock source pin. This way every switch is counted by hardware. When you want to read off the frequency you just read the timers value. This is direct counting.For lower speeds you can instead do reciprical counting with the same timer. Most micros have a timer capture pin. That or you piggyback off something like an analog comparitor peripheral that does. This way whenever your pulse comes in it captures the latest time of a change. With the timer jist freely counting up. Then you have the number of timer clock cycles between pulses.

author: XaviPacheco
author_rank: Regular Contributor
timestamp: September 19, 2018, 02:09:31 am
content: The problem was that I made a mistake in my PCB and didn't connect the speed sensor to a timer channel. But now that you mention time capture pin, I will see if my non-used pins are timers channels. That way I can just place a jumper in between.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: September 19, 2018, 03:04:27 am
content: Usually you'd have an internal timer running free, and sample its count register in the interrupt. Subtract from the value read on the last interrupt, and adjust for overflow (since the timer's count is modulo MAX_COUNT).The timer reset interrupt might increment a global counter, to make this information available.Warning: be careful with interrupts on switch contacts. Add some kind of debounce function. Whether analog (RC filter, hysteresis) or digital (e.g., interrupt disables itself and sets a timer to re-enable it, thus making a holdoff action), it should prevent the pin from triggering too many interrupts.It is very much a thing, that poorly designed systems have locked up from ambient noise triggering pin-change interrupts. Just a basic precaution.Tim

author: XaviPacheco
author_rank: Regular Contributor
timestamp: September 19, 2018, 03:13:11 am
content: I have managed to connect the speed sensor to a timer channel, which has input capture mode.

author: richardman
author_rank: Frequent Contributor
timestamp: September 19, 2018, 04:43:22 am
content: You can always setup the SysTick Timer and look at its value.

author: ogden
author_rank: Super Contributor
timestamp: September 19, 2018, 04:55:41 am
content: Quote from: richardman on September 19, 2018, 04:43:22 amDon't. SysTick is too slow for "time elapsed between two interrupts" measurement.

author: andyturk
author_rank: Frequent Contributor
timestamp: September 19, 2018, 05:07:23 am
content: Take a look at theData Watchpoint and Trace Unit. The CYCCNT register can be used to measure time and runs at the system's core clock speed.

author: emece67
author_rank: Frequent Contributor
timestamp: September 19, 2018, 11:17:51 am
content: .

author: PCB.Wiz
author_rank: Super Contributor
timestamp: September 19, 2018, 08:59:11 pm
content: Quote from: XaviPacheco on September 19, 2018, 03:13:11 amGood, that will give you the highest precision, and avoids interrupt delay jitter effects.Adjust the precaler, and you should be good to go.

author: mikerj
author_rank: Super Contributor
timestamp: September 23, 2018, 11:15:36 am
content: Quote from: ogden on September 19, 2018, 04:55:41 amA counter incremented by the SysTick interrupt may well be tool slow, but the SysTick counter itself runs from the main clock (or a divided version of it) and you can read the current value.