Created Mon, 22 Aug 2011 17:51:18 +0000 by Fixe
Mon, 22 Aug 2011 17:51:18 +0000
Any simple example code to show that "attachInterrupt" works?. For example, for external interrupt?
Thank you
Mon, 22 Aug 2011 17:55:49 +0000
For example, my test external interrupt (pin2), program:
void setup() { Serial.begin(9600); pinMode(12,OUTPUT); attachInterrupt(0, encendido, LOW);
}
void loop() {
Serial.println(digitalRead(2)); digitalWrite(12,HIGH); }
void encendido() { digitalWrite(12,LOW); }
It doesn't work, using mpide0022 20110821-test
Thank you
Mon, 22 Aug 2011 19:26:29 +0000
The PIC32 microcontrollers only support rising and falling edge triggering on external interrupts, so only RISING and FALLING are supported by attachInterrupt. When you pass in LOW, the attachInterrupt function just returns without doing anything. The Arduino system doesn't provide any way to return an error in this case.
Gene Apperson Digilent
Mon, 22 Aug 2011 19:32:28 +0000
Here is the sketch I used to test attachInterrupt.
/************************************************************************ ** ExtIntTest - Sketch to Test External Interrupts
** This sketch tests the attachInterrupt and detachInterrupt functions. ** It assumes that pin 3 has been looped back to the external interrupt ** pin to be tested. It generates edges on pin 3 to trigger interrupts. ** The interrupt service routine then blinks the LED to show that it ** has been entered. ** ** Uno32 External Interrupt pins: ** INT0 = 38, INT1 = 2, INT2 = 7, INT3 = 8, INT4 = 35 ** ** Max32 External Interrupt pins: ** INT0 = 3, INT1 = 2, INT2 = 7, INT3 = 21, INT4 = 20 **
** History: ** ** 08/08/2011(GeneA): created ** ************************************************************************/
/* ------------------------------------------------------------ / / Local Symbol Definitions / / ------------------------------------------------------------ */
#if defined(BOARD_UNO) #define pinINT0 38 #define pinINT1 2 #define pinINT2 7 #define pinINT3 8 #define pinINT4 35 #define pinSrc 3 // used as external interrupt source
#elif defined(BOARD_MEGA) #define pinINT0 3 #define pinINT1 2 #define pinINT2 7 #define pinINT3 21 #define pinINT4 20 #define pinSrc 4 // external interrupt source
#else #error "No supported board specified. #endif
#define INT0 0 #define INT1 1 #define INT2 2 #define INT3 3 #define INT4 4
/* Since the Max32 doesn't have the second LED on pin 43, ** this test assumes that an external LED has been connected ** to pin 43 on that board. */ #define pinLED1 13 #define pinLED2 43
#define intTest INT1
/* ------------------------------------------------------------ / / Local Variables / / ------------------------------------------------------------ */
volatile int intStat;
/* ------------------------------------------------------------ / / Forward Declarations / / ------------------------------------------------------------ */
void IsrTest();
/* ------------------------------------------------------------ / / Procedure Definitions / / ------------------------------------------------------------ / /** setup ** ** Parameters: ** none ** ** Return Value: ** none ** ** Errors: ** none ** ** Description: ** Initialization function called at the beginning of execution. */
void setup() {
/* Use the LEDs to indicate activity */ pinMode(pinLED1, OUTPUT); // indicates foreground task activity pinMode(pinLED2, OUTPUT); // indicates ISR activity digitalWrite(pinLED1, LOW); digitalWrite(pinLED2, LOW);
/* Make all of the external interrupt pins be inputs. */ pinMode(pinINT0, INPUT); pinMode(pinINT1, INPUT); pinMode(pinINT2, INPUT); pinMode(pinINT3, INPUT); pinMode(pinINT4, INPUT);
/* Make the stimulus pin be an output. */ pinMode(pinSrc, OUTPUT); digitalWrite(pinSrc, LOW);
}
/* ------------------------------------------------------------ / /** loop ** ** Parameters: ** none ** ** Return Value: ** none ** ** Errors: ** none ** ** Description: ** Application event loop */
void loop() { int itrTest;
/* Install the handler for the interrupt being tested. ** Test rising edge triggering first. */ intStat = 0; attachInterrupt(intTest, IsrTest, RISING);
/* For rising edge triggered interrupts, both LEDs
** should go on and off at the same time.
*/
for (itrTest = 0; itrTest < 10; itrTest++) {
digitalWrite(pinLED1, HIGH);
digitalWrite(pinSrc, HIGH);
delay(500);
digitalWrite(pinSrc, LOW);
digitalWrite(pinLED1, LOW);
delay(500);
}
/* Uninstall the handler and test to make sure that
** we aren't getting interrupts any more.
*/
detachInterrupt(intTest);
digitalWrite(pinLED2, LOW);
/* Only one LED should be blinking here.
*/
for (itrTest = 0; itrTest < 5; itrTest++) {
digitalWrite(pinLED1, HIGH);
digitalWrite(pinSrc, HIGH);
delay(500);
digitalWrite(pinSrc, LOW);
digitalWrite(pinLED1, LOW);
delay(500);
}
intStat = 0; digitalWrite(pinLED1, HIGH); digitalWrite(pinSrc, HIGH); delay(1000);
attachInterrupt(intTest, IsrTest, FALLING);
/* For rising edge triggered interrupts, both LEDs
** should go on and off at the same time.
*/
for (itrTest = 0; itrTest < 10; itrTest++) {
digitalWrite(pinLED1, LOW);
digitalWrite(pinSrc, LOW);
delay(500);
digitalWrite(pinSrc, HIGH);
digitalWrite(pinLED1, HIGH);
delay(500);
}
/* Uninstall the handler and test to make sure that
** we aren't getting interrupts any more.
*/
detachInterrupt(intTest);
digitalWrite(pinLED2, LOW);
/* Only one LED should be blinking here.
*/
for (itrTest = 0; itrTest < 5; itrTest++) {
digitalWrite(pinLED1, LOW);
digitalWrite(pinSrc, LOW);
delay(500);
digitalWrite(pinSrc, HIGH);
digitalWrite(pinLED1, HIGH);
delay(500);
}
/* Show that this iteration of the test has completed */ for (itrTest = 0; itrTest < 5; itrTest++) { digitalWrite(pinLED1, HIGH); delay(100); digitalWrite(pinLED1, LOW); delay(100); }
delay(1000);
}
/* ------------------------------------------------------------ / /** IsrTest ** ** Parameters: ** none ** ** Return Value: ** none ** ** Errors: ** none ** ** Description: ** Application event loop */
void IsrTest() {
intStat = 1 - intStat; if (intStat != 0) { digitalWrite(pinLED2, HIGH); } else { digitalWrite(pinLED2, LOW); }
}
/* ------------------------------------------------------------ */
/***********************************************************************/
Mon, 22 Aug 2011 19:38:25 +0000
Oops... I just noticed that the version of the test I posted doesn't have the correct comment for falling edge interrupts. When doing the falling edge part of the test, the LEDs should blink out of phase with each other.
Gene
Mon, 22 Aug 2011 20:10:47 +0000
Ok, thank you very much!
Thu, 01 Sep 2011 17:11:27 +0000
Dear Gene and Digilent engineer, I'm Chaiwat from INEX; Digilent's Thailand distributor.
My customer complain about Attachinterrupt function not work.
I download latest version (20110822) and try. It also does not work.
They (my local customer) would like to return the board with this issue. They complain abotu Interrupt issue. They told..THIS IS BASIC FUNCTION WHY NOT WORK ? !!! Digilent promote chipKIT is 32-bit Arduino compatible borad. But why does the interruopt function not work ?
Need your help very urgent.
I am losting customer and get the many return board from customer.
This is serious case.
Please advise.
Thu, 01 Sep 2011 18:06:22 +0000
It works on my system and passes my tests.
Can you provide more specific information about how you are trying to use it and what isn't working?
Gene Apperson Digilent
Thu, 01 Sep 2011 18:47:06 +0000
Please advise hardware connection from your interrupt example.
I connect pin 4 (Max32) to pin 21 (INT3) and coonnect LED2 at pin 43 with active low connection.
I upload the code and see operation both condition.
Connect pin 4 to pin 21 1.1 LED1 on pin 13 blink fast and next to blink slow 9 times after that LED2 on pin 43 ON (low) 1.2 LED2 on until LED1 blink fast after that LED2 off 1.3 LED1 blink slow again. wait a moment LED2 on 1.4 Back step 1.2
Remove pin 4 from pin 21 The operation also same. No anyting different
When External interrupt is occur ?
Thu, 01 Sep 2011 18:58:49 +0000
Hi Gene, I think now problem fix. It's my fault. I connect wrong hardware. Now It's ok.
Apologize about my mistake.
Thu, 01 Sep 2011 19:52:07 +0000
No problem. I'm glad it's working. Let me know if you have any other questions or issues.
Gene Apperson Digilent
Fri, 25 Mar 2016 09:58:25 +0000
Dear all,
I am also facing problem in attachInterrupt () function I am using this to read Incremental Rotary Encoder, my program is working properly on Arduino board but when I use same program its not working properly.
If I turn my encoder to clock wise its showing counter increasing, but when I turn in anti clock wise its also showing same increasing count in place of decreasing count, plz do help
hare is my code. /-------------------------------------------------------------/ unsigned long pulse = 500000;
void setup() { Serial.begin (9600); attachInterrupt(0, ai0, RISING); attachInterrupt(1, ai1, RISING); delay(1500); Serial.println(5000000); } void loop() { Serial.println(pulse); delay(150);
} void ai0() {
if (digitalRead(3) == LOW) { pulse++; } else { pulse--; } } void ai1() {
if (digitalRead(2) == LOW) { pulse++; } else { pulse--; } } /---------------------------------------------------/
Fri, 25 Mar 2016 10:43:22 +0000
Which board are you using it with?