You are on page 1of 11

Programming with the Basic Stamp Editor

For further information, refer to Basic Stamp Programming Manual Version 2.0C and BASIC Stamp Syntax and Reference Manual Version 2.1

1. Software Interface
Software interface is similar to other kinds of editors, and there are little to know to use the Basic Stamp Editor

2. Compiler Directives
$S !"P Syntax - ' {$STAMP BS1} 'This indicates to use a BASIC Stamp 1 module - ' {$STAMP BS2} 'This indicates to use a BASIC Stamp 2 module - ' {$STAMP BS2e} 'This indicates to use a BASIC Stamp 2e module PDI Studio V 1

- ' {$STAMP BS2sx} 'This indicates to use a BASIC Stamp 2sx module - ' {$STAMP BS2p} 'This indicates to use a BASIC Stamp 2p module - ' {$STAMP BS2pe} 'This indicates to use a BASIC Stamp 2pe module $PB!SIC o allow you to indicate which !ersion of the PB"SI# lan$ua$e to use% &um'er of a!aila'le commands BS1 BS( BS(e BS(ex *( *+ ,) ,) ,( ,,BS(p -. .1 BS(pe -. .1

PB"SI# 1%) PB"SI# (%) PB"SI# (%-

Syntax - ' {$PBASIC 1.0} 'Default when a BASIC Stamp 1 module is detected - ' {$PBASIC 2.0} 'Default when any BASIC Stamp 2 module is detected - ' {$PBASIC 2.5} 'Required for PBASIC 2.5 command set & enhanced
syntax

