Go Down

Topic: DUE problem with micros() inside interrupts (Read 3 times) previous topic - next topic

nicolas88

Hi,
I think I have the same problem..
I have both Arduino uno and arduino due.
I had used the function micros() inside an interrupt, and sometimes it seem not work fine in arduino due.
I would like to acquire the frequency (or time beetwen rising edge, i.e. Period) of an input signal on pin 28  (in Arduino due). The signal is a square wave 0/3.3V with PWM 50%.
For example I set the frequency to 2000Hz ( period of 500 micro second ) with a function generation.
this is the simple code:

volatile unsigned long time, Time1, Time2, Period =0;
volatile int i=0;
int Signal=28;

void setup() {
  Serial.begin(9600);
  pinMode(Signal,INPUT); 
  attachInterrupt(Signal,InterruptSegnaleGiriT,FALLING);  //Interrupt on Falling Edge
  }

void loop() {
  if(Period<490 || Period >510){
      Serial.println(Period);
      Serial.println(Time1);
      Serial.println(Time2);
      Serial.println("");
  }
void InterruptSegnaleGiriT(){
      time=micros();
          if(i==0){ 
            Time1=time;
            i=1;
            return;
          }
          if(i==1){
            Time2=time;
            Period=(Time2-Time1);
            i=0;
            return; 
      }
}

If the function micros work fine, this code does not print anything.
The problem is that sometimes the Period print is 1500 micro.
While sometimes Period print is a number near to the limit of an unsigned long,
maybe the Time1 is greater than Time2.

I Have try to put the code on arduino uno, the code work fine!!!
Maybe is a bug on Arduino due micros...
sorry for the english..
thank
       


cmaglie

C.

mcgavinz26

I just tested the code from my first post and noticed the same problem on a brand new DUE, so I don't think that my hardware is damaged.  Here are other things I have tested:

Using different pins - problem persists
Using RISING instead of FALLING - problem persists
Using CHANGE instead of FALLING - similar problem
Using higher and lower input frequencies - problem persists

I changed the code to only have a counter in the interrupt and it worked correctly, but that doesn't help me.  Here is the code:
Code: [Select]
volatile int counter = 0;

void setup() {
  Serial.begin(115200);
  attachInterrupt(2, FallEdge, FALLING);
}

void loop() {
  interrupts();
  if (counter >= 1)  {
    Serial.println(micros());
    counter = 0;
  }
}

void FallEdge(){
  counter++;
}


Then I moved the print statement out of the interrupt, but kept the micros() in the interrupt.  It still had the same problem.  Here is the code:
Code: [Select]
volatile unsigned long counter = 0;

void setup() {
  Serial.begin(115200);
  attachInterrupt(2, FallEdge, FALLING);
}

void loop() {
  interrupts();
  if (counter >= 1)  {
    Serial.println(counter);
    counter = 0;
  }
}

void FallEdge(){
  counter = micros();
}

OptimusPrime

Hi!
Same here...
I am reading the time between two interrupts - and print the measured values. Also figuered out that printing is not the problem here - proved this by changing ports and measuring these with an oszi. I assume that the value of micros() differs exact 1000micS plus or minus the values they are meant to be - perhaps interesting for debugging.
Let us now when sby finds out more!
Thanks!

Go Up