Interrupt issue

11 Nov 2010

I'm using an InterruptIn to monitor a opto sensor input, the sensor is switched on and off (armed/disarmed) using the code below

                case 0x04 :                                             // arm sensor button has been pressed so toggle sensor arm/disarm
                    senseflag = !senseflag;                         // invert senseflag
                    if (senseflag == 0) {                              // if senseflag = 0
                        sense.rise(NULL);                            // ignore sensor (switch it off)
                    } else if (senseflag == 1) {                     // if senseflag = 1
                        sense.rise(&sense_int);                    // arm patient detect sensor (switch it on)
                    }
                    break;

The input switch is part of a diode'd switch matrix that when one of 10 buttons is pressed a 4 bit binary code is inputted to a BusIn that is then read and the state of the bus stored and actions taken using a case statement based on the stored value.

If the sensor input goes low (ie the sensor no longer detects something) an alarm is activated.

Due to the application the sensor input can only be high when the sensor on button is pressed (sensor is armed), this has been monitored with a scope to ensure this is true yet sometimes (and only sometimes) the alarm sounds immeadiately upon arming the sensor implying that a rising edge has been detected. The scope says otherwise and shows no sign of noise on the line.

I have an external pull up on the sensor input and have tried setting the internal PullUp as well. The sensor has a 0 - 5v output, 0V when detecting something and 5V when not.

Is there any thing wrong with the code or something i'm missing about Interrupts and how to enable/disable them? 

11 Nov 2010

OK, so the code above has already been slightly altered. All i do now is arm enable the InterruptIn if the case statement is true and disable it on the way out of the interrupt routine:

case 0x04 :                                              // sensor button has been pressed so toggle sensor arm/disarm
        sense.rise(&sense_int);                     // arm patient detect sensor
        break;

//-------------- Interrupt routine -----------------------------------

void sense_int () {                       // patient detect sensor tripped
       alarmflag = 1;                      // set flag to exit RTZ corectly if sensor interrupts it
       fet1 = 0;                              // switch motors off
       fet2 = 0;
       wait(0.05);                          // wait for induced motor current to dissipate
       act2 = 0x00;                       // switch relays off (break motor)
       act1 = 0x00;

       lcd.cls();                             // set up alarm screen
       lcd.locate(1,1);
       lcd.printf("MOVEMENT DETECTED");
       lcd.locate(1,2);
       lcd.printf("PRESS ALARM RESET");

      while (1) {                           // sit in alarm loop
             alarm = 1;
             wait (0.05);
             alarm = 0;
             wait (0.1);
             alarm = 1;
             wait (0.1);
             alarm = 0;
             wait (0.5);
             if (switches == 0x08) {         // has the alarm reset button been pressed
                 break;
            }
        }
        if (lock ==1) {                         // if keypad locked ...
           lock = 0;                           // ....unlock keypad
           lcd.locate(0,0);
           lcd.printf("                    ");
           lcd.locate(0,1);
           lcd.printf("  KEYPAD UNLOCKED   ");
           lcd.locate(0,2);
           lcd.printf("                    ");
           wait(1);
       }
      sense.rise(NULL);                      // turn interrupt off (turned on when button pressed)
      herga_splash();                         // refresh main menu screen
}

//---------------- end of interrupt -----------------------

The result is still same, the sensor output is high when the 'arm' button is pushed (which attaches the address of the sensor interrupt function to the rising edge of the sensor input pin) yet it immeadiately jumps to the interrupt function implying that a rising edge has been detected :(

Any suggestion would be well appreciated, thanks

11 Nov 2010

Overcome the issue by enabling the interrupt permenantly and setting a flag when the arm buttons pressed, the flag is then checked in the interrupt routine and an alarm raised if the flag = 1, finally the flag is cleared at the end of the interrupt if the alarm has been raised.

many ways of skinning a cat!

 

case 0x04 :                                              // sensor button has been pressed so toggle sensor arm/disarm
        senseflag =1;                     // arm patient detect sensor
        break;

//-------------- Interrupt routine -----------------------------------

void sense_int () {                       // patient detect sensor tripped

      if (senseflag ==1){

       alarmflag = 1;                      // set flag to exit RTZ corectly if sensor interrupts it
       fet1 = 0;                              // switch motors off
       fet2 = 0;
       wait(0.05);                          // wait for induced motor current to dissipate
       act2 = 0x00;                       // switch relays off (break motor)
       act1 = 0x00;

       lcd.cls();                             // set up alarm screen
       lcd.locate(1,1);
       lcd.printf("MOVEMENT DETECTED");
       lcd.locate(1,2);
       lcd.printf("PRESS ALARM RESET");

      while (1) {                           // sit in alarm loop
             alarm = 1;
             wait (0.05);
             alarm = 0;
             wait (0.1);
             alarm = 1;
             wait (0.1);
             alarm = 0;
             wait (0.5);
             if (switches == 0x08) {         // has the alarm reset button been pressed
                 break;
            }
        }
        if (lock ==1) {                         // if keypad locked ...
           lock = 0;                           // ....unlock keypad
           lcd.locate(0,0);
           lcd.printf("                    ");
           lcd.locate(0,1);
           lcd.printf("  KEYPAD UNLOCKED   ");
           lcd.locate(0,2);
           lcd.printf("                    ");
           wait(1);
       }
      herga_splash();                         // refresh main menu screen

      }

      senseflag =0;
}

//---------------- end of interrupt -----------------------