#. Defining $aria%les
$!& VAR size Syntax/ name - name/ the name 'y which you will refer to the !aria'le 0 he sym'ols must start with a letter of underscore, can contain a mixture of letters, num'ers, and underscores% 0 Sym'ols can 'e up to *( characters lon$% 0 Be not a'le to distin$uish 'etween upper and lower case% - Si1e/ there are four choices% 0 Bit 21'it3 0 &i' 2ni''le4 ,'its3 0 Byte 25'its3 0 6ord 21.'its3 Example
mouse cat dog rhino VAR VAR VAR VAR BIT NIB BYTE WORD ' ' ' ' Value Value Value Value can can can can be be be be 0 0 0 0 or to to to 1. 15. 255. 65535.

PDI Studio V

Defining !rra's "rrays is a $roup of !aria'les of the same si1e, and sharin$ a sin$le name, 'ut 'roken up into num'ered cells, called elements% VAR Size(n) Syntax/ name - &ame and si1e/ the same as descri'ed earlier - 2n3 / the num'er of elements you want the array to ha!e Example
myList VAR Byte(10) Create a 10-byte array

7nce, an array is defined, you can access its elements 'y num'er% &um'erin$ starts from ) to n81% Example
myList(3) = 57 DEBUG ? myList(3)

his #ode will display 9my:ist2*3;-+< on the P# screen% he real power of arrays is that the index !alue can 'e a !aria'le itself%

Example
myBytes idx VAR Byte(10) VAR FOR idx = 0 TO 9 myBytes(idx) = idx * 13 NEXT FOR idx = 0 TO 9 DEBUG ? myBytes(idx) NEXT STOP ' Repeat with idx = 0,1, 2...9 ' Show contents of each cell Nib ' Define 10-byte array ' Define 4-bit var ' Repeat with idx = 0,1, 2...9 ' Write idx * 13 to each cell

If you run this pro$ram, DEB=> will display each of the 1) !alues stored in the elements of the array/ myBytes2)3 ; )?1* ; ), myBytes213 ; 1?1* ; 1*, myBytes2(3 ; (?1* ; (. %%% myBytes2@3 ; @?1* ; 11+% "nother uniAue property of PB"SI# arrays is this/ Bou can refer to the ) th cell of the array 'y usin$ Cust the arrayDs name without an index !alue% PDI Studio V *

Example
myBytes VAR Byte(10) ' Define 10-byte array ' Store 17 to 0th cell ' Display contents of 0th cell ' Also displays 0th cell myBytes(0) = 17 DEBUG ? DEBUG ? myBytes(0) myBytes

!(I!S "n "lias is an alternati!e name for an existin$ !aria'le% Example cat tabby

VAR VAR

Byte cat

' Create a byte-sized variable ' Create alias for cat

In this example, tabby is an alias to the !aria'le cat% "nythin$ stored in cat shows up in tabby and !ice !ersa% Both names refer to the same physical piece of E"F% his kind of alias can 'e useful when you want to reuse a temporary !aria'le in different places in your pro$ram, 'ut also want the !aria'leDs name to reflect its function in each place%

#. Defining and )sing Constants


Bou can assi$n names to constants in a fashion similar to how !aria'les are declared% CON ConstantValue Syntax/ Name Example
Cheers CON 3 ' Number of cheers. FOR counter = 1 TO Cheers GOSUB Make_Cheers NEXT

=sin$ named constants does not increase the amount of code downloaded to the B"SI# Stamp, and it often impro!es the clarity of the pro$ram% 6eeks after a pro$ram is written, you may not remem'er what a particular num'er was supposed to representGusin$ a name may Co$ your memory 2or simplify the detecti!e work needed to fi$ure it out3% *+m%er Presentation PDI Studio V ,

In your pro$rams, you may express a num'er in !arious ways, dependin$ on how the num'er will 'e used and what makes sense to you% By default, the B"SI# Stamp reco$ni1es num'ers like ), @@ or .(1,- as 'ein$ in our e!eryday decimal 2'ase81)3 system% Howe!er, you may also use hexadecimal 2'ase81.4 also called hex3 or 'inary 2'ase8(3% Example
99 $1A6 %1101 ' Decimal (no prefix) ' Hex (prefix $ required) ' Binary (prefix % required)

,. Defining and )sing Pins with the Pin Directives


here are some situations where it is handy to refer to a pin usin$ a !aria'le 2like I&( or 7= (3 and also as a constant 2(, in this case3% he PI& directi!e lets you define a context8sensiti!e sym'ol representin$ an II7 pin% Dependin$ on where and how this pin8type sym'ol is used determines whether it is treated as an II7 pin input !aria'le, and II7 pin output !aria'le or as a constant representin$ the pin num'er% If we were to use a constant sym'ol to represent an II7 pin, we mi$ht do somethin$ like this/
signal INPUT Wait: IF signal = 0 THEN Wait ' wait until signal pin = 1 CON signal 1 ' constant-type symbol representing I/O 1 ' set signal pin to input

- Here we define signal to represent our desired II7 pin, then we use the I&P= command to set it to the input direction and later we check the state of the si$nal pin and loop 2wait3 while it is eAual to lo$ic )% his code has a common 'u$, howe!er4 the I&P= command works as expected, 'ecause its Pin ar$ument reAuires a num'er representin$ the II7 pin, 'ut the Condition ar$ument in the IFJ HE& statement will always e!aluate to false 'ecause signal is a constant eAual to 1, and 91 ; )< is false% 6hat the user really meant to happen is somethin$ like/ IF I&1 ; ) HE& 6ait 'ecause IN1 is the input !aria'le representin$ the current state of II7 pin 1% his situation is perfect for the PI& directi!e/

PDI Studio V

signal INPUT signal Wait:

PIN

' pin-type symbol representing I/O 1 ' set signal pin to input

IF signal = 0 THEN Wait ' wait until signal = 1

6e only chan$ed one thin$ in this code/ the #7& directi!e was chan$ed to PI&% &ow signal is a context8sensiti!e sym'ol that will 'e treated as a constant or as a !aria'le dependin$ on where it is used% In the I&P= command signal is used as the Pin ar$ument, and since that ar$ument reAuires a num'er representin$ the pin num'er, signal is treated as a constant eAual to 1% In the IFJ HE& statement, signal is compared to another !alue 2which implies that what signal represents is expected to chan$e at run8time4 i%e%/ signal s !alue is 9!aria'le<3 so signal is treated as a !aria'le eAual to the input !aria'le for the defined pin 2I&1 in this case3%

-. "ath and .perators


!BS !%sol+te $al+e

Example
Result VAR WORD Result = -99 D!"#$ SD!% & Result ' Put -99 (2's complement format) into Result ' Displa' it on t(e screen as a si)ne* +

D!"#$ SD!% & A"S Result ' Displa' it on t(e screen as a si)ne* +

I*$E&SE he In!erse operator 2K3 complements 2in!erts3 the 'its of a num'er% Each 'it that contains a 1 is chan$ed to ) and each 'it containin$ ) is chan$ed to 1%

