Timer issue when writing to SD card

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?

Snippets of code are rarely useful. You need to post your entire sketch. Writing to the SD card takes some time. If it takes more than a millisecond, then your code will miss an update cycle.

If you want to test this, make your flag.dataWrite a byte and increment it in your ISR and then check the value in loop(). If it is 1, then you are good. If it is 2 or greater, you've missed something.

I did not want to past to entire code, because it is way to big, I wanted to point out the timer relevant part.
My idea is identical to you, but without the 1 ms timer, the amount of data written to the SD card is a lot more. In addition, if I double the timer (from 1ms to 2ms by changing OCR2A to 250), the counter is still less then it should.
The suggestion of the counter is useful, thanks.

I tried the counter, looks like there is no dataloss.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.