Error with attachInterrupt

Hi guys!

I am working on a project with Arduino Due and I have to use interrupts. The problem is that each time code runs there comes a stray interrupt on one of the pins. Actually the code was very long and it took me several hours to find this bug. So i wrote a separate piece of code to test it. Here it is:

void ISRAF(){
  Serial.println('b');  
}
void ISRAB(){
  Serial.println('a');
}

void setup(){

 
  pinMode(7, INPUT);
  pinMode(6, INPUT);
  REG_PIOC_WPMR=0x50494F00;
  REG_PIOC_DIFSR=0x01800000;    //ebounce filter is enabled
  REG_PIOC_SCDR=1600;           //set debounce time
  REG_PIOC_PUER=0x07800000;     //pins 6,7 are pulled up
  REG_PIOC_IFER=0x01800000;
  Serial.begin(115200);
  attachInterrupt(7, ISRAF, FALLING);
  attachInterrupt(6, ISRAB, FALLING);

  Serial.println("started");
  
}

void loop(){

}

Now the problem here is that each time the cpu resets there is a stray interrupt on pin 7. I have tried several options: changing the pin, using delays, disabling the interrupts in setup and enabling them after a while.... with no avail.

In the end I had to use a flag to ignore the first stray interrupt. Still I want to know what is the problem with my code. Note that i have to set the debouncing filter for the pins (nescessity of my project).

Please Help! Thanks in advance!!

Serial.println('b');

search the forum for "Serial Print in ISR"

BulldogLowell:

Serial.println('b');

search the forum for "Serial Print in ISR"

I have removed the Serial.print but the problem still persists. Could it be a problem with my board?? I have used it pretty roughly.

bool a=0,b=0;

void ISRAF(){
  b=1; 
}
void ISRAB(){
  a=1;
}

void setup(){

 
  pinMode(7, INPUT);
  pinMode(6, INPUT);
  REG_PIOC_WPMR=0x50494F00;
  REG_PIOC_DIFSR=0x01800000;    //ebounce filter is enabled
  REG_PIOC_SCDR=1600;           //set debounce time
  REG_PIOC_PUER=0x07800000;     //pins 6,7 are pulled up
  REG_PIOC_IFER=0x01800000;
  Serial.begin(115200);
  attachInterrupt(7, ISRAF, FALLING);
  attachInterrupt(6, ISRAB, FALLING);

  Serial.println("started");
  
}

void loop(){
   if(a){
     Serial.println("a");
     a=0;
   }
   if(b){
     Serial.println("b");
     b=0;
   }
}

Variables used in interrupts must be declared "volatile" (eg. "volatile bool a, b;").

techtweektweek:
I have removed the Serial.print but the problem still persists. Could it be a problem with my board?? I have used it pretty roughly.

Please describe in as much detail as possible how the problem manifests itself with the code in Reply #3 - what actually happens and what should happen?

For those of us unfamiliar with a DUE, what do these lines do? - in a little more detail than your comments

  REG_PIOC_WPMR=0x50494F00;
  REG_PIOC_DIFSR=0x01800000;    //ebounce filter is enabled
  REG_PIOC_SCDR=1600;           //set debounce time
  REG_PIOC_PUER=0x07800000;     //pins 6,7 are pulled up
  REG_PIOC_IFER=0x01800000;

One of the joys (?) of using interrupts is that you never know if they know more than you do. :slight_smile:

...R