Example
Result VAR ",-! Result = .////000/ D!"#$ "23 & Result Result = 4 Result D!"#$ "23 & Result ' Store 1its in 1'te Result ' Displa' in 1inar' (.////000/) ' %omplement Result ' Displa' in 1inar' (.0000///0)

*egative PDI Studio V .

S1&

S2+are &oot

Example
D!"#$ S5R /00 D!"#$ S5R 99 ' Displa' s6uare root of /00 (/0) ' Displa' of s6uare root of 99 (9 *ue to truncation)

SI* C.S

Sine Cosine

Example
De)r VAR WORD Sine VAR WORD 8OR De)r = 0 -O 9:9 S-!P ;: ' #se *e)rees Sine = S23 (De)r < /2= > /=0) ' %on7ert to 1ra*s? *o S23 D!"#$ @An)leA @? D!% De)r? -A"? @SineA @? SD!% Sine? %R ' Displa' 3!B' Define 7aria1les

Binar' .perators L 8 ? ?? ?I I II FI& F"M DI> NN OO EEV P Q R "ddition Su'traction Fultiplication Fultiplication 2returns upper 1.8'its3 Fultiply 'y 58'it inte$er, 58'it fraction Di!ision Fodulus 2Eemainder of di!ision3 :imits a !alue to a specified low :imits a !alue to a specified hi$h Eeturns specified di$it of num'er Shift 'its left 'y specified amount Shift 'its ri$ht 'y specified amount Ee!erse specified num'er of 'its Bitwise "&D Bitwise 7E Bitwise M7E

PDI Studio V

