Returning a value from an Interrupt?
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?
I've tried using the INTERRUPT NOCODE command among other things to no avail. Any suggestions?
Comments
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
Can you suggest how I might set a flag? I'll try anything at this point...
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...
·
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
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...
·
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!!