PDA

View Full Version : variables and gosubs???



guest_05
- 18th November 2006, 16:20
Hi guys,

i've never used varibles in pbp before, so im trying to do 2 things here:


what i want to do is this:




main:

i = 0
while(i < 10){

Variable = i


gosub ("subName" + Variable)

i = i +1

}

goto main


subName1:

return



subName2:

return


subName3:

return



subName4:

return



its a bit of a mix of picbasic, and php, but im sure someone will get what im trying to do :)

Is it possible?

Thanks very much.

Darrel Taylor
- 18th November 2006, 16:55
Hi guest_05,

You can use the BRANCH or BRANCHL command for that.

Something like this...
i VAR BYTE
Variable VAR BYTE

main:
i = 0
while(i < 5)
Variable = i
gosub SubSwitcher
i = i +1
wend
goto main


SubSwitcher:
BRANCHL variable,[subName0, subName1, subName2, subName3, subName4]
return


subName0:

return


subName1:

return


subName2:

return


subName3:

return


subName4:

return

sayzer
- 18th November 2006, 17:59
DT, you are my teacher and I learn a lot from you here. With all my respect to you, please allow me to make a point;
BRANCHL will cause the program to jump. Thus, "return" with a "subroutine" will not work.

I made my point as below.




i VAR BYTE

main:
FOR i = 0 to 4
BRANCHL i,[subName0, subName1, subName2, subName3, subName4]
Sayzers_Point: 'continue from where we were.
NEXT i
GOTO main


subName0:

GOTO Sayzers_Point


subName1:

GOTO Sayzers_Point


subName2:

GOTO Sayzers_Point


subName3:

GOTO Sayzers_Point


subName4:

goto Sayzers_Point


or another example could be as below.




i VAR BYTE

main:

FOR i = 0 to 4

SELECT CASE i

CASE 0
'do something
CASE 1
'do something else
CASE 2
'do something more
CASE 3
'do something interesting
CASE 4
'do something bro
END SELECT

NEXT i

GOTO main





---------------------------------

mister_e
- 18th November 2006, 18:10
Nope..it will perfectly work dude and what Darrel did it's really clever.

When you'll meet the Return, it will automatically RETURN at the main... but not at SubSwitcher.

It's a kind of On YourVar Gosub.

Try it...

Darrel Taylor
- 18th November 2006, 18:10
That's why the GOSUB was placed inside the while loop.

That gosub calls the SubSwitcher: which then redirects to the appropriate subroutine. Then the subroutine does the return.

The return in the SubSwitcher: is only there in case the variable is larger than the number of available subroutines. Otherwise, it would run subName0 which would be in error.

The subroutines don't return to the SubSwitcher, they return back to the while loop.

HTH,

mister_e
- 18th November 2006, 18:13
How dare you reply exactly at the same time :D

Darrel Taylor
- 18th November 2006, 18:23
That seems to happen alot with us Steve. :D

I should also note here, that Sayzer's examples will work just fine too.

Just different ways to skin a dog.
(sorry, I'm a cat person)
<br>

mister_e
- 18th November 2006, 18:25
:D here we say... every road leads to Roma.

Why? i don't know... but why skin a cat as well :D

sayzer
- 18th November 2006, 18:28
Since I am still learning,

with the information above, will the "return" go back to FOR loop in the example below ?
I guess it will, but I also do not like to guess.



i VAR BYTE

main:

FOR i = 0 TO 4
BRANCHL i,[subName0, subName1, subName2, subName3, subName4]
NEXT i

GOTO main


subName0:

RETURN


subName1:

RETURN


subName2:

RETURN


subName3:

RETURN


subName4:

RETURN





mister_e, you are also my teacher as you know! :)

mister_e
- 18th November 2006, 18:32
In your example the return won't work as Branch(l) use Goto. It's just a Select CAse goto.. or kind of

I wonder why Branch(l) don't use gosub so far... easy to modify the macro but...

sayzer
- 18th November 2006, 18:42
In your example the return won't work as Branch(l) use Goto. It's just a Select CAse goto.. or kind of

I wonder why Branch(l) don't use gosub so far... easy to modify the macro but...


Did you mean the example in post #9 ?


