Shop OBEX P1 Docs P2 Docs Learn Events
Returning a value from an Interrupt? — Parallax Forums

Returning a value from an Interrupt?

RandyRRandyR Posts: 5
edited 2009-04-24 13:30 in General Discussion
I'm trying to update a counter variable from within an interrupt, but the value is lost after returning from the interrupt code. I'm running the SX/B 2.00.21 beta.

I've tried using the INTERRUPT NOCODE command among other things to no avail. Any suggestions?

Comments

  • JonnyMacJonnyMac Posts: 9,213
    edited 2009-04-23 23:32
    Can you post your code? There is no reason for your value becoming "lost" -- I use interrupts to handle timers, etc. which means the values are not being lost.
  • RandyRRandyR Posts: 5
    edited 2009-04-24 01:44
    Thanks for the quick response!

    Here's an abreviated version of the code. When the sensor is taken high, the interrupt is triggered and the IF statement within the interrupt turns on LED1. But the same IF statement below in the main program won't turn on LED2.

    DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
    FREQ 4_000_000

    Sensor PIN RB.0 INPUT INTR_RISE
    LED1 PIN RA.0 OUTPUT
    LED2 PIN RA.1 OUTPUT
    Counter VAR Byte

    INTERRUPT NOCODE
    ISR_Start:
    · IF Sensor = 1 THEN
    · · Counter = 60
    · · IF Counter <> 0 THEN
    · ··· LOW LED1
    · ··· Pause 200
    · ··· HIGH LED1
    · · ENDIF
    · ENDIF

    ISR_Exit:
    · WKPND_B = %00000000
    RETURNINT

    PROGRAM Start

    Start:
    · WKED_B = %11111000
    · WKEN_B = %11111000
    · WKPND_B = %00000000
    · HIGH LED1
    · HIGH LED2
    · Counter = 0

    Main:
    · IF Counter <> 0 THEN
    · · LOW LED2
    ··· Pause 200
    ··· HIGH LED2
    · ENDIF
    · PAUSE 5000
    · Counter = 0
    GOTO Main


    Post Edited By Moderator (Bean (Hitt Consulting)) : 4/24/2009 1:21:39 PM GMT
  • JonnyMacJonnyMac Posts: 9,213
    edited 2009-04-24 02:12
    You're going to get strange behavior putting PAUSE in your interrupt, but that won't corrupt "counter." What are you trying to accomplish? I would suggest simply setting a flag in the interrupt and letting the foreground take care of blinking the LEDs for you.
  • RandyRRandyR Posts: 5
    edited 2009-04-24 11:11
    Agreed about the pause statements. I only have the LED's (and pauses) there to test the code. The full application has 3 sensors, 3 relays and 10 DIP switches and everything else works great. The code above is the test code I'm using to isolate and resolve this one problem. I've tried dozens of combinations and I simply can't get the interrupt code to pass a value back to the main program.

    Can you suggest how I might set a flag? I'll try anything at this point...
  • BeanBean Posts: 8,129
    edited 2009-04-24 11:56
    Randy,
    First of all remove the NOCODE option from the INTERRUPT line.

    Second, I don't see anywhere in your code where you are changing the value of counter ? You test it at a couple places and assign it a value in a couple places but how are you expecting it's value to change ?

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There is a fine line between arrogance and confidence. Make sure you don't cross it...

    ·
  • RandyRRandyR Posts: 5
    edited 2009-04-24 12:40
    The "Counter = 60" statement within the interrupt code should change the value, shouldn't it? I removed the NOCODE option, but still same result.
  • Dave HeinDave Hein Posts: 6,347
    edited 2009-04-24 13:10
    Randy,

    Which variable are you having trouble with?· Is it Counter or WKPND_B?· Do the LEDs flash correctly when you get the interrupt?· If both LEDs flash, then the main routine is see the new value of Counter OK.· Is the value of KDPND_B the problem?

    I don't understand the reason for the "IF Counter <> 0" statement.· The line above it set the value of Counter to 60, so it will alway be nonzero.

    Dave
  • BeanBean Posts: 8,129
    edited 2009-04-24 13:15
    Randy,
    Are you expecting the variable "Counter" to be automatically incremented with each interrupt ? It will not be. You will need a "INC Counter" in the interrupt routine to make that happen.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There is a fine line between arrogance and confidence. Make sure you don't cross it...

    ·
  • RandyRRandyR Posts: 5
    edited 2009-04-24 13:30
    I'm having trouble with "Counter", but I think I have just found the issue!

    The interrupt was occurring (and returning) into the pause which is followed by a reset of the counter variable! My error seems to be caused by confusion with the delays in the program execution. I'll work on correcting this in the full production code and confirm.

    Thanks for all the prompt responses!!
Sign In or Register to comment.