TCNT and timing related question

Hello everyone,

In my code, I am using the TCNT3 for a timing of a function. Before the function gets called,
I make it equal to zero TCNT3 = 0; then at the end of the function, I read again the
timer_diff = TCNT3;

Now, I have the amount value of TCNT3 which is 28355, so the timer_diff = 28355;

My CPU runs at 20MHz and the Timer3 has a prescaler of 1024. In order to figure out the time it took the function to execute in seconds, I did the following:

1024 / 20.000000 = 0.0000512 seconds then I use this number to calculate

time = 51.2µsec * 28355 = 1451776 µsec and then I divide this number 1451776/1000 = 1451,776 milliseconds.

which is 1.46 seconds....

so if one function call takes 1.46 seconds then adding a counter which adds the value of the time each time function is called will yield the total time it took to call the function lets say 100 times then the total time will be 100 * 1.46 seconds = 145,1 seconds

but when I use my iPhone's stopwatch, or just calculate via calculator, and then compare it in serial

output I get a deviation of 1000 milliseconds, but should be 1450 considering above mentioned right....

any ideas about this hole concept are very much appreciated.

David

Which MCU/board you are writing about?

He's obviously using a classic AVR with one of the usual 16-bit timers (like Timer1 on almost all classic AVRs), probably a Mega, but possibly a 328PB or 1284P/similar.

That is a sound way to measure the time a function call takes, yes. That's how I usually do it (though I don't write functions that are that slow, what the hell are you doing?! When I am timing functions with a 16-bit timer, I'm prescaling by 1 or 2! Which is a good thing, because I'm usually doing it on a megaavr's type B timers, which don't have the lovely independent prescaler that the classic AVR 16-bit timers have).

It's not clear to me if you're reporting a difference of 1 second between measured time and what you calculate from that (if so, that's within expected error from a human pressing a stopwatch button, particularly on a phone touchscreen). Or that you're getting something radically different? Show the numbers involved.

it is the ATmega-1284p AVR. The TCNT3 is resting on Timer-3, which is a 16 bit one.

What bother me in my SERCOM output is the following:

Say I want to call the function X(), 100 times. I enter number and then, I get 100 invocations of that function x(). That is okay, as I posted above, each call x() takes a 1.46 seconds. So 100 calls will then be 100 x 1.46 = 145 seconds, and 145000 milliseconds. In fact in my output I see other value, which confuses me.

robo-Drive: enter number of cycles: (1-99999)
100
you entered : 00100
ADC-(adcValue) = 205    ADC-(lowValue) = 158    ADC-(highValue) = 162   time = 0000000010
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 192   time = 0000000020
ADC-(adcValue) = 128    ADC-(lowValue) = 158    ADC-(highValue) = 220   time = 0000000030
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 246   time = 0000000040
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 015   time = 0000000050
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 038   time = 0000000060
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 059   time = 0000000070
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 079   time = 0000000080

some prints removed to to forum editor restrictions. ....

ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 070   time = 0000000340
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 074   time = 0000000350
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 077   time = 0000000360
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 080   time = 0000000370
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 083   time = 0000000380
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 086   time = 0000000390
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 088   time = 0000000400
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 091   time = 0000000410
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 093   time = 0000000420
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 095   time = 0000000430
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 097   time = 0000000440
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 099   time = 0000000450
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 101   time = 0000000460
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 102   time = 0000000470
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 104   time = 0000000480
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 105   time = 0000000490
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 107   time = 0000000500
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 108   time = 0000000510
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 109   time = 0000000520
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 110   time = 0000000530
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 111   time = 0000000540
ADC-(adcValue) = 128    ADC-(lowValue) = 158    ADC-(highValue) = 112   time = 0000000550
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 113   time = 0000000560
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 114   time = 0000000570
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 115   time = 0000000580
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 116   time = 0000000590
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 116   time = 0000000600
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 117   time = 0000000610
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 118   time = 0000000620
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 118   time = 0000000630
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 119   time = 0000000640
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 119   time = 0000000650
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 120   time = 0000000660
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 120   time = 0000000670
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 121   time = 0000000680
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 121   time = 0000000690
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 121   time = 0000000700
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 122   time = 0000000710
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 122   time = 0000000720
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 122   time = 0000000730
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 123   time = 0000000740
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 123   time = 0000000750
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 123   time = 0000000760
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 123   time = 0000000770
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 124   time = 0000000780
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 124   time = 0000000790
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 124   time = 0000000800
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 124   time = 0000000810
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 124   time = 0000000820
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 125   time = 0000000830
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 125   time = 0000000840
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 125   time = 0000000850
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 125   time = 0000000860
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 125   time = 0000000870
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 125   time = 0000000880
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 125   time = 0000000890
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 125   time = 0000000900
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 125   time = 0000000910
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 125   time = 0000000918
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 126   time = 0000000928
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 126   time = 0000000938
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 126   time = 0000000946
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 126   time = 0000000954
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 126   time = 0000000962
ADC-(adcValue) = 126    ADC-(lowValue) = 158    ADC-(highValue) = 126   time = 0000000970
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 126   time = 0000000980
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 126   time = 0000000990
ADC-(adcValue) = 127    ADC-(lowValue) = 158    ADC-(highValue) = 126   time = 0000001000

Where is "time" coming from?

time is calculated from the TCNT3 difference.

TCNT3 = 0;
t_0  = 0;

function(){
   
  does its work;
}
 t_1 = TCNT3; saving the actual value of TCNT3 in uint16_t variable

 t_delta = t_1 -t_0; calculating the difference

 time = (t_delta * 51) / 1000; // we get here time in milliseconds, which is then printed in SERCOM output