-----------------------------

mister_e
- 18th November 2006, 18:52
Yes indeed... post #9

The various BRANCH?xxx and BRANCHL?xxx macros, mess around BIT?GOTO and L?GOTO

EDIT: also ... TABLE?C

guest_05
- 18th November 2006, 18:54
Ahhhhhh!!! what a cleaver way to do it!!! thanks so much Darrel.

I would never have thought about using an array like that

Cheers.

Now i just have to find out if im using the code the right way lol

(p.s based on the code you gave me, how much more code could a 16f628 allow?)

and one more question, im trying to use this code to make a sort of clock.

i'm using the pins LOW to make the circuit, but i dont really want to use HIGH to stop the circuit, is there anway to just turn the pin off, like when the program is run at the start?
Thanks again :)

Ta.

sayzer
- 18th November 2006, 18:59
....

(p.s based on the code you gave me, how much more code could a 16f628 allow?)

Ta.



DT's code is 58 words; you have about 2048 words in 628.


mister_e, in this case, can I make a conclusion as

"return" in a subroutine referred by a BRANCHL inside a WHILE will take the subroutine back to that WHILE statement.

But, this is not true for a FOR loop.


Am I right?


-------------------------

mister_e
- 18th November 2006, 19:13
Nope... a return will never work with Branch(L) anyway, as Branch(l) use goto.

Branch index,[Label1, Label2,Label3] is something like ...


Select CASE index
Case 1 : goto Label1
Case 2 : goto Label2
Case 3 : goto Label3
End Select

sayzer
- 18th November 2006, 19:18
...

The subroutines don't return to the SubSwitcher, they return back to the while loop.

...


If there is no typo here, did I misunderstand this statement then ?



-----------------------------

rhino
- 18th November 2006, 19:33
I'm a little confused as well. I guess as a test, you can put a statement under the branch instruction to flash an LED or something to see what happens. The way I read it is it will return back to the while loop because there is nothing else in the SubSwitcher routine to execute after the branch statement, just another return back to the while loop.

Darrel Taylor
- 18th November 2006, 19:43
Ok guys, sit back, take a breath, give me a little while to come up with a better answer.

All will be revealed. Or further confused. We'll see.

mister_e
- 18th November 2006, 19:51
I'll try something...



StartPoint:
Gosub Somewhere
Label1:
Pause 100
Goto Start
Somewhere:
Pause 1
Return


Nothing hard here right? after the Return... you return to Label1 ... right?

Now...


StartPoint:
Gosub Somewhere
Label1:
Pause 100
Goto Start
Somewhere:
Goto SomewhereElse
z: goto z

SomewhereElse:
Return


Same thing happen here...

This said...



Start:
for routines=0 to 3
StartPoint:
gosub subselect
ReturnPoint:
pause 200
next
goto start

SubSelect:
branchl routines, [Routine0, Routine1, Routine2, Routine3]

Routine0:
return


Where the Return of Routine0 leads you?... to ReturnPoint

I know i'm a pretty bad teacher... :(

sayzer
- 18th November 2006, 20:01
and I am a good student.


I thought that once the program jumps, then it will forget where it was and will return to an unknown place. So that it will never be 100% precise.


---------------------

mister_e
- 18th November 2006, 20:04
Gosub and Return are good friends...

As long as you have a GOSUB... you can insert 2789423564875634275624365 432956345634564326586324957243657 234596243652 43656243756243654 356437856 46582436587243657 26435726435 2436 (+/- 1) goto in-between. And when you'll use Return, it will return just under the GOSUB.

SteveB
- 18th November 2006, 20:13
I thought that once the program jumps, then it will forget where it was and will return to an unknown place. So that it will never be 100% precise.

To be even more specific, the GOSUB will put a return address on the top of the "Stack" It will remain there forever (as long as the PIC is not reset) until a RETURN is executed. So, Steve's 2.7894235e+128 GOTOs do nothing to modify the address on the top of the stack. And, no matter when the RETURN is executed, the program will return to that address.

HTH,
Steve B

mister_e
- 18th November 2006, 20:17
SteveB....SteveB....SteveB....
SteveB....SteveB....SteveB....
Whizzz... Woohoo...

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1198&stc=1&d=1163881004">

