westfw:
You really, really, can't interrupt a 16MHz processor every microsecond...
Here's the code produced for that ISR:
00000480 <__vector_11>:
480: 1f 92 push r1
482: 0f 92 push r0
484: 0f b6 in r0, 0x3f ; 63
486: 0f 92 push r0
488: 11 24 eor r1, r1
48a: 8f 93 push r24
48c: 9f 93 push r25
48e: af 93 push r26
490: bf 93 push r27
492: 10 92 85 00 sts 0x0085, r1 ; 0x800085 <DATA_REGION_ORIGIN+0x25>
496: 10 92 84 00 sts 0x0084, r1 ; 0x800084 <DATA_REGION_ORIGIN+0x24>
49a: 80 91 41 01 lds r24, 0x0141 ; 0x800141
49e: 90 91 42 01 lds r25, 0x0142 ; 0x800142 <microseconds+0x1>
4a2: a0 91 43 01 lds r26, 0x0143 ; 0x800143 <microseconds+0x2>
4a6: b0 91 44 01 lds r27, 0x0144 ; 0x800144 <microseconds+0x3>
4aa: 01 96 adiw r24, 0x01 ; 1
4ac: a1 1d adc r26, r1
4ae: b1 1d adc r27, r1
4b0: 80 93 41 01 sts 0x0141, r24 ; 0x800141
4b4: 90 93 42 01 sts 0x0142, r25 ; 0x800142 <microseconds+0x1>
4b8: a0 93 43 01 sts 0x0143, r26 ; 0x800143 <microseconds+0x2>
4bc: b0 93 44 01 sts 0x0144, r27 ; 0x800144 <microseconds+0x3>
4c0: bf 91 pop r27
4c2: af 91 pop r26
4c4: 9f 91 pop r25
4c6: 8f 91 pop r24
4c8: 0f 90 pop r0
4ca: 0f be out 0x3f, r0 ; 63
4cc: 0f 90 pop r0
4ce: 1f 90 pop r1
4d0: 18 95 reti
I count "about" 60 cycles; almost 4 microseconds.
I'd go so far as to say that you shouldn't count on being able to interrupt ANY processor at microsecond intervals; as the chips get faster, the overhead of an interrupt tends to get higher...
Thanks, it makes a lot of sense. It's not an assignment, I want a source of time to measure one way delays of UDP packages on a wireless network.
Anyway, by changing the prescaler to 64 and compare register to 4, thus getting a tick every 20 microseconds, I was able to get closer and, for a 10 seconds delay, I've got 20*ticks = 9.998.800 seconds.
However, I've tested @johnwasser code and it seems to work. For a 10 seconds delay, I've got 160.038.912 ticks of 62,5 nanoseconds (every time, INCREDIBLY precise). I'm still studying his code because some parts aren't yet clear to me.
When I said precise, I meant it. I didn't know Arduino boards could be so precise with that level of granularity. I copied this from the console, take a look:
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 11
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 11
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
Time passed in seconds (RTC DS3231): 10
Time passed in microseconds (Counter): 9998336
This was how I outputted it:
Serial.println((unsigned long) (FastTimer()*62.5/1000));