Go Down

Topic: attachInterrupt on HIGH level (Read 1 time) previous topic - next topic


Hi everyone,
i'm facing problems in implementing an interrupt triggered by HIGH level on a pin

Code: [Select]
int pin = 12;
volatile int state = LOW;

void setup()
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
  pinMode(2, INPUT);
  attachInterrupt(2, blink, HIGH);

void loop()
  digitalWrite(pin, state);

void blink()
  state = !state;

the answer i have from the serial monitor is


Therefore i can only suppose that the attachInterrupt function changes in sort of of way the pin status. How come?


Jan 16, 2014, 07:13 pm Last Edit: Jan 16, 2014, 07:20 pm by MarkT Reason: 1
Firstly delayMicroseconds() doesn't work for large values (known bug), which is why
you are seeing the state change immediately.

Secondly why are you trying to delay for 20ms in an interrupt routine, that would be
locking up the whole system and breaking millis(), micros() and delay() due to missed
timer interrupts?

Interrupt routines should run fast and lean - many things don't work while it
them (such as delay() and serial IO), since interrupts are off.

Set some variables, digitalRead, digitalWrite, analogWrite, that's the sort of thing to do in an interrupt routine - the heavy lifting (and waiting) is for loop().

[ Oh yes, the other thing, you probably don't want to use attachInterrupt on HIGH, try

[ ARGH, sorry, forgot this was DUE forum - is this the Due?  Don't know about
delayMicroseconds () on that, it may well work, but the issues of delaying in an
ISR still apply ]
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


Right, proper reply for Due:

You really never ever want to use HIGH as an interrupt condition on the Due.

If you do the whole system will block while the pin in question is HIGH since the
interrupt status never clears so the ISR runs repeatedly so long as the pin is high.

Furthermore even if the pin is LOW when you first attach the interrupt it runs
once (probably because some status hasn't been properly cleared).

Just using RISING or CHANGE like one normally would.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


i need to use attachInterrupt HIGH. my sam3x is connected to a PIC10F204 which controls the board's switching off. when i press the power button for more than 2 seconds the PIC sends an HIGH signal to the SAM and i'm afraid RISING could be triggered from some kind of noise


That's a question for software debouncing surely.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


The Due also has glitch filtering which should stop triggering from electrical noise

Page 647 of the data sheet

Something like this, I haven't tested it yet.
Code: [Select]
void deglitch(int pin)
  g_APinDescription[pin].pPort->PIO_SCIFSR |= g_APinDescription[pin].ulPin; // Glitch Input Filter Select Register
  g_APinDescription[pin].pPort->PIO_IFER |= g_APinDescription[pin].ulPin;


Since i'm using ethernet pins PB3 and PB4  should the function be like this?
Code: [Select]

void deglitch(Pio* pPio, uint32_t bitMask)
    pPio -> PIO_SCIFSR |= bitMask; // Glitch Input Filter Select Register
    pPio -> PIO_IFER |= bitMask;

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