problems with attachInterrupt

Fixe
Posts: 5
Joined: Thu Aug 18, 2011 6:19 pm

problems with attachInterrupt

Post by Fixe » Mon Aug 22, 2011 6:51 pm

Any simple example code to show that "attachInterrupt" works?. For example, for external interrupt?

Thank you

Fixe
Posts: 5
Joined: Thu Aug 18, 2011 6:19 pm

Re: problems with attachInterrupt

Post by Fixe » Mon Aug 22, 2011 6:55 pm

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

GeneApperson
Posts: 239
Joined: Wed Jun 01, 2011 9:53 pm
Location: Pullman WA
Contact:

Re: problems with attachInterrupt

Post by GeneApperson » Mon Aug 22, 2011 8:26 pm

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

GeneApperson
Posts: 239
Joined: Wed Jun 01, 2011 9:53 pm
Location: Pullman WA
Contact:

Re: problems with attachInterrupt

Post by GeneApperson » Mon Aug 22, 2011 8:32 pm

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);
}

}

/* ------------------------------------------------------------ */

/***********************************************************************/

GeneApperson
Posts: 239
Joined: Wed Jun 01, 2011 9:53 pm
Location: Pullman WA
Contact:

Re: problems with attachInterrupt

Post by GeneApperson » Mon Aug 22, 2011 8:38 pm

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

Fixe
Posts: 5
Joined: Thu Aug 18, 2011 6:19 pm

Re: problems with attachInterrupt

Post by Fixe » Mon Aug 22, 2011 9:10 pm

Ok, thank you very much!

inex
Posts: 4
Joined: Thu Sep 01, 2011 6:01 pm

Re: problems with attachInterrupt

Post by inex » Thu Sep 01, 2011 6:11 pm

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.

GeneApperson
Posts: 239
Joined: Wed Jun 01, 2011 9:53 pm
Location: Pullman WA
Contact:

Re: problems with attachInterrupt

Post by GeneApperson » Thu Sep 01, 2011 7:06 pm

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

inex
Posts: 4
Joined: Thu Sep 01, 2011 6:01 pm

Re: problems with attachInterrupt

Post by inex » Thu Sep 01, 2011 7:47 pm

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

2. Remove pin 4 from pin 21
The operation also same. No anyting different

When External interrupt is occur ?

inex
Posts: 4
Joined: Thu Sep 01, 2011 6:01 pm

Re: problems with attachInterrupt

Post by inex » Thu Sep 01, 2011 7:58 pm

Hi Gene,
I think now problem fix.
It's my fault. I connect wrong hardware. Now It's ok.

Apologize about my mistake.

Post Reply