PBASIC Programming
PBASIC Programming
Program Structure
Humans often think of computers and microcontrollers as smart devices and yet, they will do nothing without a specific set of instructions. This set of instructions is called a program. It is our job to write the program. Stamp programs are written in a programming language called PBASIC, a Parallax-specific version of the BASIC (Beginners All-purpose Symbolic Instruction Code) programming language. BASIC is very popular because of its simplicity and English-like syntax. A working program can be as simple as a list of statements. Like this:
statement1 statement2 statement3 END
Comments
Comments are included in a program for readability and notes. These statements are not loaded into the microcontroller and do not take up any program space. Each comment starts with an apostrophe. Comments can be on lines by themselves, or to the right of PBASIC programming statements. Heres an example of using comments:
' Program 1: LED ' This program lights up an LED ' Hardware Setup: ' Active-low LED connected to Stamp Pin P7 HIGH 7 PAUSE 500 LOW 7 END ' Turn LED on ' for half second ' Turn LED off
Capitalization
PBASIC is not case-sensitive. PBASIC keywords, variables, and constants can be entered in either upper or lower case, with no change in meaning.
debug DEBUG DeBuG Debug Hello Hello Hello Hello VAR Byte All these commands are interpreted the same
Variables
Variable Sizes
The BASIC Stamp supports four variable types. For the most efficient use of the Stamps memory, a variable should be defined based on program requirements. Using a Word variable, for example, when the value will never exceed 15 is an inefficient use of the Stamps variable memory space.
Type Bit Nib Byte Word Bits 1 4 8 16 Range 0 .. 1 0 .. 15 0 .. 255 0 .. 65,535
Variable Declaration
Variables must be declared before they can be used in a program. All variable declarations are usually placed together at the top of a program. To declare a variable, enter the name of the variable, the keyword VAR, and the size of the variable. Heres an example of declaring several variables:
flag counter status VAR VAR VAR Bit Nib Byte
Shorthand to print x = , value, CR ASCII value! Decimal Indicated Binary, starts with % sign Indicated Hex, starts with $ sign Printing variables along with text DEBUG The temperature is , DEC x, degrees, CR
Debug Formatters
? DEC IHEX IBIN STR ASC Note: Shorthand notation. Prints var = <value>, CR Decimal Indicated hexadecimal Indicated binary String from BYTEARRAY ASCII The default is ASCII! To print a number, you must include a formatter.
Using Variables
x y result x = 25 y = 2 result = x y DEBUG "x y = ", DEC result, CR result = x / y DEBUG "x / y = ", DEC result, CR result = x + y * 10 DEBUG "x + y * 10 = ", DEC result, CR ' Subtraction ' Division ' Order of operations VAR VAR VAR Byte Byte Byte
Output: x y = 23 x / y = 12 x + y * 10 = 14
Constants
Constants are values that cannot change during the execution of program. Named constants will usually assist the reader in understanding the nature of the program and, in many cases, assist the original programmer that has set the program aside for some time. The value of a constant can only be changed by editing the source code.
Constant Declaration
Named constants must be declared before they can be used in a program. All constant declarations are usually placed together at the top of a program. To declare a constant, enter the name of the constant, the keyword CON, and the value of the constant. Heres an example of declaring several constants:
MaxTemp MidPoint CON CON 212 750
Using Constants
ScaleFactor degreeF CON VAR 100 Byte
We call this an unconditional branch because it always happens. GOTO redirects the program to another location. The location is specified as part of the GOTO command and is called an address. Remember that addresses start a line of code and are followed by a colon (:). Youll frequently see GOTO at the end of the main body of code, forcing the program statements to run again. Conditional branching will cause the program flow to change under a specific set of circumstances. The simplest conditional branching is done with IF-THEN construct. The PBASIC IF-THEN construct is different from other flavors of BASIC. In PBASIC, THEN is always followed by a valid program address (other BASICs allow a variety of programming statements to follow THEN). If the condition statement evaluates as TRUE, the program will branch to the address specified. Otherwise, it will continue with the next line of code.
IF-THEN
General format:
Start: statement 1 statement 2 statement 3 IF condition THEN Start
The statements will be run and then the condition is tested. If it evaluates as TRUE, the program will branch back to the line called Start. If the condition evaluates as FALSE, the program will continue at the line that follows the IF-THEN construct.
By using GOTO the statements are unconditionally repeated, or looped. By using IF-THEN, we can add a conditional statement to the loop. The next few examples are called conditional looping. The loops will run under specific conditions. Conditional programming is what gives microcontrollers their smarts.
With this loop structure, statements will be run so long as the condition evaluates as TRUE. When the condition is evaluated as FALSE, the program will continue at the line following the IF-THEN statement. Its important to note that in the previous listing the statements will always run at least once, even if the condition is FALSE.
FOR-NEXT LOOP
The final example of conditional looping is the programmed loop using the FOR-NEXT construct.
FOR controlVar = startVal TO endVal STEP stepSize statement 1 statement 2 statement 3 NEXT
The FOR-NEXT construct is used to cause a section of code to execute (loop) a specific number of times. FOR-NEXT uses a control variable to determine the number of loops. The size of the variable will determine the upper limit of loop iterations. For example, the upper limit when using a byte-sized control variable would be 255.
VAR
FOR counter = 1 TO 5 DEBUG "Loop Number: ", DEC counter DEBUG " Hello World!", CR PAUSE 500 NEXT
Output: Loop Number: Loop Number: Loop Number: Loop Number: Loop Number:
1 2 3 4 5
The STEP option of FOR-NEXT is used when the loop needs to count increments other than one. If, for example, the loop needed to count even numbers, the code would look something like this:
FOR controlVar = 2 TO 20 STEP 2 statement 1 statement 2 statement 3 NEXT
The BS2, BS2e, BS2sx and BS2p have 32 bytes of Variable RAM space. Of these, the first six bytes are reserved for input, output, and direction control of the I/O pins. The remaining 26 bytes are available for general-purpose use as variables.
I/O Registers
Occupy first 3 words RAM (6 bytes) 16-bit registers (Stamp has 16 I/O pins) Are all initialized to zero All pins set to inputs by default
OUTS REGISTER Name OUT0 OUT15 OUTA, OUTB, OUTC, OUTD OUTL, OUTH OUTS
OUTS REGISTER REFERENCE 15 14 13 12 11 OUTA OUTB OUTC OUTD OUTL OUTH OUTS DIRS REGISTER 0 = INPUT, 1 = OUTPUT Name DIR0 .. DIR15 DIRA, DIRB, DIRC, DIRD DIRL, DIRH DIRS
DIRS REGISTER REFERENCE 15 14 13 12 11 DIRA DIRB DIRC DIRD DIRL DIRH DIRS
3. Use HIGH or LOW keywords These set the direction, and write a value
HIGH 5 LOW 3 ' ' Specifies that pin P5 is an output, and sets it high Specifies that pin P3 is an output, and sets it low
4. Use keywords that do it for you No need to use OUTPUT or DIRS first FREQOUT, PULSOUT, SEROUT, ...
Aliases
An alias is an alternate name for an existing variable. One of the most useful applications of aliases is to create alternate names for the Stamp's built-in variable names used for input and output. This can greatly increase a program's readability and understandability. To declare an alias, enter the alias, the keyword VAR, and the name of the existing variable. Heres an example of creating two aliases, called "btn" and "LED", which refer to BASIC Stamp pins P7 and P8, respectively.
btn LED VAR VAR IN7 OUT8 ' name (alias) the input ' name (alias) the output