Pages: [1]   Go Down
Author Topic: Interrupt testing Mega 2560  (Read 401 times)
0 Members and 1 Guest are viewing this topic.
KENT UK
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/* 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 */
//====================DECLARATIONS=================
int button0=2;
int button1=3;
int button2=21;
int button3=20;
int button4=19;
int button5=18;
//==========================SET UP========================
void setup()
{
  Serial.begin(9600);
  Serial.println("Hello Computer");
  pinMode(button0,INPUT);
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  pinMode(button3,INPUT);
  pinMode(button4,INPUT);
  pinMode(button5,INPUT);
  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
}
//=============================MAIN==============================
void loop()
{
  Serial.println("Start of loop");
  Serial.println("Start of 10 Secs delay");
  delay(10000);
  Serial.println("Start of 20 SEcs.delay");
  delay(20000);
  Serial.println("Start of 30 Secs. delay");
  delay(30000);
  Serial.println("End of loop");
}
//======================Subroutines (Functions)=========================
void Funct0()   // Interrupt 0 here
{     
  showf0();
}
//-------------------------------------------------
void Funct1()  // Interrupt 1 here
{   
  showf1();
}
//-------------------------------------------------
void Funct2()  // intereupt 2 here
{   
  showf2();

//-------------------------------------------------
void Funct3()   // Interrupt 3 here
{     
  showf3();
}
//-------------------------------------------------
void Funct4()  // Interrupt 4 here
{   
  showf4();
}
//-------------------------------------------------
void Funct5()  // interrupt 5 here
{   
  showf5();

//=======================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");
}
//---------------------END----------------------------


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can't use functions that require interrupts, like Serial.print() in interrupt service routines.
Logged

KENT UK
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: