You are here: PBASIC Language Reference > PIN Directive

PIN

BS2 icon BS2e icon BS2sx icon BS2p icon BS2pe icon BS2px icon{PBASIC 2.5}

 

This section discusses the PIN directive available with PBASIC 2.5

(For BASIC Stamp I/O pin definitions and ratings, see Pin Descriptions)

(For BS2px I/O pin commands, see COMPARE and CONFIGPIN.)

With the introduction of PBASIC 2.5 syntax comes the PIN directive. The purpose of the PIN directive is to give an alias to an I/O pin and let the compiler determine whether a given line of code requires a numeric value for the pin, the output bit for that pin, or the input bit for that pin. Using the PIN directive simplifies I/O pin aliasing in sophisticated programs.

PIN Syntax

Syntax: PIN_Symbol PINPin_Number

Summary of PIN Behaviors in Context

PIN_Symbol behaves like a constant:

  1. ...when used in a command’s Pin argument. Example:
OUTPUT  PIN_Symbol
  1. ...when used in the index of an array. Example:
myArray(PIN_Symbol) = 25

PIN_Symbol behaves like an input variable (INx):

  1. ...when used in a command’s non-Pin argument that expects to read a variable/constant/expression. Example:
DEBUG BIN PIN_Symbol
  1. ...when used in a command’s Condition argument. Example:
IF PIN_Symbol = 1 THEN…
  1. ...when used to the right of the equal sign (=) in an assignment statement. Example:
myVariable = PIN_Symbol + 1

PIN_Symbol behaves like an output variable (OUTx):

  1. ...when used in a command’s non-Pin argument that expects to write a result to a variable. Example:
LOOKUP index, [0, 1, 1, 0, 1], PIN_Symbol
  1. ...when used to the left of the equal sign (=) in an assignment statement. Example:
PIN_Symbol = 1

PIN Examples

Using the PIN declaration is similar to CON:

' {$PBASIC 2.5}

RedLED  PIN     0                               ' red LED is connected to pin 0

IsOn    CON     1                               ' LED is active high
IsOff   CON     0

Setup:
  OUTPUT RedLED                                 ' same as OUTPUT 0

Main:
  DO
    RedLED = IsOn                               ' same as Out0 = 1
    PAUSE 1000
    RedLED = IsOff                              ' same as Out0 = 0
    PAUSE 1000
  LOOP

Most programs will work like the example above where a given I/O pin will either be an input or an output. There are times, however, when a change in function is required mid program. Prior to PBASIC 2.5, programmers wishing to use named constants were forced to create redundant definitions or complicated modifiers. For example:

' {$PBASIC 2.0}

SDA     CON     8                               ' I2C data I/O
SCL     CON     9                               ' I2C clock output

...

I2C_Start:
  INPUT SDA
  INPUT SCL
  LOW SDA

Clock_Hold:                                     ' monitor device clock hold
  IF (INS.LOWBIT(SCL) = 0) THEN Clock_Hold      ' <-- tricky modifiers
  RETURN

With PBASIC 2.5 the tricky modifiers can be eliminated:

' {$PBASIC 2.5}

SDA     PIN     8                               ' I2C data I/O
SCL     PIN     9                               ' I2C clock output

...

I2C_Start:
  INPUT SDA
  INPUT SCL
  LOW SDA

Clock_Hold:                                     ' monitor device clock hold
  DO : LOOP WHILE (SCL = 0)                     ' easier, same as "IN9 = 0"
  RETURN

The compiler is "smart" and knows how to replace a PIN directive with a constant value, an INS register bit or and OUTS register bit. Note that as in the example above, the programmer is still responsible for setting the DIRS bit of the I/O pin as required by the program.

 

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012