I'm trying to add high-quality logging and I want to put an accurate time-stamp
on each line. Gordon has provided an example using setInterval to increment
an internal clock global variable, which can then be read by other code. But my
experience of other microcontrollers made me a bit concerned about how this
might work. For example, perhaps there's some other code which delays setinterval
by a few MS - not significant really but perhaps this might miss the rollover to the
next second.
So, I've just written a quick test program. It uses setInterval to setup a callback every
1 second, and then calls getTime() to see how close setInterval is being called to every second. (I couldn't get the upload code attachment to work, so it's pasted below)
The results are quite surprising - setInterval is calling my target function with an accuracy of less than 1/10 ms. To try to push it off track, I've setup another 10
setIntervals, all just wasting time. It doesn't affect it... even if I make them waste
a lot of time. (This is a bit of a puzzle, it almost suggests that successive setIntervals
will interrupt each other to make sure they all start close to target time??)
Of course, it's possible that some other activity - e.g. hardware interfacing might hit the accuracy if interrupts are off for a while.
var startTime=0;
var seconds=0;
var acc=0;
function checkTime()
{
var t=getTime();
seconds++;
var d=(t-startTime)-seconds;
acc = acc+d;
print(seconds,t-startTime,d, acc/seconds, (d-acc/seconds)*1000);
}
function wasteTime()
{
var a,b,c;
for(var i=0;i++;i<200)
a=b*c+b*a;
}
function testTime()
{
startTime=getTime();
for(var i=0;i<10;i++)
setInterval(wasteTime,1000);
setInterval(checkTime,1000);
}
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hi
I'm trying to add high-quality logging and I want to put an accurate time-stamp
on each line. Gordon has provided an example using setInterval to increment
an internal clock global variable, which can then be read by other code. But my
experience of other microcontrollers made me a bit concerned about how this
might work. For example, perhaps there's some other code which delays setinterval
by a few MS - not significant really but perhaps this might miss the rollover to the
next second.
So, I've just written a quick test program. It uses setInterval to setup a callback every
1 second, and then calls getTime() to see how close setInterval is being called to every second. (I couldn't get the upload code attachment to work, so it's pasted below)
The results are quite surprising - setInterval is calling my target function with an accuracy of less than 1/10 ms. To try to push it off track, I've setup another 10
setIntervals, all just wasting time. It doesn't affect it... even if I make them waste
a lot of time. (This is a bit of a puzzle, it almost suggests that successive setIntervals
will interrupt each other to make sure they all start close to target time??)
Of course, it's possible that some other activity - e.g. hardware interfacing might hit the accuracy if interrupts are off for a while.