Show Posts
Pages: [1]
1  Products / Arduino Due / Re: DUE problem with micros() inside interrupts on: February 28, 2013, 08:59:36 am
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
       

Pages: [1]