Electronics > Projects, Designs, and Technical Stuff
Measuring time span very precise
<< < (3/16) > >>
magic:
Mega2560 is a big part so it probably has multiple counters with input capture on different pins, in which case no switching is even needed.


--- Quote from: Kleinstein on September 07, 2019, 07:11:55 am ---However there is a tricky point when counter overflow and capture event happen at the same time. This difficulty can be solved, but it needs some extra thoughts or searching for an existing solution.

--- End quote ---
Good observation.

If I'm reading the docs correctly, a lower numbered interrupt has higher priority, so input capture IRQ should be executed before overflow IRQ if they occur in the same clock cycle. Probably the simplest way to deal with it is to check if the captured timestamp is zero and then execute the overflow IRQ handler and clear the overflow IRQ flag before proceeding further.

edit
Or not. Better check the overflow flag.
Interrupts may be disabled when an overflow happens and then an event may be captured at count=1 and yet the capture IRQ will be executed before the overflow IRQ when interrupts are unblocked.
Kleinstein:
The interrupt priorities do not fully help to solve the possible race condition, as it applies to the interrupt waiting in the queue. There still is a possibility that the capture interrupt is executes before an overflow interrupt.

The way to check for the race condition is to check the interrupt flag for the overflow inside the ICP ISR.  The value of the captured time stamp tells if a possible pending overflow interrupt should have come before ( small capture value) or after the capture event ( large capture value).

A suitable program can be found here (text in German):
https://rn-wissen.de/wiki/index.php/Timer/Counter_(Avr)#Input_Capture
HendriXML:
Thanks for all the comprehensive suggestions! I'll investigate them.

Initially the idea was to use a prefab Arduino with a not so great oscillator. I could try to replace it with a good spec one. I've used them Mega2560 timers controlling a steppers using an acceleration table (instead of linear acceleration) which took loss off torque in higher RPM's into account. I also did a fun project to have 15 rbg leds be driven using color event tables which where created for supporting "thunderstruck by AC/DC".
If there's a possibility to reset their counter without software interference then that would be neat, I'll have to try to figure that out.
If interrupts are needed for starting and stopping then I would probably leave this road, not because it would not do the current job well. But mainly because off my the goals I've set to minimize the delays to less that a usec. I'd also like to use electronic components to build up experience with them, instead of the more know territory. (I haven't created a digital circuit yet)
I'm now short in time, but will respond more later on.
Thanks to you all!
Kleinstein:
There are likely interrupts used for the control at start and stop and if needed also the extension of the resolution. However the actual timing is done by the timer hardware. So the accuracy is as good as the clock with clock cycle resolution.
The trick is not to reset the timer, but already have the timer running and use time stamps at the start and end.
magic:
If your MCU has two timers with separate input capture pins, events A and B can be captured by separate timers.
In such case they may be spaced even 1 clock cycle apart, which is 1/16µs at 16MHz.
The software will then read timestamps captured by each counter and calculate the difference.

edit
Basically, we are trying to convince you that all the digital logic hardware you described in the original post is already in your MCU, just use it :)
Navigation
Message Index
Next page
Previous page
There was an error while thanking
Thanking...

Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod