I wanted to validate a 1ms timer, but I got a very strange result.
Briefly about the project:
I'm using mega328p avr in a nano shield, I read in value from analog pin, then save it into sd card.
Previously I was using polling; when the program has reached the proper code line, then the data has saved to the SD card. It caused me oversampling, so I wanted to make the writing slower, so I set a 1ms timer, like this:
ISR(TIMER2_COMPA_vect)
{
flag.dataWrite = true;
}
void setup()
{
/* Timer 2 128 prescale, CTC mode */
TCCR2A = (1U << WGM21);
TCCR2B = (1U << CS22) | (1U << CS20);/
TIMSK2 = (1U << OCIE2A);
OCR2A = 125; /* F_CPU / 128 --> 1 ms */
sei();
...
}
void loop()
{
if(flag.dataWrite)
{
counter += 1;
sprintf(buf, "%u\t%u", analogRead(sensorPin), counter);
myFile.println(buf);
flag.dataWrite = false;
}
....
}
I set up a stopwatch, and I measure 1 minute. In that case, I would expect, that in 1 minute the counter value on the SD card should be somewhere around 60.000. But, the counter only reaches around 54.000.
Then I rearranged the program, now I am not writing the data to the SD card every milisec, only at the end of the measurement. In that case I get the proper value, the counter is around 60.000.
My question is, why is the timer runs slower at the first time?