Electronics Projects: How to Program PBASIC Subroutines

PBASIC subroutines are invaluable in your electronics projects using a BASIC Stamp processor. A subroutine is a section of a program that can be called from any location in the program. When the subroutine finishes, control of the program jumps back to the location from which the subroutine was called.


Subroutines are useful because they let you separate long portions of your program from the program’s main loop, which simplifies the main program loop to make it easier to understand. Another benefit of subroutines is that they can make your program smaller.


Suppose you're writing a program that needs to perform complicated calculation several times. If you place the complicated calculation in a subroutine, you can call the subroutine from several places in the program. That way, you write the code that performs the complicated calculation only once.


To create and use subroutines, you need to use two PBASIC commands. The first is GOSUB, which calls the subroutine. You typically use the GOSUB command within your program’s main loop whenever you want to call the subroutine. The second command is RETURN, which is always the last command in the subroutine.


To create a subroutine, you start with a label and end with a RETURN command. Between them, you write whatever commands you want to execute when the subroutine is called.


Here’s an example of a subroutine that generates a random number between 1 and 999 and saves it in a variable named Rnd:


GetRandom:
RANDOM Rnd
Rnd = Rnd // 999 + 1
RETURN

To call this subroutine, you would simply use a GOSUB command like this:


GOSUB GetRandom

This GOSUB command transfers control to the GetRandom label. Then, when the GetRandom subroutine reaches its RETURN command, control jumps back to the command immediately following the GOSUB command.


Here is a complete program that uses a subroutine to get a random number between 1 and 1,000 and uses the random number to cause the LED on pin 0 to blink at random intervals. You can run this program on any Basic Stamp circuit that has an LED on pin 0.


' LED Blinker Program
' Doug Lowe
' July 10, 2011
'
' This program blinks the LED on pin 0 randomly.
' {$STAMP BS2}
' {$PBASIC 2.5}
Rnd VAR Word
Led1 PIN 0
DO
GOSUB GetRandom
HIGH Led1
PAUSE Rnd
LOW Led1
PAUSE 100
LOOP
GetRandom:
RANDOM Rnd
Rnd = Rnd // 999 + 1
RETURN

When you use a subroutine, it's vital that you prevent your program from accidentally “falling into” your subroutine and executing it when you didn’t intend it to be executed. For example, suppose that the program in Listing 3-5 used a FOR-NEXT loop instead of a DO loop because you wanted to blink the LED only 100 times. Here’s an example of how not to write that program:


FOR Counter = 1 TO 100
GOSUB GetRandom
HIGH Led1
PAUSE Rnd
LOW Led1
PAUSE 100
NEXT
GetRandom:
RANDOM Rnd
Rnd = Rnd // 999 + 1
RETURN

Do you see why? After the FOR-NEXT loop blinks the LED 100 times, the program will continue with the next command after the FOR-NEXT loop, which is the subroutine!


To prevent that from happening, you can use yet another PBASIC command, END, which simply tells the BASIC Stamp that you have reached the end of your program, so it should stop executing commands. You would place the END command after the NEXT command, like this:


FOR Counter = 1 TO 100
GOSUB GetRandom
HIGH Led1
PAUSE Rnd
LOW Led1
PAUSE 100
NEXT
END
GetRandom:
RANDOM Rnd
Rnd = Rnd // 999 + 1
RETURN

Then, the program will stop after the FOR-NEXT loop finishes.











dummies

Source:http://www.dummies.com/how-to/content/electronics-projects-how-to-program-pbasic-subrout.html

No comments:

Post a Comment