Go Down

Topic: Inaccurate timing with micros() and interrupts with Due (Read 2025 times) previous topic - next topic


did anyone solve the problem http://forum.arduino.cc/index.php?topic=148161?  I think I have a similar problem.
I use the Due to generate triggered pulses on an output pin, either using a deayMicroseconds() in an interrupt, or by reading out micros() in the interrupt and changing output pins in the loop when micros()> storedTime.
While most pulses are accurate (observed with an oscilloscope), a fraction of pulses are actually much shorter (down to few microseconds) than the delay time.
The same code works fine with a Mega2560, same problems on another Due.
I used a function generator to trigger the interrupts, the pulses measured with an oscilloscope, I attach an image of the oscilloscope screen. A sample code based on blink is attached below.
If anyone has an idea, that would really help me.
Thanks a lot!

The code:

int led = 12;

void setup() {               
  pinMode(led, OUTPUT);   
attachInterrupt(2,blinking,RISING) ;
void loop() {

void blinking()
  digitalWrite(led, HIGH);   
  digitalWrite(led, LOW);   


It is well known that delay() and micros() related functions don't work properly within interrupt service routines. See also http://arduino.cc/en/Reference/AttachInterrupt .

Inside the attached function, delay() won't work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.

Try thinking about other ways to implement your code without using delayMicroseconds() in an ISR. What about simple pwm or timer counter interrupts? There are good libraries out there that can help you!


Roll your own ISR using a timer IRQ. Problem solved.


Are you testing with 1.5.2 or the latest nightly build?


Thanks a lot! It does work correctly with the new nightly built. I was using 1.5.2 previously.
But still I will try to use timer IQR, reading now how to do this.

Go Up