Go Down

Topic: RISING and FALLING interrupt on same pin? (Read 2994 times) previous topic - next topic

ArduinoTom

Can I use a single pin to tigger a RISING interrupt service routine, as well as a separate FALLING interrupt service routine?

Jack Christensen

Use a pin change interrupt, or INT0 and INT1 can both be configured to interrupt on any logical change. Looks like it will be up to the code to determine rising or falling, but that should be straightforward. Read the pin first thing in the ISR, if it's low, then falling, if high then rising. So one ISR rather than two, but the same functionality can be accomplished I think.

DuaneB

Thats badically it, the external interrupts int0 and int1 which are on digital pins 2 and 3 can be set to interrupt on rising, falling or change which is the one you want.

For the other interrupts its a little more difficult, but there is a library that helps. here is a quick overview of the library 'pinchaneint'

http://rcarduino.blogspot.com/2012/03/need-more-interrupts-to-read-more.html

Duane B

rcarduino.blogspot.com


Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

Nick Gammon


Can I use a single pin to tigger a RISING interrupt service routine, as well as a separate FALLING interrupt service routine?


The pin change interrupt isn't going to help any more than doing a CHANGE interrupt would. In any case, there is only one ISR.

Conceivably you could switch from RISING to FALLING inside the ISR, but why bother? By the time you have done that, you may as well have found out (by reading the pin) which one it is.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

robtillaart


Code: [Select]


void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, isr, CHANGE);
}

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

void isr()
{
  if (digitalRead(2) == HIGH) doRising();  // you can use direct port read to be faster - http://www.arduino.cc/en/Reference/PortManipulation -
  else doFalling();
}

doRising()
{
}

doFalling()
{
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

InlineSkater

#5
Jan 23, 2016, 09:01 pm Last Edit: Jan 23, 2016, 09:10 pm by InlineSkater
I realised a routine for starting an exposure timer, which has a single start button at INT1 and the same button should be used to store permanently the selected time (set by a rotary encoder on the remaining pin INT0) into the EEPROM for further usage, if found worthwhile.

Depending on the duration time the button is pressed, different actions will start accordingly. During these activities the interrupts are temporarily blocked from being interrupted by other interrupts.

Here is what I did to swap interrupt modes within different but corresponding ISRs. I hope it may help a bit.

Have fun...

InlineSkater ;)


Code: [Select]

/*------------------------------------------------------------------+
| Question: Can we have an ISR on one pin with different reaction   |
| modes set? Answer: Yes, we can...                                 |
|                                                                   |
| Successfully tested with Arduino Nano in breadboard testbed       |
|                                                                   |
| Author   : InlineSkater                                           |
| Date     : 23.01.2016 20:30:28                                    |
| Revision : 1.0                                                    |
|                                                                   |
| Switch goes low at Pin 3: checked bei ISR, indicated by red LED   |
| ISR mode change realised within two corresponging ISRs to detect  |
| falling & subsequent rising edge of start pulse. Short & long     |
| pulse discriminated and corresponding action started.             |
+------------------------------------------------------------------*/

#define DEBUG
#define REDLED 13
#define BUTTON 3

long unsigned int startTime;
long unsigned int stopTime;
int delta_time;
int delta_store = 600;
int delta_start = 50;

void setup() {
#ifdef DEBUG
  Serial.begin(9600);
  Serial.println(">>> button_test4 >>>");
  while(!Serial) {
    // wait here for established serial connection
  }
#endif
  pinMode(BUTTON, INPUT); 
  digitalWrite(BUTTON, HIGH);
  pinMode(REDLED, OUTPUT);
  digitalWrite(REDLED, HIGH);
  attachInterrupt(INT1, button_low_isr, FALLING);
}

void loop() {
  delta_time = stopTime - startTime;
    if (delta_time >= delta_store) {      // was it a short/long duration?
#ifdef DEBUG
      Serial.println("ACTION 1");
#endif
      action0_task();                    // it was a long duration pulse
    } else {
      if (delta_time >= delta_start) {
#ifdef DEBUG
        Serial.println("ACTION 0");
#endif
        action1_task();                  // it was a short duration pulse
      }   
    }
}

void action0_task() {        // store display time into EEPROM
  noInterrupts();            // as a semaphore: no ISR allowed
 
  // action code here: store digits with no interruption
 
  interrupts();
}

void action1_task() {        // start display time count down
  noInterrupts();            // as a semaphore: no ISR allowed
 
  // action code here: start timer with no interruption
 
  interrupts();
}

void button_low_isr() {      // 1st action: button goes low and we see it
  digitalWrite(REDLED, LOW);
  startTime = millis();      // time shall be noted & mode changed
  attachInterrupt(INT1, button_high_isr, RISING);
}

void button_high_isr() {    // 2nd action: button goes high and we see it
  digitalWrite(REDLED, HIGH);
  stopTime = millis();      // time shall be noted & mode changed
  attachInterrupt(INT1, button_low_isr, FALLING);
}




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!

Arduino
via Egeo 16
Torino, 10131
Italy