Author Topic: Is it possible to measure the time elapsed between two interrupts triggerings?  (Read 1831 times)

0 Members and 1 Guest are viewing this topic.

Offline XaviPachecoTopic starter

  • Regular Contributor
  • *
  • Posts: 243
  • Country: do
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.
 

Offline XaviPachecoTopic starter

  • Regular Contributor
  • *
  • Posts: 243
  • Country: do
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?
 

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
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.
 

Offline XaviPachecoTopic starter

  • Regular Contributor
  • *
  • Posts: 243
  • Country: do
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.
 

Online T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21674
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
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
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline XaviPachecoTopic starter

  • Regular Contributor
  • *
  • Posts: 243
  • Country: do
I have managed to connect the speed sensor to a timer channel, which has input capture mode.
 

Offline richardman

  • Frequent Contributor
  • **
  • Posts: 427
  • Country: us
You can always setup the SysTick Timer and look at its value.
// richard http://imagecraft.com/
JumpStart C++ for Cortex (compiler/IDE/debugger): the fastest easiest way to get productive on Cortex-M.
Smart.IO: phone App for embedded systems with no app or wireless coding
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3731
  • Country: lv
You can always setup the SysTick Timer and look at its value.

Don't. SysTick is too slow for "time elapsed between two interrupts" measurement.
« Last Edit: September 19, 2018, 05:26:52 am by ogden »
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Take a look at the Data Watchpoint and Trace Unit. The CYCCNT register can be used to measure time and runs at the system's core clock speed.
 
The following users thanked this post: hans

Offline emece67

  • Frequent Contributor
  • **
  • !
  • Posts: 614
  • Country: 00
.
« Last Edit: August 19, 2022, 02:03:28 pm by emece67 »
 

Offline PCB.Wiz

  • Super Contributor
  • ***
  • Posts: 1537
  • Country: au
I have managed to connect the speed sensor to a timer channel, which has input capture mode.

Good, that will give you the highest precision, and avoids interrupt delay jitter effects.
Adjust the precaler, and you should be good to go.
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3238
  • Country: gb
You can always setup the SysTick Timer and look at its value.

Don't. SysTick is too slow for "time elapsed between two interrupts" measurement.

A 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.
 
The following users thanked this post: newbrain


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf