How attach interrupt timmer with exact time interval

Hello everybody,
I’m new at this forum and i need some help.

I’m triying the DueTimer library (https://github.com/ivanseidel/DueTimer) in Arduino Due (ATSAM3X8EAU DataSheet http://www.atmel.com/images/doc11057s.pdf) and I have some problems when I try to use Interrupt and Timer. I want to sample 6 differential analog signals with least 100 samples per second ( 1 sample every 0.01 second = 10 milliseconds = 10. 000 microseconds).
When I program the timer for call an interruption every 10 milliseconds and I try check the time interval between calls, I ascertain it is called every 6 milliseconds :astonished: and I don’t understand it.

This is my simple test code:

#include <DueTimer.h>
unsigned long timenow=0;
void myHandler(){
  Serial.println(micros()-timenow);
}
void setup(){
  Serial.begin(9600); 
  timenow=micros();
 Timer3.attachInterrupt(myHandler).start(10000); // <- microseconds Every 10 ms = 0.01 sec.	 }
void loop(){
}

And this is my result:

If I rest this values, I have 6 milliseconds (6000 microseconds) between interrupt calls:

t =
       16085
       22085
       28085
       34085
       40085
       46085
       52085
       58085
       64085
       70085
       76085
       82085
       88085
       94085

>> diff(t)

ans =
        6000
        6000
        6000
        6000
        6000
        6000
        6000
        6000
        6000
        6000
        6000
        6000
        6000

I don’t understand why, someone can help me to understand it and explain me why it occurs?

When it runs more time, the time between interrupt calls is 4 milliseconds:

tt =
     2356085
     2360085
     2364085
     2368085
     2372085
     2376085
     2380085
     2384085
     2388085

diff(tt)

ans =
        4000
        4000
        4000
        4000
        4000
        4000
        4000
        4000

Why time between interrupts calls isn’t the same? I need exact time interval between calls interrupts because the sample time interval must be the same.

Thanks for your help and sorry for possible mistakes when I write in English.

You shouldn’t be calling serial operations from inside an ISR, and you have
a low baud rate anyway.

Firstly get the ISR to store the time interval in a volatile variable,
so you can print it out from loop().

Secondly try 115200 baud since there’s no reason not to.

Hello,
Thank you, I do it and now works right.
When I set attachInterrupt every 10.000 microseconds, I get 9.999 microseconds between calls, I set 10.002 and now I get exactly 10.000 microseconds between calls.

#include <DueTimer.h>

int dura = 10; //seconds
volatile unsigned long timenow=0;
volatile unsigned long timeISR=0;

void myHandler(){
  timeISR=(micros()-timenow);
  timenow=micros();
}

void setup(){
  Serial.begin(115200); 
  timenow=micros();
  Timer3.attachInterrupt(myHandler).start(10002); // <- microseconds Every 10 ms = 0.01 sec.	  	 	  	 
}

void loop(){
  Serial.println(timeISR);
}

I have only one question too, if I need save AnalogRead values in to a file in a SdCard, I can do it from inside ISR?

Thanks!!!

SD card? SPI, so keep it out of the interrupt.
As mentioned, you should keep the interrupt to a minimum. Use maybe a Boolean variable toggled inside the interrupt and then write it in the loop with a if( previousVar !=var) kind of thing ? previousVar would be the last known, and saved state