Programming BS

Embed Size (px)

Citation preview

  • 8/12/2019 Programming BS

    1/11

    Programming with the Basic Stamp Editor

    For further information, refer to Basic Stamp Programming Manual Version 2.0Cand

    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

  • 8/12/2019 Programming BS

    2/11

    - ' {$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 commandsBS1 BS( BS(e BS(ex BS(p BS(pe

    PB"SI# 1%) *(PB"SI# (%) *+ ) ) -. -.PB"SI# (%- ( - - .1 .1

    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 & enhancedsyntax

    #. Defining aria%les

    !&

    Syntax/ name VAR size

    - name/ the name 'y which you will refer to the !aria'le

    he sym'ols must start with a letter of underscore, can contain a

    mixture of letters, num'ers, and underscores%

    Sym'ols can 'e up to *( characters lon$%

    Be not a'le to distin$uish 'etween upper and lower case%

    - Si0e/ there are four choices%

    Bit 1'it2

    &i' ni''le3 'its2

    Byte 4'its2

    5ord 1.'its2

    Examplemouse VAR BIT ' Value can be 0 or 1.

    cat VAR NIB ' Value can be 0 to 15.

    dog VAR BYTE ' Value can be 0 to 255.

    rhino VAR WORD ' Value can be 0 to 65535.

    PDI Studio V (

  • 8/12/2019 Programming BS

    3/11

    Defining !rra's

    "rrays is a $roup of !aria'les of the same si0e, and sharin$ a sin$le name,

    'ut 'roken up into num'ered cells, called elements%

    Syntax/ name VAR Size(n)

    - &ame and si0e/ the same as descri'ed earlier

    - n2 / the num'er of elements you want the array to ha!e

    Example

    myList VAR Byte(10) Create a 10-byte array

    6nce, an array is defined, you can access its elements 'y num'er% &um'erin$

    starts from ) to n71%

    Example

    myList(3) = 57

    DEBUG ? myList(3)

    his #ode will display 8my9ist*2:-+; on the P# screen%

    he real power of arrays is that the index !alue can 'e a !aria'le itself%

    Example

    myBytes VAR Byte(10) ' Define 10-byte array

    idx VAR Nib ' Define 4-bit var

    FOR idx = 0 TO 9 ' Repeat with idx = 0,1, 2...9

    myBytes(idx) = idx * 13 ' Write idx * 13 to each cell

    NEXT

    FOR idx = 0 TO 9 ' Repeat with idx = 0,1, 2...9

    DEBUG ? myBytes(idx) ' Show contents of each cell

    NEXT

    STOP

    If you run this pro$ram, DEB1* : ), myBytes12 : 1>1* : 1*,

    myBytes(2 : (>1* : (. %%% myBytes?2 : ?>1* : 11+%

    "nother uni@ue property of PB"SI# arrays is this/ Aou can refer to the )thcell

    of the array 'y usin$ ust the arrayCs name without an index !alue%

    PDI Studio V *

  • 8/12/2019 Programming BS

    4/11

    Example

    myBytes VAR Byte(10) ' Define 10-byte array

    myBytes(0) = 17 ' Store 17 to 0th cell

    DEBUG ? myBytes(0) ' Display contents of 0th cell

    DEBUG ? myBytes ' Also displays 0th cell

    !(I!S

    "n "lias is an alternati!e name for an existin$ !aria'le%

    Example

    cat VAR Byte ' Create a byte-sized variable

    tabby VAR cat ' 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 "% 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'leCs name to reflect its function in each place%

    #. Defining and )sing Constants

    Aou can assi$n names to constants in a fashion similar to how !aria'les aredeclared%

    Syntax/ Name CON ConstantValue

    Example

    Cheers CON 3 ' Number of cheers.

    FOR counter = 1 TO Cheers

    GOSUB Make_Cheers

    NEXT

  • 8/12/2019 Programming BS

    5/11

    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$ni0es num'ers like ), ?? or .(1- as 'ein$ in our

    e!eryday decimal 'ase71)2 system% Gowe!er, you may also use hexadecimal

    'ase71.3 also called hex2 or 'inary 'ase7(2%

    Example

    99 ' Decimal (no prefix)

    $1A6 ' Hex (prefix $ required)

    %1101 ' 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

    like I&( or 6

  • 8/12/2019 Programming BS

    6/11

    signal PIN 1 ' pin-type symbol representing I/O 1

    INPUT signal ' set signal pin to input

    Wait:

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

    5e only chan$ed one thin$ in this code/ the #6& directi!e was

    chan$ed to PI&% &ow signal is a context7sensiti!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

  • 8/12/2019 Programming BS

    7/11

    S& S+are &oot

    Example

    D!"#$ S5R /00 ' Displa s6uare root of /00 (/0)

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

    SI* Sine

    CS Cosine

    Example

    Der VAR WORD ' Define 7aria1les

    Sine VAR WORD

    8OR Der = 0 O :9 S!P ;: ' #se *erees

    Sine = S3 (Der < /2 > /0) ' %on7ert to 1ra*s? *o S3

    D!"#$ @Anle @? D!% Der? A"? @Sine @? SD!% Sine? %R

    ' Displa

    3!B

    Binar' perators

    K "ddition

    7 Su'traction

    > ultiplication>> ultiplication returns upper 1.7'its2

    >H ultiply 'y 47'it inte$er, 47'it fraction

    H Di!ision

    HH odulus emainder of di!ision2

    I& 9imits a !alue to a specified low

    "L 9imits a !alue to a specified hi$h

    DI= eturns specified di$it of num'er

    MM Shift 'its left 'y specified amount

    NN Shift 'its ri$ht 'y specified amountEV e!erse specified num'er of 'its

    O Bitwise "&D

    Bitwise 6

    Q Bitwise L6

    PDI Studio V +

  • 8/12/2019 Programming BS

    8/11

    3. Categorical Listing Of 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.0Cand BASIC Stamp Syntax and Reference Manual Version 2.1

    B&!*C4I*5 6 P&5&!" C*&(

    B&!*C4Rump to address specified 'y offset%

    I7...4E*> #onditionally execute one or more 'locks of code%

    5Rump to address%

    5S)BRump to su'routine at address%

    &E)&* eturn 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%

    SPGalt pro$ram execution until B"SI# Stamp is reset%

    (PI*5 S&)C)&ESD8(PExecute code 'lock repetiti!ely, with optional, conditional exit%

    E9Ierminate execution of a loopin$ code 'lock D6%%%966P or F6%%%&EL2%

    7&...*E9Execute code 'lock repetiti!ely, a finite num'er of times usin$ a

    counter%

    EEP&" !CCESS

    D!!Store data in EEP6 durin$ pro$ram download%

    &E!Dead EEP6 !alue into !aria'le%

    :&IE5rite !alue into EEP6%S&ESwitch E"DH5IE access to different pro$ram slot%

    &!" !CCESS

    5Eead Scratch Pad " !alue into !aria'le%

    P)5rite !alue into Scratch Pad "%

    *)"E&ICS

    PDI Studio V 4

  • 8/12/2019 Programming BS

    9/11

    (;)P9ook 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$etCs matchin$ !alue in ta'le and store match num'er )7&2

    in !aria'le%

    &!*D"=enerate a pseudo7random num'er%

    DI5I!( I6

    I*P)ake pin an input%

    )P)ake pin an output%

    &EE&SEe!erse direction of a pin%

    (:ake pin output low%

    4I54ake pin output hi$h%

    55(Eake pin an output and to$$le state%

    P)(SI*easure width of an input pulse%

    P)(S)6utput a pulse 'y in!ertin$ a pin for a $i!en amount of time%

    B)*De'ounce 'utton, perform auto7repeat, 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 L71) power line control codes%

    !)9ISwitch control to auxiliary IH6 pin $roup%

    "!I*ISwitch control to main IH6 pin $roup%

    IE&"Switch control to specified IH6 pin $roup%

    P((I* Specify pin and state for a polled7interrupt%

    P(()Specify pin and state for output upon a polledinterrupt%P(("DESpecify the polled7interrupt mode%

    !S

  • 8/12/2019 Programming BS

    10/11

    (CDI*ead data from an 9#D%

    (CD)5rite data to an 9#D%

    !*!(5 I6

    P:"6utput usin$ pulse width modulation, then return pin to input%

    Pead a - k 7 -) k potentiometer and scale result%

    &CI"Eeasure a !aria'le resistance or capacitance%

    I"EE

    P!)SEPause execution for )T.--*- milliseconds%

    P((:!IPause until a polled7interrupt occurs%

    S)*D

    S)*D=enerate tones or white noise%

    7&E)=enerate one or two sine wa!es of specified fre@uencies%

    D"7)=enerate DF telephone tones%

    P:E& C*&(

    *!P&ap for a short period% Power consumption is reduced%

    S(EEPSleep for 17.--*- seconds% Power consumption is reduced%

    E*DSleep until the power cycles or the P# connects% Power consumption is

    reduced%

    P&5&!" DEB)55I*5DEB)5Send information to the P# for !iewin$ in the De'u$ erminalCs ecei!e

    windowpane%

    DEB)5I*etrie!e information from the user !ia the P#, entered into the De'u$

    erminalCs ransmit windowpane%

    S

  • 8/12/2019 Programming BS

    11/11

    F G HT where they appear, commas, 'ackslashes, pound si$ns, and e@ual si$ns

    must 'e typed in the positions shown%

    >>&6E/ Aou may use parentheses to enclose expressions in PB"SI# (%) and

    (%-, 'ut they are not necessary%