Pages: [1]   Go Down
Author Topic: Inaccurate timing with micros() and interrupts with Due  (Read 775 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
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!
Jonas

The code:

int led = 12;

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

void blinking()
{
  digitalWrite(led, HIGH);   
  delayMicroseconds(75);             
  digitalWrite(led, LOW);   
  delayMicroseconds(75);
}


* Foto.JPG (665.43 KB, 1632x1224 - viewed 29 times.)
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 .
Quote
Note

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!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

0
Offline Offline
God Member
*****
Karma: 24
Posts: 587
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you testing with 1.5.2 or the latest nightly build?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: