PDA

View Full Version : "gosub" to lable in included file?



amgen
- 5th August 2011, 17:49
Making seperate files so the main pb program file isn't so long and convoluted, and the subjects are more organized, then "including' on the main file......
can I call a routine in another file from the main page with a "gosub"?
'''''''''''''''''''''''''''''''''''''''''''''
INCLUDE "TX_RX.pbp"

on main pbp
xxx
yyy
gosub 123 ''''in TX_RX file
continue

thanks, don
amgen

HenrikOlsson
- 5th August 2011, 18:03
Hi,
Yes you can, did you try it?
The INCLUDE statement puts content of the included file exactly where you place the INCLUDE statement.

/Henrik.

amgen
- 5th August 2011, 18:27
Hi Henrik,
yes, I must have left off the "return" or something, trying again it worked fine. But since I put the defines for the seperate file related variables in that file ( for example the tx and rx variable names etc) , then that "include" had to be left at the top of the main.
Calls to subroutines on that file do work, but I am wondering where those routines get placed into the compiled program or mabye it doesn't matter ?

don

HenrikOlsson
- 5th August 2011, 18:37
They get placed, line by line, exactly where you put the INCLUDE statement. If you you have variables declared inside the included file which you are trying to use "before" actually including the file you'll get errors.

If the following is placed in an include file

myVAR VAR BYTE
TestIt:
HSEROUT["This is a test",13]
High PortB.0
RETURN

And your complete program looked like this:

INCLUDE "myIncludeFile.pbp"
Start:
Low PortB.0
GOSUB TestIt
Pause 1000
Goto Start
It would be exactly the same as

myVAR VAR BYTE
TestIt:
HSEROUT["This is a test",13]
High PortB.0
RETURN
Start:
Low PortB.0
GOSUB TestIt
Pause 1000
Goto Start
See? The INCLUDE statements just takes the content of the file and places it, line by line, "inplace" of the actual INCLUDE statement - that's it.

amgen
- 5th August 2011, 19:37
OK, then it seems that, that would/could throw your main program out-of-wack if you don't realize the includes stuff was placed in the main program first (if that is where they were placed).
Then to place any runnable code , intended to be used as subroutines , in included files, should be:

'TX_RX file

var1 var byte
var2 var word
etc

goto around_code

code and subs to be called later......

around_code

????
don

HenrikOlsson
- 5th August 2011, 20:00
Yes, you need to jump over the subroutines in order for them not to execute as the PIC starts up - exactly as you would if you literally placed your subroutines at the top of the program. There's really no difference. INCLUDE just puts whatever is in the included file, exactly the way it is in the file, exactly at the spot of the INCLUDE statement.

I usually create a file containing all my subroutines, at the beginning of it I have a GOTO OverSubroutines and at the very end I have a label OverSubroutines:
If I have "local" variables used only internally by the subroutines I usually declare them in the subroutines file instead of in the main program file.

You can definitely have the INCLUDE statment after the END of the main program in which case the subroutines will be place there and you don't need to jump around them. But then, if something in the main program tries to access variables you have declared IN the included file you'll get errors because those variables doesn't exist to as the compiler parses thru the source code again exactly as would be the case if you literlly put your variable declarations at the end of the source file.

/Henrik.

amgen
- 5th August 2011, 20:44
Henrik,Thankyou, good stuff.

I will be gone over weekend but next week I would like to ask about making/organizing assorted tables, strings, arrays to be used to send different strings and data, mabye using dw or dt at program time and where to put things, flash, EEprom, ram for easy access etc.
don