PDA

View Full Version : Can RESUME replace RETURN in a GOSUB?



peterdeco1
- 7th October 2005, 11:54
Hello Everyone. I broke the rules and have a GOSUB that could possibly never get cancelled under certain conditions. I haven't had a problem yet but I have read posts by others on this forum who have had problems when a GOSUB never gets cancelled. Can I replace BADCODE with GOODCODE to get the same results? Thank you. - Peter

BADCODE:
SOMEWHERE IN MY CODE:
IF PORTA.1 = 1 THEN GOSUB DOSOMETHING

DOSOMETHING:
IF PORTB.2 = 1 Then Return 'NO PROBLEM - GOSUB GETS CANCELLED
IF PORTA.5 = 0 Then Return 'NO PROBLEM - GOSUB GETS CANCELLED
IF PORTB.0 = 0 Then START '***HERE IS THE PROBLEM - GOSUB MAY NEVER GET CANCELLED
GoTo DOSOMETHING 'STAY HERE UNTIL RB2, RA5 OR RB0 DOES SOMETHING
****************************************
GOODCODE:
SOMEWHERE IN MY CODE:
IF PORTA.1 = 1 THEN DOSOMETHING 'REMOVE THE GOSUB

DOSOMETHING:
IF PORTB.2 = 1 Then RESUME 'IS THIS THE SAME AS A GOSUB RETURN?
IF PORTA.5 = 0 Then RESUME 'IS THIS THE SAME AS A GOSUB RETURN?
IF PORTB.0 = 0 Then START 'IS THIS A GOOD REPLACEMENT FOR BADCODE?
GoTo DOSOMETHING 'STAY HERE UNTIL RB2, RA5 OR RB0 DOES SOMETHING

Dwayne
- 7th October 2005, 14:31
Hello Peter,

Peter>>I broke the rules and have a GOSUB that could possibly never get cancelled under certain conditions. I haven't had a problem yet but I have read posts by others on this forum who have had problems <<

I think you are asking the wrong question to yourself... I think you should be asking yourself:

"Why Chance it?"

Good Ol Murphy will get you sometime or another. If you know there is a known problem, and you have found that known problem, and it is a fixable situation....I would fix it...ASAP. It is not worth the headache when it dies.

Dwayne

peterdeco1
- 7th October 2005, 18:23
Thanks Dwayne. That is what I am trying to do. If I can substitute RESUME with GOSUB and RETURN this should fix it right? The alternative is to break down about 200 lines of code and find an alternative. The subroutine START that I have in my bad GOSUB code is very important. It addresses, powers down other chips, checks for button & switches & has to be there. - Peter

Darrel Taylor
- 7th October 2005, 18:43
Hi Peter,

The RESUME command does the same thing as a RETURN, except that it also turns on the GIE (INTCON.7) bit to re-enable Interrupts. &nbsp; Not a good idea if your program isn't set-up to handle interrupts. And, since it's still a RETURN, it doesn't solve your problem.

However, unlike Visual Basic, the program doesn't have to return from the exact same Subroutine that was called to begin with. You can GOTO another section of code and then RETURN from there.
BADCODE:
SOMEWHERE IN MY CODE:
IF PORTA.1 = 1 THEN GOSUB DOSOMETHING

DOSOMETHING:
IF PORTB.2 = 1 Then Return
IF PORTA.5 = 0 Then Return
IF PORTB.0 = 0 Then DoSomethingElse
GoTo DOSOMETHING 'STAY HERE UNTIL RB2, RA5 OR RB0 DOES SOMETHING

DoSomethingElse:
...
RETURN
Or, if you want it to jump somewhere that doesn't return, you can set a "Flag" in the subroutine, and act on it in the main loop.
FlagBit VAR BIT

BADCODE:
SOMEWHERE IN MY CODE:
FlagBit = 0
IF PORTA.1 = 1 THEN GOSUB DOSOMETHING
IF FlagBit = 1 then START
...

DOSOMETHING:
IF PORTB.2 = 1 Then Return
IF PORTA.5 = 0 Then Return
IF PORTB.0 = 0 Then FlagBit = 1 : Return
GoTo DOSOMETHING 'STAY HERE UNTIL RB2, RA5 OR RB0 DOES SOMETHING
This way, the RETURN has been satisfied, so you're free to GOTO anywhere.
<br>

peterdeco1
- 8th October 2005, 10:51
Thank you Darrel. I thought
IF PORTB.0 = 0 Then FlagBit = 1 : Return
was the same as
IF PORTB.0 = 0 Then FlagBit = 1
Return
If it is the same, isn't it going to RETURN regardless of PORTB.0 or Flagbit? Does the first statement RETURN only when PORTB.0 = 0? Thank you. -Peter


DOSOMETHING:
IF PORTB.2 = 1 Then Return
IF PORTA.5 = 0 Then Return
IF PORTB.0 = 0 Then FlagBit = 1 : Return
GoTo DOSOMETHING 'STAY HERE UNTIL RB2, RA5 OR RB0 DOES SOMETHING

Darrel Taylor
- 8th October 2005, 11:11
You can have several statements on a single line and they will all be executed ONLY if the IF is true.

IF A = B then C = A : LOW PORTB.0 : PAUSE 100 : RETURN

Which is the same as ...

IF A = B then
&nbsp;&nbsp;&nbsp;&nbsp;C = A
&nbsp;&nbsp;&nbsp;&nbsp;LOW PORTB.0
&nbsp;&nbsp;&nbsp;&nbsp;PAUSE 100
&nbsp;&nbsp;&nbsp;&nbsp;RETURN
ENDIF

HTH

peterdeco1
- 9th October 2005, 09:03
Hi Darrel. You have opened up a whole new way for me to accomplish certain tasks. I migrated from a different basic compiler which doesn't support multiple commands on a single line. Being able to do this solves a lot of problems. - Peter