Darrel Taylor
- 18th November 2006, 20:27
So much for my fancy reply. Too slow.

Got it Sayzer??

SteveB
- 18th November 2006, 20:33
So much for my fancy reply. Too slow.

Ahhhhhhh :(, I was really looking forward to it. My stab at it was just a down and dirty continuation of what Steve was getting at. We'll let Sayzer and Ryan weigh in on whether it worked for them.

SteveB
- 18th November 2006, 20:37
Steve,
ROFL, Great picture!

mister_e
- 18th November 2006, 20:40
Hehe, Rock and roll, DEVIL!

sayzer
- 18th November 2006, 20:40
I have a new teacher now! :D

Thanks for this simple and clear explanation SteveB.

I was expecting DT to come up with really complex explanations. heheh :)


--------------------------

mister_e
- 18th November 2006, 20:47
mmm... not complex Sayzer, let's say detailled.

rhino
- 18th November 2006, 21:06
Yep... makes sense. After all of your responses and looking at the manual for BranchL...
...Index selects one of a list of Labels. Execution resumes at the
indexed Label. For example, if Index is zero, the program jumps to the
first Label specified in the list, if Index is one, the program jumps to the
second Label, and so on. If Index is greater than or equal to the
number of Labels, no action is taken and execution continues with the
statement following the BRANCHL.....(hence the RETURN)
I guess seeing that return in Darrel's code was throwing me off, but now it I see why it's in there. It looked to me like a way of nesting subroutines. Both of the Steves explainations of how the return address in the stack works answers one of the questions I didn't even ask.... scary! Thankyou all. :)

Darrel Taylor
- 18th November 2006, 21:08
I was going more for ... Cool!

Well, since we don't need the explanation anymore, I'll leave it at this, since it's already done. :)

http://www.darreltaylor.com/files/GOSUB-N.gif

mister_e
- 18th November 2006, 21:13
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1199&stc=1&d=1163884369">

rhino
- 18th November 2006, 21:16
Cool indeed!!! You're going to have to share with us your posting techniques!

sayzer
- 18th November 2006, 21:17
Holy Gif!

DT, I can't believe you made it within half an hour!
What can I say? It is just as you said it was.




rhino,

I now see that you were in a different page then I was.

But, you had your answer and I had mine.


Thanks to all.

I enjoyed watching posts coming in and out, too.

Especially this last post by DT!

---------------------------


Edit: I just realized that it has been two hrs....


------------------------------------

mister_e
- 18th November 2006, 21:28
Darrel...Darrel...Darrel...

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1198&d=1163881004">
Darrel...Darrel...Darrel...
Woohoo..Whizz..ROCK AND ROLL!!!

Darrel Taylor
- 18th November 2006, 22:41
Thanks guys,


You're going to have to share with us your posting techniques!

Piece of cake (with the right software).
Alt-Print Screen (windows XP, screen capture)
MS Paint to import and crop
Selteco Bannershop GIF animator. http://www.selteco.com/bannershop/
and about an hour creating 58 animation frames.

I'll have to try it again sometime.
<br>

Darrel Taylor
- 18th November 2006, 23:21
Oh, Ta,
I see you added another question to post#13. Almost missed it.


and one more question, im trying to use this code to make a sort of clock.

i'm using the pins LOW to make the circuit, but i dont really want to use HIGH to stop the circuit, is there anway to just turn the pin off, like when the program is run at the start?
Thanks again


INPUT PORTB.0

' --- OR ---

TRISB.0 = 1

Either of those will return the pin to High-Impedance state.

HTH,

SteveB
- 19th November 2006, 01:16
Well, what d'ya know!? I Make a run for something to throw on the grill and the next thing I know, Darrel has raised the bar when in comes to posting code explainations!

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1202&d=1163898624">
Now that's what I was looking forward to!!! :D

Darrel not only came up with a quite elegent solution for the origianl problem in a matter of minutes, but then went over the top in showing how it works.

Way to Go!

Steve B

rhino
- 19th November 2006, 02:46
Very cool Darrel. I'm sure we're going to start seeing some interesting posts from now on. Thanks again, and keep raising the bar!