PDA

View Full Version : plz help...



Srigopal007
- 1st October 2004, 17:05
Hi. Can someone please help me with this problem. I was wondering if someone has a clue on how to use a look-up table with pbp. basically just reading from a file that is outside of the main PBP program. to be more specific, is it possible to call a variable or an entire subroutine from another file into the main pbp program. Any help or direction will be greatly appreciated. many thanks in advance.


Srigopal

Dwayne
- 1st October 2004, 17:16
Search for "Making Program Space your playground"

Melanie did a wonderful job on this...

Dwayne

http://www.picbasic.co.uk/forum/showthread.php?s=&threadid=137&highlight=Making+program+space+playground

Srigopal007
- 1st October 2004, 17:53
Hi, thanks for the reply. my question to you is, lets say I have two files (One of the file is filled with subroutines, while the other one is where my main program will go). Now, in my main program I want to use one of the subroutines to perform a task (lets say this particular subroutine displays the number 8 onto a screen) and I want to call that subroutine into my main file, where I will be programming in, so that I can display the number 8. How can I call one of those subroutines which is located in another file, into my program? I think in every PBP program we do this by typing in the following
INCLUDE "modedefs.bas"


but not sure how to implement it in my case.

Any code example or advise would be greatly appreciated. Thanks



srigopal

Melanie
- 1st October 2004, 19:59
What you are trying to do is create a program code 'overlay'.

Whilst this happens all the time in a conventional CPU arrangement like your PC, the internal archetecture of the PIC causes some limitations.

1. You need to load the PIC codespace reserved for the overlay in real-time from an external device (such as an EEPROM). This is possible, but not practical from a real-time point of view... certainly not for a single subroutine. It takes significant time to load an overlay... eg you want to display your '8' and it might take 500mS... fine... you can cope with that, but filling a 2 x 16 LCD witch characters that way could take 15 seconds! when I played with this a couple of years back, I recall I was getting a loading delay of about 15 seconds per 1000 words of codespace at best.

2. There is a limit to the number of codespace write/rewrite operations before you wear out the PIC. 100,000 operations may seem a lot, but if you're going to do it once every second to the same spot... your PIC will die before the end of the week.

You can use this method to load say a complex Set-Up/Menu program which is only likely to be used rarely... and then once exited to reload your main program. There is still however a significant time-lag which is unacceptible.

The answer is therefore, Yes, it can be done, but because of the PIC's archetecture it's impractical, delivers unacceptibley poor loading times that cripple overall program performance, and has a limited life cycle.

If you need more codespace, go to an 18 series with 32kb.

rossfree
- 22nd October 2004, 16:54
Melanie,

A search of the data base got me here. I read the thread started by Srigopal007 and get a different meaning from his request.

I am about to take on a fairly complex project (complex for me :-). And I was wondering how to accomplish it with PBP and still keep it manageable. It seems I start quite a few programs that work quite well but as they grow, I find it difficult to keep my thoughts straight. It gets overwhelming for me.

To help myself stay organized and keep the main program size smaller, I thought I would write a second .bas file that I could INCLUDE at the beginning of my main program file. I would work on two files at once, adding subroutines to the file to be INCLUDED and calling them from the main program file.

My understanding is that the two files (plus any other INCLUDE files) will be treated as one when they are compiled. Is this correct?

If so, is there any correct method of writing the file to be INCLUDED (such as header information)? Or can the file be just a "myfile.bas" that has list of subroutines and nothing more?

I think this was what Srigopal007 was trying to ask originally.

Ross

mister_e
- 23rd October 2004, 00:49
you can do it really simple. Lets figure that i want to use those already workable pe-done software
LedBlink.bas
DisplayOnGraphicLCD.bas
DecodeSerialData.bas

the only thing you have to do at the begining of your *main* program is to add these 3 lines:

INCLUDE "LedBlink.bas"
INCLUDE "DisplayOnGraphicLCD.bas"
INCLUDE "DecodeSerialData.bas"

that's it. The only things you have to be sure... Variable definition must not be duplicate. But i never do it like this. I alway use only one file. And I use MicroCode Studio that show you and can make you jump directly to the VAR,DEFINE, SUBROUTINE, LABEL,.... you want to. If you don't know this FREE and usefull program see this link.

http://www.rentron.com/PicBasic/products/MicroCode_Studio.htm

depending on how you want to work. Everybody's different.

regards

Melanie
- 23rd October 2004, 01:00
Yes, of course at compile-time you can use "include", (and also conditional indudes) but somehow when I answered the question originally I was under the impresion this was a run-time request. I suppose looking at it again you could read into it either way. I suppose I made the assumption (always a fatal thing to do) that the request wasn't for the known obvious.

rossfree
- 23rd October 2004, 03:27
Thanks for getting back ya'll.

Yes I do have Microcode Studio and love it. I use the bootloader too. It rocks!

Starting to receive parts for a newly designed board that will control most everything to do with heatsealing and thermoforming fluoropolymer films at work. Really excited about it. I have an electronics gooru friend in Ohio and we've been putting everything we can possibly think of into this board.

It includes two thermocouple inputs, a zero detect circuit, dual power supply, three Pic processors (two for stand-alone phase control of power alternistors) 3 to 8 decoder, 4 relays, 4 transistor drivers, 2 zero to ten volt output circuits (or 4-20 ma)... the list goes on. His work is pretty much over. Mine is just beginning. The board was meant as a platform for most projects we could throw at it. Screw terminals all around on a 6" x 10" circuit board.

I'm babbling. :-)

So anyway... wish me luck! And thanks for the replies!

Cudos to this Forum!

Ross

Srigopal007
- 25th October 2004, 19:04
So does this mean that I can have multiple subroutines in one file called myfile.bas and just type in INCLUDE "myfile.bas" into the main file and it will work perfectly.

