Newcomer can't understand why subroutines don't work with RETURN
glennw
Posts: 8
I have a 'basic' question about the behavior of my subroutines. I am writing a program that interfaces to an A/D converter and LCD display to measure analog voltages. My main program loop looks like this:
'''''''''''''''''''''''''''
'define and initialize variables and constants
'dislay welcome screen
'set page
'''''''''''''''''''''''''''
Main:
PAUSE 100
BUTTON Btn1, 0, 255, 250, btnWrk, 1, Backlight
BUTTON Btn2, 0, 255, 250, btnWrk, 1, DisplayMode
loopcount = loopcount + 1
mux_ch = ch0
GOSUB Read_0834
volts = param
mux_ch = ch1
GOSUB Read_0834
chrg_amps = param
mux_ch = ch2
GOSUB Read_0834
load_amps = param
IF loopcount = 10 THEN
loopcount = 0
GOTO Update_display
ENDIF
GOTO Main
Every 10 main program loops I go and update my display with the latest A/D values. My Read_0834 subroutine works as expected and uses a RETURN statement. My Update_display subroutine looks like this:
Update_display:
''''''''''''''''''''''''Set & scale Volts, Amps'''''''''''''''''''''''''''''''''
'set and scale volts, chrg_amps, load_amps
'''''''''''''''''''''''Display results'''''''''''''''''''''''''''''''''''''
IF page = 2 THEN
SEROUT 15, 32, [noparse][[/noparse]148] 'move cursor to Volts position
SEROUT 15, 32, [noparse][[/noparse]DEC volt1, DEC volt2, "."] 'write battery voltage
SEROUT 15, 32, [noparse][[/noparse]DEC volt3, DEC volt4]
SEROUT 15, 32, [noparse][[/noparse]157] 'move cursor to Chrg amps position
SEROUT 15, 32, [noparse][[/noparse]DEC chrg_amps1, DEC chrg_amps2, "."] 'write chrg_amps
SEROUT 15, 32, [noparse][[/noparse]DEC chrg_amps3]
ELSEIF page = 3 THEN
SEROUT 15, 32, [noparse][[/noparse]148] 'move cursor to Volts position
SEROUT 15, 32, [noparse][[/noparse]DEC volt1, DEC volt2, "."] 'write battery voltage
SEROUT 15, 32, [noparse][[/noparse]DEC volt3, DEC volt4]
SEROUT 15, 32, [noparse][[/noparse]157] 'move cursor to Drain amps position
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps1, DEC load_amps2, "."] 'write chrg_amps
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps3]
ELSEIF page = 1 THEN
SEROUT 15, 32, [noparse][[/noparse]139] 'move cursor to charge A-Hr position
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps1, DEC load_amps2, "."] 'write load_amps
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps3]
SEROUT 15, 32, [noparse][[/noparse]159] 'move cursor to drain A-Hr position
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps1, DEC load_amps2, "."] 'write chrg_amps
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps3]
ENDIF
RETURN
When the RETURN statement is used, the subroutine exits to the beginning of the program (and displays my welcome screen). If I use a GOTO main statement at the end of the subroutine, then it functions as I expect.
My Backlight and DisplayMode subroutines from the BUTTON command have a similar problem with RETURN.
Any help would be appreciated.
Glenn
'''''''''''''''''''''''''''
'define and initialize variables and constants
'dislay welcome screen
'set page
'''''''''''''''''''''''''''
Main:
PAUSE 100
BUTTON Btn1, 0, 255, 250, btnWrk, 1, Backlight
BUTTON Btn2, 0, 255, 250, btnWrk, 1, DisplayMode
loopcount = loopcount + 1
mux_ch = ch0
GOSUB Read_0834
volts = param
mux_ch = ch1
GOSUB Read_0834
chrg_amps = param
mux_ch = ch2
GOSUB Read_0834
load_amps = param
IF loopcount = 10 THEN
loopcount = 0
GOTO Update_display
ENDIF
GOTO Main
Every 10 main program loops I go and update my display with the latest A/D values. My Read_0834 subroutine works as expected and uses a RETURN statement. My Update_display subroutine looks like this:
Update_display:
''''''''''''''''''''''''Set & scale Volts, Amps'''''''''''''''''''''''''''''''''
'set and scale volts, chrg_amps, load_amps
'''''''''''''''''''''''Display results'''''''''''''''''''''''''''''''''''''
IF page = 2 THEN
SEROUT 15, 32, [noparse][[/noparse]148] 'move cursor to Volts position
SEROUT 15, 32, [noparse][[/noparse]DEC volt1, DEC volt2, "."] 'write battery voltage
SEROUT 15, 32, [noparse][[/noparse]DEC volt3, DEC volt4]
SEROUT 15, 32, [noparse][[/noparse]157] 'move cursor to Chrg amps position
SEROUT 15, 32, [noparse][[/noparse]DEC chrg_amps1, DEC chrg_amps2, "."] 'write chrg_amps
SEROUT 15, 32, [noparse][[/noparse]DEC chrg_amps3]
ELSEIF page = 3 THEN
SEROUT 15, 32, [noparse][[/noparse]148] 'move cursor to Volts position
SEROUT 15, 32, [noparse][[/noparse]DEC volt1, DEC volt2, "."] 'write battery voltage
SEROUT 15, 32, [noparse][[/noparse]DEC volt3, DEC volt4]
SEROUT 15, 32, [noparse][[/noparse]157] 'move cursor to Drain amps position
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps1, DEC load_amps2, "."] 'write chrg_amps
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps3]
ELSEIF page = 1 THEN
SEROUT 15, 32, [noparse][[/noparse]139] 'move cursor to charge A-Hr position
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps1, DEC load_amps2, "."] 'write load_amps
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps3]
SEROUT 15, 32, [noparse][[/noparse]159] 'move cursor to drain A-Hr position
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps1, DEC load_amps2, "."] 'write chrg_amps
SEROUT 15, 32, [noparse][[/noparse]DEC load_amps3]
ENDIF
RETURN
When the RETURN statement is used, the subroutine exits to the beginning of the program (and displays my welcome screen). If I use a GOTO main statement at the end of the subroutine, then it functions as I expect.
My Backlight and DisplayMode subroutines from the BUTTON command have a similar problem with RETURN.
Any help would be appreciated.
Glenn
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Who says you have to have knowledge to use it?
I've killed a fly with my bare mind.
That can and may happen as it probably does in this case. That isn't true in the general case however. It really returns to the next return location in the return stack. In this case it happens to be the beginning of the program.
I just disn't want folks to think this was a tricky way to return to the beginning of the program.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
Regarding the Button command use, "Backlight" and "DisplayMode" are statement labels (addresses)·to which your program will branch if the condition (state) of the Btn is met.
Your program will then continue executing at that label, and where it goes next (sequentially to the next statement, or branching elsewhere) depends on how you structure that portion of your program.· The place to which your Button command branches is not a subroutine, and will not have a Return. So, for example, replace those Return statements with some appropriate GoTo (or other flow control)·statements.
PAR
·
I appreciate all the responses. This is a great forum!