3. Categorical (isting .f Commands


his section lists all a!aila'le PB"SI# commands for all B"SI# Stamp models, $rouped 'y cate$ory% For the detailed usa$e, refer to the Basic Stamp Programming Manual Version 2.0C and BASIC Stamp Syntax and Reference Manual Version 2.1

B&!*C4I*5 6 P&.5&!" C.* &.( B&!*C4 Sump to address specified 'y offset% I7... 4E*? #onditionally execute one or more 'locks of code% 5. . Sump to address% 5.S)B Sump to su'routine at address% &E )&* Eeturn from su'routine% &)* Switch execution to another pro$ram slot% P.((&)* Switch execution to another pro$ram pa$e upon the occurrence of a polled interrupt% SE(EC 8C!SE2.- E!aluate expression and conditionally execute a 'lock of code 'ased on comparison to multiple conditions% S .P Halt pro$ram execution until B"SI# Stamp is reset% (..PI*5 S &)C )&ES D.8(..P Execute code 'lock repetiti!ely, with optional, conditional exit% E9I erminate execution of a loopin$ code 'lock 2D7%%%:77P or F7E%%%&EM 3% 7.&...*E9 Execute code 'lock repetiti!ely, a finite num'er of times usin$ a counter% EEP&." !CCESS D! ! Store data in EEPE7F durin$ pro$ram download% &E!D Eead EEPE7F !alue into !aria'le% :&I E 6rite !alue into EEPE7F% S .&E Switch EE"DI6EI E access to different pro$ram slot% &!" !CCESS 5E Eead Scratch Pad E"F !alue into !aria'le% P) 6rite !alue into Scratch Pad E"F% *)"E&ICS PDI Studio V 5

(..;)P :ook up data specified 'y offset and store in !aria'le% his instruction pro!ides a means to make a lookup ta'le% (..;D.:* Find tar$etDs matchin$ !alue in ta'le and store match num'er 2)8&3 in !aria'le% &!*D." >enerate a pseudo8random num'er% DI5I !( I6. I*P) Fake pin an input% .) P) Fake pin an output% &E$E&SE Ee!erse direction of a pin% (.: Fake pin output low% 4I54 Fake pin output hi$h% .55(E Fake pin an output and to$$le state% P)(SI* Feasure width of an input pulse% P)(S.) 7utput a pulse 'y in!ertin$ a pin for a $i!en amount of time% B) .* De'ounce 'utton, perform auto8repeat, and 'ranch to address if 'utton is in tar$et state% C.)* #ount cycles on a pin for a $i!en amount of time% 9.) >enerate M81) power line control codes% !)9I. Switch control to auxiliary II7 pin $roup% "!I*I. Switch control to main II7 pin $roup% I. E&" Switch control to specified II7 pin $roup% P.((I* Specify pin and state for a polled8interrupt% P.((.) Specify pin and state for output upon a polledinterrupt% P.((".DE Specify the polled8interrupt mode% !S<*C4&.*.)S SE&I!( I6. SE&I* Input data in an asynchronous serial stream% SE&.) 7utput data in an asynchronous serial stream% .:I* Input data from a 18wire de!ice% .:.) 7utput data to a 18wire de!ice% S<*C4&.*.)S SE&I!( I6. S4I7 I* Shift data in from synchronous serial de!ice% S4I7 .) Shift data out to synchronous serial de!ice% I2CI* Input data from I(# serial de!ice% I2C.) 7utput data to I(# serial de!ice% P!&!((E( I6. (CDC"D 6rite a command to an :#D% PDI Studio V @

(CDI* Eead data from an :#D% (CD.) 6rite data to an :#D% !*!(.5 I6. P:" 7utput usin$ pulse width modulation, then return pin to input% P. Eead a - kT 8 -) kT potentiometer and scale result% &C I"E Feasure a !aria'le resistance or capacitance% I"EE P!)SE Pause execution for )U.--*- milliseconds% P.((:!I Pause until a polled8interrupt occurs% S.)*D S.)*D >enerate tones or white noise% 7&E1.) >enerate one or two sine wa!es of specified freAuencies% D "7.) >enerate D FF telephone tones% P.:E& C.* &.( *!P &ap for a short period% Power consumption is reduced% S(EEP Sleep for 18.--*- seconds% Power consumption is reduced% E*D Sleep until the power cycles or the P# connects% Power consumption is reduced% P&.5&!" DEB)55I*5 DEB)5 Send information to the P# for !iewin$ in the De'u$ erminalDs Eecei!e windowpane% DEB)5I* Eetrie!e information from the user !ia the P#, entered into the De'u$ erminalDs ransmit windowpane% S<* !9 C.*$E* I.*S B.(D )PPE& C!SE U any word that appears 'old with all capital letters must 'e typed exactly as shown% hese are all reser!ed words% Italics = italici1ed words must 'e replaced with your content% > ? U sAuare 'rackets must 'e typed, in the position shown around the $i!en syntax element% 7nly used with PB"SI# (%) and (%-% @ A U parentheses must 'e typed in the position shown around the $i!en syntax element4 only used this way with PB"SI# 1%)%?? B C U curly 'races indicate optional syntax items% hey are not typed as part of any syntax other than compiler directi!es% D U !ertical line separates mutually exclusi!e syntax elements% PDI Studio V 1)

E F G H U where they appear, commas, 'ackslashes, pound si$ns, and eAual si$ns must 'e typed in the positions shown% ??&7 E/ Bou may use parentheses to enclose expressions in PB"SI# (%) and (%-, 'ut they are not necessary% =sed within an expression, parentheses will chan$e the order of e!aluation% See pa$e 1)) for details and examples%

PDI Studio V

11

You might also like