this is a oversimplified version of what I plan to do
--------------------------------------------------------------------
'file:myfile.bas

subroutine1:
HIGH LED1
HIGH LED3

Subroutine2:
HIGH LED2
HIGH LED3

Subroutine3:
HIGH LED1
HIGH LED2
'end of myfile.bas
--------------------------------------------------------------------


'mainfile.bas

include "modedefs.bas"
include "myfile.bas"

LED1 VAR PORTB.1
LED2 VAR PORTB.2
LED3 VAR PORTB.3
TRISB = %00000000 'making all of PortB into outputs

start :
goto Subroutine1
goto Subroutine2
goto Subroutine3
goto start
==============================================
question 1:
I am not sure if this is the correct way of calling these subroutines from (myfile.bas)... please let me know if there is a better way of performing the above operation of calling a subroutines from another file.

question2:
Another question which I think is a no brainer for most but for me i it is very hard to visualize when performing this task is ... if i compile this piece of code, will the compiler add myfile.bas and main.bas into the program memory of the PIC microcontroller or will it only include the main.bas ? lets say that I am using the 16C73B which has only 4K of space .... and myfile.bas takes up 3K while main.bas takes up 2K. does this mean that I will get an error when compiling the code.

many thanks in advance for those who read and reply to this message and to get me thinking straight on these few topics.

rossfree
- 26th October 2004, 01:25
Srigopal007

I'd like to take a stab at answering your question.

I re-read the PBP manual on INCLUDES and what I learned supports what Melanie was saying earlier, where she "jumps" to her "Main:" program. Tell me if I'm right Melanie :-)

The assembler inserts the statements from your INCLUDED file in exactly the location where the "INCLUDE myfile.bas" is placed in your main file. That means, that if the INCLUDE statement is placed at the beginning of your program, the program will run those statements first, in the order that they appear in myfile.bas.

That's probably not too good.


Melanie likes to put INCLUDE files near the top of her program but then places a 'Jump' statement before the INCLUDE to re-direct the program to skip over the INCLUDE to get to her main program.

Or, you can put the INCLUDE file at or near the bottom of your main file.

How'd I do Melanie? Do I pass?

Ross

Melanie
- 26th October 2004, 02:52
You're pretty much spot-on Ross.

At compile time, 'includes' will be inserted into your source code at the point where you place the include statement.

You can have as many includes as you want (each referencing a unique filename). Each 'include' will load a file with that name and insert it's contents into your main source file during compilation so that the compiler sees it exactly where you've placed it.

On another thread I detailed the way I layout my programs. If the 'include' is a heap of definitions (eg PIC Configuration Fuse Directives), then I would place that include at the appropriate spot at the start. If the includes are subroutines, then I would place those in the section I reserve for subroutines. Naturally I would not place an include that is say a subroutine at the very start of my program because it's the wrong place to put it as you have correctly stated.

mister_e
- 26th October 2004, 03:19
Let's answer to questions...

question 1:
I am not sure if this is the correct way of calling these subroutines from (myfile.bas)... please let me know if there is a better way of performing the above operation of calling a subroutines from another file.

as your subroutine must know where to return back you must use GOSUB, RETURN statements


question2:
Another question which I think is a no brainer for most but for me i it is very hard to visualize when performing this task is ... if i compile this piece of code, will the compiler add myfile.bas and main.bas into the program memory of the PIC microcontroller or will it only include the main.bas ? lets say that I am using the 16C73B which has only 4K of space .... and myfile.bas takes up 3K while main.bas takes up 2K. does this mean that I will get an error when compiling the code.

Since some statement may be use in both program, the compiler will not add them as 3K+2K. Since my english is really not perfect, i'll try to explain it simple. try this. If you compile this line
lcdout $fe,1,"a"

it will gives you about 117 words. but if you double it
lcdout $fe,1,"a"
lcdout $fe,1,"a"

you will get about 126 words. not 2*117. So since all your INCLUDED program + main will be consider as 1, compiler will calculate regarding all repeated called statement + others. I know it's not really clear but... this is the best way i can explain.


Well done Ross! Have a beer!

In fact INCLUDE must be insert at the end of the main program to avoid to be run before main program occure. It depend on the way you work on program, you can also use 'jump' at the begining. I'll give me my version. Use it if you want.

These lines work. i use PIC12F675 for this example.




;=======
;main.bas
;=======
CMCON = 7
ANSEL = 0
TRISIO = 0
a var byte
led1 var gpio.0
led2 var gpio.1
led3 var gpio.2

gpio=0


Start:
high led1
pause 1000
high led2
pause 1000
high led3
pause 1000
gpio=0
pause 1000

gosub sub1
gosub sub2
gosub sub3
gosub sub4
GOTO Start

include "2.bas"
include "3.bas"

;====
;2.bas
;====
sub1:
for a=1 to 10
gpio=5
pause 200
gpio=0
pause 200
next
return

sub2:
for a=0 to 7
gpio=a
pause 200
next
return

;====
;3.bas
;====
sub3:
for a = 0 to 10
gpio=3
pause 200
gpio=0
pause 200
next
return

sub4:
for a=0 to 10
gpio=4
pause 200
gpio=0
pause 200
next
return



I think this method is a little bit killer... i never do this kind of task. As many, i like to see everything, every code line in my face. Since MicroCode Studio give you flexibility to jump to different Subroutine (better as shi... MPLAB that i hate so much... but it's my own opinion) it's better for you to do some copy/paste to have everything in the same windows. But i hope i answer your question !

regards

Srigopal007
- 26th October 2004, 17:03
Thanks so much steve and everyone who has contributed to this post. your help is genereously appreciated. I am getting a better picture of the programming tasks now. thanks you.