Pages: [1]   Go Down
Author Topic: attachInterrupt on HIGH level  (Read 614 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 160
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
int pin = 12;
volatile int state = LOW;

void setup()
{
  Serial.begin(9600);
 
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
  Serial.println(state);
  pinMode(2, INPUT);
  Serial.println(state);
  attachInterrupt(2, blink, HIGH);
  Serial.println(state);
}

void loop()
{
 
  digitalWrite(pin, state);
}

void blink()
{
  delayMicroseconds(20000);
  state = !state;
}

the answer i have from the serial monitor is

0
0
1

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

0
Offline Offline
Shannon Member
****
Karma: 201
Posts: 11744
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
 RISING ]

[ 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 ]
« Last Edit: January 16, 2014, 01:20:54 pm by MarkT » Logged

[ I won't respond to messages, use the forum please ]

0
Offline Offline
Shannon Member
****
Karma: 201
Posts: 11744
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

[ I won't respond to messages, use the forum please ]

Offline Offline
Full Member
***
Karma: 1
Posts: 160
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Shannon Member
****
Karma: 201
Posts: 11744
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's a question for software debouncing surely.
Logged

[ I won't respond to messages, use the forum please ]

Perth, Australia
Offline Offline
Newbie
*
Karma: 3
Posts: 9
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
}
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 160
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Since i'm using ethernet pins PB3 and PB4  should the function be like this?
Code:
void deglitch(Pio* pPio, uint32_t bitMask)
{
    pPio -> PIO_SCIFSR |= bitMask; // Glitch Input Filter Select Register
    pPio -> PIO_IFER |= bitMask;
}
Logged

Pages: [1]   Go Up
Jump to: