Getting Arduino Clock Cycles between Triggers

Is it possible to get the exact difference of Arduino clock cycles between two rising edge input trigger intervals to a resolution of 1 clock cycle?

For example, if the Arduino clock was ideal and I had a nearly ideal 1 second period pulse, could Arduino capture if there was one more or one less clock cycle between input triggers than a previous interval?

I don't need a total number of clock cycles if a counter rolls over, just want to know the actual difference in clock cycles between intervals, and there would not be a difference of more than a few clock cycles, certainly never more than 255 clock cycles.


You could almost do it easily with dual timer input capture registers, on any processor that has two of them. The Mega does. But if the pulses are a second long, the 16 bit timer capture registers would overflow.

Why do you need to measure second long pulses to 160ns accuracy?

It just seemed like an easier mousetrap.

clock arduino with 10 mhz ocxo and count the difference in its clock cycles between a stable 1pps GPS reference, do some calculations, averaging to take care of jitter, then send a slow adjustment to a dac to discipline the OCXO for precise timekeeping.

There are similar reasons I'd like to count clock cycles between events for timing, but I don't know how to do that. The 328p cannot?

The 328p cannot?

Yes, it can. You will be using timer 1.

These two libraries are a good place to start...

"Input capture" is a good key phrase...

Thanks for the links. I recall seeing frequency counter discussions on Gammon’s site.

The first google result has input capture code on git hub. I spent time trying to understand it, but cannot.

It runs and when I insert a signal, I get this result:

0:4096437 0
1:15583074 0
2:409407 0
3:15583071 0
4:409408 0
5:15583070 0
6:409407 0
7:15583070 0
8:409407 0
9:15583072 0
10:409407 0

And a faster signal:

0:237 60043
1:799 60092
2:800 60141
3:799 60190
4:801 60239
5:798 60288
6:801 60337
7:799 60387
8:800 60435
9:799 60485
10:801 60535

And a 10 MHz sin did not seem to register.

I have not yet figured out what the results mean, and they seem to fluctuate more than I expect, but that is using the arduino clock. The input signals should be stable within a few hundred picoseconds.