Go Down

Topic: Interrupt testing Mega 2560 (Read 727 times) previous topic - next topic


I have had some unexplained odd behaviour using multiple interuppts so I wrote the attached code to try and sort out what was going on. I did find that the response to inputs on interupts 3,4,5. was different and the system locked up somtimes when using theses inputs. However I have done some more work and I found that 3,4,5 seem to need good clean signals ie no fast bounce whereas 0,1,2 seem more tolerant. Maybe its just the board I have. Interseting to know if anyone else has had any similar results.

Code: [Select]
/* This is a test routine to explore the working of Interrupts on the Mega2560.
* The results are output via the Serial Monitor.
* Switches will have to be wired to the inputs declared below with either
* pull up or down resistors as needed.
* Lines 26 thro 31 the command FALLING will need to be changed in accordance
* with how you want to operate the interrupts.
* Malcolm Page malcolm@gbapj.com March 2013 */
int button0=2;
int button1=3;
int button2=21;
int button3=20;
int button4=19;
int button5=18;
//==========================SET UP========================
void setup()
  Serial.println("Hello Computer");
  attachInterrupt(0,Funct0,FALLING);   //int 0 on Pin 2
  attachInterrupt(1,Funct1,FALLING);   //int 1 on Pin 3
  attachInterrupt(2,Funct2,FALLING);   //int 2 on Pin 21
  attachInterrupt(3,Funct3,FALLING);   //int 3 on Pin 20
  attachInterrupt(4,Funct4,FALLING);   //int 4 on Pin 19
  attachInterrupt(5,Funct5,FALLING);   //int 5 on Pin 18
void loop()
  Serial.println("Start of loop");
  Serial.println("Start of 10 Secs delay");
  Serial.println("Start of 20 SEcs.delay");
  Serial.println("Start of 30 Secs. delay");
  Serial.println("End of loop");
//======================Subroutines (Functions)=========================
void Funct0()   // Interrupt 0 here
void Funct1()  // Interrupt 1 here
void Funct2()  // intereupt 2 here

void Funct3()   // Interrupt 3 here
void Funct4()  // Interrupt 4 here
void Funct5()  // interrupt 5 here

//=======================Subroutines / Functions =============
void showf0()
  Serial.println("INTERRUPT 0");
void showf1()
  Serial.println("INTERRUPT 1");
void showf2()
  Serial.println("INTERRUPT 2");
void showf3()
  Serial.println("INTERRUPT 3");
void showf4()
  Serial.println("INTERRUPT 4");
void showf5()
  Serial.println("INTERRUPT 5");


You can't use functions that require interrupts, like Serial.print() in interrupt service routines.
The art of getting good answers lies in asking good questions.


I am not you will see in the test prog the interupt calls another function to handle the output


It's completely irrelevant if you move the calls to a subroutine. As long as the interrupt handler is not finished you're still in interrupt context (so other interrupts are disabled). If you call methods like Serial.print() it may block forever because as soon as the buffer is filled it never gets cleared (this usually is done in an interrupt) because interrupts are disabled.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131