Go Down

Topic: Arduino Due External Interrupts (Read 3217 times) previous topic - next topic

Splinky

Hi,

I'm trying to use external interrupts on the Arduino Due but can't seem to get it working. Here is my code:

Code: [Select]

void setup(){
  Serial.begin(115200);
  pinMode(2, INPUT);
  attachInterrupt(2, state_change, CHANGE);
}

void loop(){
}

void state_change(){
  Serial.println("woot");
}


This should attach an interrupt to pin number 2 (according to http://www.arduino.cc/en/Reference/AttachInterrupt ). I have also tried using the old way of attaching to interrupt '0' to use pin 2 and I have also tried interrupt '4' to use pin 19 as well as tried attach interrupt '19' to use pin 19 but nothing is working. I realise it may be bad practice to put a serial command in an interrupt function but this is just to get the interrupt working.

Has anyone used external interrupts on the Due yet?

Thanks
Mark

retrolefty

Are you sure it's not working using pin 19? Recall that once inside a ISR all interrupts are disabled and serial transmit is now interrupt driven, so the command can't execute even if the interrupt is properly triggered?

Lefty

Splinky

Ok, so I changed the code to:

Code: [Select]

int state = 0;

void setup(){
  Serial.begin(115200);
  pinMode(2, INPUT);
  attachInterrupt(19, state_change, CHANGE);
}

void loop(){
  Serial.println(state);
  delay(1);
}

void state_change(){
  state = (state + 1);
}


So everytime I change the state of pin 19 (which I did by connecting it to another Arduino with a simple 'blink' type sketch, it should increment state. But it doesn't :(

Graynomad

What IDE version do you have? I think the interrupts will only be supported as of 1.5.2

Quote
Recall that once inside a ISR all interrupts are disabled

Not the case natively with the Due, interrupts are nested by default. What they will do with the core files to "fix" that I'm not sure.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Splinky

I am using 1.5. I'll give it a try as soon as I can get a hold of 1.5.2 (doesn't seem to be out yet).

Graynomad

I found the thread, it's "IDE 1.5.1 (released on Nov, 5)" that they will be implemented, or should that be were implemented :)

http://arduino.cc/forum/index.php/topic,128525.0.html

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Splinky

Ah, thanks Graynomad! I will try it out. I apologise for creating this whole new thread for it, I did not come across that thread in my initial search for my answer.

Splinky

Yay, it works on 1.5.2r2 IDE! Thanks again! woop woop

retrolefty

#8
Nov 15, 2012, 04:51 pm Last Edit: Nov 15, 2012, 04:52 pm by retrolefty Reason: 1
For future reference, when using global variables shared by both the ISR and main loop you should define the variable as volatile. In your example it should be:

Code: [Select]
volatile int state = 0;

http://arduino.cc/en/Reference/Volatile

Lefty

Go Up