Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug in micros() when called inside ISR? (Due) #1301

Closed
cmaglie opened this issue Mar 2, 2013 · 5 comments
Closed

Possible bug in micros() when called inside ISR? (Due) #1301

cmaglie opened this issue Mar 2, 2013 · 5 comments
Labels
Component: Core Related to the code for the standard Arduino API Type: Bug

Comments

@cmaglie
Copy link
Member

cmaglie commented Mar 2, 2013

Forum topic:
http://arduino.cc/forum/index.php/topic,148161.msg1136944.html#msg1136944

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...
@cmaglie
Copy link
Member Author

cmaglie commented May 3, 2013

Fixed in #1388

@cmaglie cmaglie closed this as completed May 3, 2013
@Matioupi
Copy link

I'm not sure the issue is solved on version 1.5.4r2 or IDE, cf the issue i'm facing described here :

http://forums.adafruit.com/viewtopic.php?f=19&t=44082&p=220114#p220114

actually, I think there is an issue on successive millis() calls inside ISR if the handler execution time is longer than 1 ms

@NicoHood
Copy link
Contributor

NicoHood commented Aug 1, 2014

I still have this bug in IDE 1.5.7 and all other IDEs. I am using Arduino Mega and if i print (only for debug of course!) the micros in the interrupt they differ. the 2nd value is before the first, the 3rd is correct the 4th not and so on. very random.

@cmaglie
Copy link
Member Author

cmaglie commented Aug 1, 2014

@NicoHood
please open another issue with and post a sample sketch to reproduce the problem.

Thanks!

@NicoHood
Copy link
Contributor

NicoHood commented Aug 1, 2014

#2219

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core Related to the code for the standard Arduino API Type: Bug
Projects
None yet
Development

No branches or pull requests

3 participants