Workshops | InterAccess

BASIC Stamp 2
Serial Port
RS-232
(Serial Out) Sout
1
24
Vin (+5.5 to +15 volts)
(Serial In) Sin
2
23
Vss (Ground)
(Attention) ATN
(Ground) Vss
3
22
4
21
RESET
Vdd (+5v regulated)
P0
5
20
P15
P1
6
19
P14
P2
7
18
P13
P3
8
17
P12
P4
9
16
P11
P5
10
15
P10
P6
11
14
P9
P7
12
13
P8
Input / Output
Execution Speed:
RAM Storage:
EEPROM (Program) Storage:
Voltage:
Input / Output
approx. 4,000 instructions/sec
32 Bytes (6 Bytes for I/O, 26 Bytes for variables)
2K Bytes (approx. 500 instructions)
5 to15 Vdc
Source/Sink Current per pin:
20 mA
Source/Sink Current per unit:
40 ma
PC Interface Serial:
Power
RS-232 (9600 baud)
Basic Connections
for the BASIC Stamp
Rx
Tx
DTR
0.1 µF
1
S OUT
V IN
24
+6 to +15 volts
2
S IN
V SS
23
Ground
3
ATN
RESET
22
4
V SS
V DD
21
0.1 µF
+5v
+5v
Ground
1
5
6
BS2
Basic Stamp
DB-9 Female
(back view)
9
+5v
Pushbutton
12
P7
10 K
P11
16
P10
15
P9
14
P8
13
Reset
10 K
LEDs
all 470 Ω
Introduction to the BASIC Stamp
Stamp 2 OEM is available in either an assembled form or a kit form. These
three packages are functionally equivalent.
In addition to the dual-inline and OEM packages, there are prototyping
boards available that feature a surface mounted BS2. Please check
www.parallax.com → Products → Development Boards for product
descriptions.
Pin
Name
1
SOUT
2
SIN
3
ATN
4
VSS
5-20
P0-P15
21
VDD
22
RES
23
VSS
24
VIN
Description
Serial Out: connects to PC serial port RX pin (DB9 pin 2 / DB25
pin 3) for programming.
Serial In: connects to PC serial port TX pin (DB9 pin 3 / DB25 pin
2) for programming.
Attention: connects to PC serial port DTR pin (DB9 pin 4 / DB25
pin 20) for programming.
System ground: (same as pin 23) connects to PC serial port GND
pin (DB9 pin 5 / DB25 pin 7) for programming.
General-purpose I/O pins: each can sink 25 mA and source 20
mA. However, the total of all pins should not exceed 50 mA (sink)
and 40 mA (source) if using the internal 5-volt regulator. The total
per 8-pin groups (P0 – P7 or P8 – 15) should not exceed 50 mA
(sink) and 40 mA (source) if using an external 5-volt regulator.
5-volt DC input/output: if an unregulated voltage is applied to the
VIN pin, then this pin will output 5 volts. If no voltage is applied to
the VIN pin, then a regulated voltage between 4.5V and 5.5V
should be applied to this pin.
Reset input/output: goes low when power supply is less than
approximately 4.2 volts, causing the BASIC Stamp to reset. Can
be driven low to force a reset. This pin is internally pulled high
and may be left disconnected if not needed. Do not drive high.
System ground: (same as pin 4) connects to power supply’s
ground (GND) terminal.
Unregulated power in: accepts 5.5 - 15 VDC (6-40 VDC on BS2IC Rev. e, f, and g), which is then internally regulated to 5 volts.
Must be left unconnected if 5 volts is applied to the VDD (+5V)
pin.
See the "BASIC Stamp Programming Connections" section on page 27 for
more information on the required programming connections between the
PC and the BASIC Stamp.
Page 14 • BASIC Stamp Syntax and Reference Manual 2.2 • www.parallax.com
Table 1.2: BASIC Stamp 2 Pin
Descriptions.
MEMORY ORGANIZATION – Basic Stamp 2
word
The Basic Stamp 2 has 32 bytes of working memory. This is
not much when compared to the megabyte memories of
desktop computers – but used carefully it’s good enough for
our purposes.
byte
nibble
bit
INS 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0
OUTS 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0
DIRS 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
W0 1 0 0 1 0 1 1 1 0 0 0 0 0 0 0 0
W1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0
W2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
W3 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0
W4 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
W5 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0
W6 0 1 0 0 0 0 1 0 1 1 0 0 0 0 0 0
W7 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
W8 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0
W9 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
It is Random Access Memory (RAM) which means that it is fast
for reading and writing. (There another much larger but
slower memory available on the Stamp besides RAM.)
The memory is organized in 16-bit words. A word is broken
down into progressively smaller parts: a byte (8-bits), a nibble
(4-bits) and the single bit.
In order to make the most of limited RAM, the Stamp allows
memory to be addressed at the level of bits, nibbles, bytes or
words.
Low-order bits, nibbles and bytes are on the right hand-side
and high-order on the left. (This corresponds to the numbers
we are already familiar with – decimal numbers. For example,
in the number ‘2005’, ‘2’ is the high-order digit and ‘5’ is the
low-order digit.
W10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Input/Output Memory-Mapping
W11 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0
The first three words, INS, OUTS and DIRS have a special
behaviour. They are memory-mapped to the input/output
pins P0-P15.
W12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
15
11
7
3
0
high-order low-order
The first word, INS, will always contain the current state of
the pins P0-P15. Reading the bit values in this word will
whether a given pin is voltage HIGH or voltage LOW. This
word is read-only.
input
P15
P0
INS 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0
OUTS 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0
P15
output
P0
The second word, OUTS, contains the output values destined
for the pins P0-P15. To bring an output pin HIGH it is
necessary to write a ‘1’ to the corresponding bit in OUTS. This
word is read-write.
The third and final memory mapped word is DIRS and it
controls the direction of data flow on the I/O pins. The pins
P0-P15 can be used for either input or output at any given
moment of time, and this word specifies the direction of each
pin. When a bit is set to ‘0’ the corresponding pin acts as an
input. Setting the bit to ‘1’ will make the pin an output. A pin
can be changed from an input to output and back on-the-fly,
under program control.
W0–W12: Working Memory
DIRS 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
direction
0 = input
1 = output
The remaining 13 words, W0 to W12 are available to the
programmer for storing variables. It is best to let the compiler
determine the actual storage location by using the VAR
command. (e.g: “fooBar VAR Byte”).
IN0
IN1
IN2
IN3
IN4
IN5
IN6
IN7
IN8
IN9
OUT0
OUT1
OUT2
OUT3
OUT4
OUT5
OUT6
OUT7
OUT8
OUT9
OUT10 OUT11 OUT12 OUT13 OUT14 OUT15
DIR0
DIR1
DIR2
DIR3
DIR4
DIR5
DIR6
DIR7
DIR8
DIR9
DIR10
INA
INB
INC
IND
OUTA
OUTB
OUTC
OUTD
DIRA
DIRB
DIRC
DIRD
IN10
IN11
DIR11
IN12
DIR12
IN13
DIR13
IN14
DIR14
IN15
DIR15
4: BASIC Stamp Architecture – Memory Organization
the variable RAM for these models, only the BS2p40 module has the extra
16 I/O pins for which this feature is intended.
THE INPUT/OUTPUT VARIABLES.
The word variable INS is unique in that it is read-only. The 16 bits of INS
reflect the state of I/O pins P0 through P15. It may only be read, not
written. OUTS contains the states of the 16 output latches. DIRS controls
the direction (input or output) of each of the 16 I/O pins.
A 0 in a particular DIRS bit makes the corresponding pin an input and a 1
makes the corresponding pin an output. So if bit 5 of DIRS is 0 and bit 6 of
DIRS is 1, then I/O pin 5 (P5) is an input and I/O pin 6 (P6) is an output.
A pin that is an input is at the mercy of circuitry outside the BASIC Stamp;
the BASIC Stamp cannot change its state. A pin that is an output is set to
the state indicated by the corresponding bit of the OUTS register.
When the BASIC Stamp is powered up, or reset, all memory locations are
cleared to 0, so all pins are inputs (DIRS = %0000000000000000). Also, if
the PBASIC program sets all the I/O pins to outputs (DIRS =
%1111111111111111), then they will initially output low, since the output
latch (OUTS) is cleared to all zeros upon power-up or reset, as well.
Table 4.2: RAM Organization for
all BS2 models.
NOTE: There are 16 words, of
two bytes each for a total of 32
bytes*. All bits are individually
addressable through variable
modifiers; the bits within the
upper three words are also
individually addressable though
the pre-defined names shown.
All registers are word, byte,
nibble and bit addressable.
*The BS2p, BS2pe, and BS2px
have an additional set of INS,
OUTS, and DIRS registers that
are switched in and out of the
memory map in place of the main
INS, OUTS, and DIRS registers
by using AUXIO, MAINIO, and
IOTERM. Only the BS2p40 has
the required extra I/O pins this
feature is intended for.
Word Name
INS*
OUTS*
DIRS*
W0
W1
W2
W3
W4
W5
W6
W7
W8
W9
W10
W11
W12
Byte Names
INL, INH
OUTL, OUTH
DIRL, DIRH
Nibble Names
INA, INB
INC, IND
OUTA, OUTB
OUTC, OUTD
DIRA, DIRB
DIRC, DIRD
Bit Names
IN0 – IN7
IN8 – IN15
OUT0 – OUT7
OUT8 – OUT15
DIR0 – DIR7
DIR8 – DIR15
Special Notes
Input pins
Output pins
I/O pin direction control
B0, B1
B2, B3
B4, B5
B6, B7
B8, B9
B10, B11
B12, B13
B14, B15
B16, B17
B18, B19
B20, B21
B22, B23
B24, B25
BASIC Stamp Syntax and Reference Manual 2.2 • www.parallax.com • Page 83
PROGRAM FLOW – STRUCTURES
1. Simple Block
statement 1
statement 2
statement 3
statement 1
statement 2
statement 3
2. Conditional (IF)
False
A?
True
IF (condition A) THEN
statement 1
statement 2
statement 3
ENDIF
statement 1
statement 2
statement 3
3. Looping (FOR...NEXT)
A?
False
True
statement 1
statement 2
statement 3
DO WHILE (condition A)
statement 1
statement 2
statement 3
LOOP
FOR counter = n to m
statement 1
statement 2
statement 3
NEXT
PROGRAM FLOW – STRUCTURES – page 2
3. Branching (IF...ELSE)
False
A?
IF (condition A) THEN
statement 1
statement 2
statement 3
ELSE
statement 4
statement 5
statement 6
ENDIF
True
statement 1
statement 2
statement 3
statement 4
statement 5
statement 6
4. Nested Branching (IF...ELSE...IF...ELSE)
False
A?
True
B?
False
True
statement 1
statement 2
statement 3
statement 4
statement 5
statement 6
statement 7
statement 8
statement 9
IF (condition A) THEN
statement 1
statement 2
statement 3
ELSE
IF (condition B) THEN
statement 4
statement 5
statement 6
ELSE
statement 7
statement 8
statement 9
ENDIF
ENDIF
5. Nested Branching – Simplified (IF...ELSEIF...ELSE)
A?
False
True
B?
False
True
C?
False
True
statement 1
statement 2
statement 3
statement 4
statement 5
statement 6
statement 7
statement 8
statement 9
statement 10
statement 11
statement 12
IF (condition A) THEN
statement 1
statement 2
statement 3
ELSEIF (condition B) THEN
statement 4
statement 5
statement 6
ELSEIF (condition C) THEN
statement 7
statement 8
statement 9
ELSE
statement 10
statement 11
statement 12
ENDIF
Programming Essentials
Programming Essentials
Contents of a Working Program
In Section 1 of the BASIC Stamp II manual you were introduced to the BASIC Stamp, its architecture
and the concepts of variables and constants. In this section, we’ll introduce the various elements of a
program: linear code, branching, loops and subroutines.
The examples in this discussion use pseudo-code to demonstrate and describe program structure.
Italics are used to indicate the sections of pseudo-code that require replacement with valid
programming statements in order to allow the example to compile and run correctly. You need not
enter any of the examples here as all of these concepts will be used in the experiments that follow.
People 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 Parallaxspecific 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:
statement 1
statement 2
statement 3
END
This is a very simple, yet valid program structure. What you’ll find, however, is that most programs
do not run in a straight, linear fashion like the listing above. Program flow is often redirected with
branching, looping and subroutines, with short linear sections in between. The requirements for
program flow are determined by the goal of the program and the conditions under which the
program is running.
StampWorks Manual Version 1.2 • Page 13
Programming Essentials
Branching – Redirecting the Flow of a Program
A branching command is one that causes the flow of the program to change from its linear path. In
other words, when the program encounters a branching command, it will, in almost all cases, not be
running the next [linear] line of code. The program will usually go somewhere else. There are two
categories of branching commands: unconditional and conditional. PBASIC has two commands, GOTO
and GOSUB that cause unconditional branching.
Here’s an example of an unconditional branch using GOTO:
Label:
statement 1
statement 2
statement 3
GOTO Label
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 (:). You’ll 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.
Take a look at this listing:
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.
Page 14 • StampWorks Manual Version 1.2
Programming Essentials
As your requirements become more sophisticated, you’ll find that you’ll want your program to branch
to any number of locations based on a condition. One approach is to use multiple IF-THEN
constructs.
IF (condition_0) THEN Label_0
IF (condition_1) THEN Label_1
IF (condition_2) THEN Label_2
This approach is valid and does get used. Thankfully, PBASIC has a special command, BRANCH, that
allows a program to jump to any number of addresses based on the value of a variable. This is very
handy because the conditions we’ve referred to in the text are often checking the value of a control
variable. BRANCH is a little more complicated in its setup, but very powerful in that it can replace
multiple IF-THEN statements. BRANCH requires a control variable and a list of addresses
In the case of a single control variable, the previous listing can be replaced with one line of code:
BRANCH controlVar, [Label_0, Label_1, Label_2]
When controlVar is zero, the program will branch to Label_0, when controlVar is one the
program will branch to Label_1 and so on.
Looping – Running Code Again and Again
Looping causes sections of the program to be repeated. Looping often uses unconditional and
conditional branching to create the various looping structures. Here’s an example of unconditional
looping:
Label:
statement 1
statement 2
statement 3
GOTO Label
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.”
StampWorks Manual Version 1.2 • Page 15
Programming Essentials
Label:
statement 1
statement 2
statement 3
IF (condition) THEN Label
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. It’s important to note that in the previous listing the statements will always run at least
once, even if the condition is FALSE.
To prevent this from taking place, you need to test the condition before running the statements. The
code can be written as follows so that the statements (1 – 3) will only run when the condition is
TRUE. When the condition evaluates as FALSE, the program continues at Label_2.
Label_1:
IF NOT (condition) THEN Label_2
statement 1
statement 2
statement 3
GOTO Label_1
Label_2:
statement 4
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.
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:
Page 16 • StampWorks Manual Version 1.2
Programming Essentials
FOR controlVar = 2 TO 20 STEP 2
statement 1
statement 2
statement 3
NEXT
Subroutines – Reusable Code that Saves Program Space
The final programming concept we’ll discuss is the subroutine. A subroutine is a section of code that
can be called (run) from anywhere in the program. GOSUB is used to redirect the program to the
subroutine code. The subroutine is terminated with the RETURN command. RETURN causes the
program to jump back to the line of code that follows the calling GOSUB command.
Start:
GOSUB MySub
PAUSE 1000
GOTO Start
MySub:
statement 1
statement 2
statement 3
RETURN
In this example, the code in the MySub is executed and then the program jumps back to the line
PAUSE 1000.
StampWorks Manual Version 1.2 • Page 17
Programming Essentials
The Elements of PBASIC Style
Like most versions of the BASIC programming language, PBASIC is very forgiving and the compiler
enforces no particular formatting style. So long as the source code is syntactically correct, it will
compile and download to the Stamp without trouble.
Why, then, would one suggest a specific style for PBASIC? Consider this: Over two million BASIC
Stamps have been sold and there are nearly 2500 members of the BASIC Stamp mailing list (on
Yahoo! Groups). This makes it highly likely that you'll be sharing your PBASIC code with someone, if
not co-developing a BASIC Stamp-oriented project. Writing code in an organized, predictable manner
will save you – and your potential colleagues – time; in analysis, in troubleshooting and especially
when you return to a project after a long break.
The style guidelines presented here are just that: guidelines. They have been developed from style
guidelines used by professional programmers using other high-level languages such as Java™,
C/C++ and Visual Basic®. Use these guidelines as is, or modify them to suit your needs. The key is
selecting a style the works well for you or your organization and sticking to it.
1. Do It Right The First Time
Many programmers, especially new ones, fall into the "I'll slug it out now and fix it later." trap.
Invariably, the "fix it later" part never seems to happen and sloppy code makes its way into
production projects. If you don't have time to do it right, when will you have time to do it again?
Start clean and you'll be less likely to introduce errors in your code. And if errors do pop up,
clean formatting will make them easier to find and fix.
2. Be Organized and Consistent
Using a blank program template will help you organize your programs and establish a consistent
presentation.
3. Use Meaningful Names
Be verbose when naming constants, variables and program labels. The compiler will allow names
up to 32 characters long. Using meaningful names will reduce the number of comments and
make your programs easier to read, debug and maintain.
Page 18 • StampWorks Manual Version 1.2
Programming Essentials
4. Naming Constants
Begin constant names with an uppercase letter and use mixed case, using uppercase letters at
the beginning of new words within the name:
AlarmCode
CON
25
5. Naming Variables
Begin variable names with a lowercase letter and use mixed case, using uppercase letters at the
beginning of new words within the name. Avoid the use of internal variable names (such as B0
or W1):
waterLevel
VAR
Word
6. Naming Program Labels
Begin program labels with an uppercase letter, used mixed case, separate words within the label
with an underscore character and begin new words with an uppercase letter. Labels should be
preceded by at least one blank line, begin in column 1 and be terminated with a colon (except
after GOTO and THEN where they appear at the end of the line and without a colon):
Print_String:
READ eeAddr, char
IF (char = 0) THEN Print_String_Exit
DEBUG char
eeAddr = eeAddr + 1
GOTO Print_String
Print_String_Exit:
RETURN
StampWorks Manual Version 1.2 • Page 19
Programming Essentials
7. PBASIC Keywords
All PBASIC language keywords, including VAR, CON and serial/debugging format modifiers (DEC,
HEX, BIN) should be uppercase:
Main:
DEBUG "BASIC Stamp", CR
END
8. Variable Types
Variable types should be be in mixed case and start with an uppercase letter:
status
counter
ovenTemp
rcValue
VAR
VAR
VAR
VAR
Bit
Nib
Byte
Word
9. Indent Nested Code
Nesting blocks of code improves readability and helps reduce the introduction of errors.
Indenting each level with two spaces is recommended to make the code readable without taking
up too much space:
Main:
..FOR outerLoop = 1 TO 10
....FOR innerLoop = 1 TO 10
......DEBUG DEC outerLoop, TAB, DEC innerLoop, TAB
......DEBUG DEC (outerLoop * innerLoop)
......PAUSE 100
....NEXT
..NEXT
Note: The dots are used to illustrate the level of nesting and are not a part of the code.
Page 20 • StampWorks Manual Version 1.2
Programming Essentials
10. Be Generous With Whitespace
Whitespace (spaces and blank lines) has no effect compiler or BASIC Stamp performance, so be
generous with it to make listings easier to read. As suggested in #6 above, allow at lease one
blank line before program labels (two blanks lines before a subroutine label is recommended).
Separate items in a parameter list with a space:
Main:
BRANCH task, [Update_Motors, Scan_IR, Close_Gripper]
GOTO Main
Update_Motors:
PULSOUT leftMotor, leftSpeed
PULSOUT rightMotor, rightSpeed
PAUSE 20
Task = (task + 1) // NumTasks
GOTO Main
An exception to this guideline is with the bits parameter used with SHIFTIN and SHIFTOUT. In
this case, format without spaces:
SHIFTIN A2Ddata, A2Dclock, MSBPost, [result\9]
11. IF-THEN Conditions
Enclose IF-THEN condition statements in parenthesis:
Check_Temp:
IF (indoorTemp >= setPoint) THEN AC_On
The StampWorks files (available for download fromwww.parallaxinc.com) include a blank
programming tempalate (Blank.BS2) that will help you get started writing organized code. It's up to
you to follow the rest of the guidelines above – or develop and use guidelines of your own.
StampWorks Manual Version 1.2 • Page 21
'{$STAMP BS2}
'{$PBASIC 2.5}
DEBUG "hello world"
'{$STAMP BS2}
'{$PBASIC 2.5}
' Filename: blinkenlight.bs2
' Author:
' For:
' Date:
Gordon Hicks <[email protected]>
Interaccess Microcontroller Workshop
2005 Nov 18
' Description:
'
- An LED is connected to P8
'
- The LED blinks: one second on, one second off
' --- Mainline --Main:
HIGH 8
PAUSE 1000
LOW 8
PAUSE 1000
GOTO Main
'
'
'
'
make pin 0 go high; LED turns on
pause for 1000 ms
make pin 0 go low; LED turns off
pause for 1000 ms
' repeat
' --- End of Listing ---
'{$STAMP BS2}
'{$PBASIC 2.5}
'
'
'
'
'
'
'
'
'
'
Filename: chase_lights.bs2
Author:
For:
Date:
Gordon Hicks <[email protected]>
Interaccess Microcontroller Workshop
2005 Nov 18
Description:
- Four LEDs are connected each to P7, P8, P9, P10
- LEDs blink on and off in order (chaser lights)
' --- Mainline --Main:
'first LED
HIGH 8
PAUSE 100
LOW 8
' make pin 0 go high (+5v)
' pause for 100 ms (100 ms = 1/10 second)
' make pin 0 go low (0v or ground)
'second LED
HIGH 9
PAUSE 100
LOW 9
'third LED
HIGH 10
PAUSE 100
LOW 10
'fourth LED
HIGH 11
PAUSE 100
LOW 11
GOTO Main
' do it again
' --- End of Listing ---
'{$STAMP BS2}
'{$PBASIC 2.5}
'
'
'
'
'
'
'
'
'
'
'
Filename: pattern_chase.bs2
Author:
For:
Date:
Gordon Hicks <[email protected]>
Interaccess Microcontroller Workshop
2005 Nov 18
Description:
- Four LEDs are connected each to P7, P8, P9, P10
- LEDs blink on and off in order (chaser lights)
- Same behaviour as chase_lights.bs2, but using memory-mapped I/0 technique
' --- Constants --period CON 100
' milliseconds - sets speed of pattern
' --- Input/Output Assignments --LEDs VAR OUTC
DIRC = %1111
' --- Mainline ---
' 4 LEDs assigned to the output pins P8, P9, P10 and P11
' P8, P9, P10 and P11 are outputs
Main:
LEDs = %0001
PAUSE period
LEDs = %0010
PAUSE period
LEDs = %0100
PAUSE period
LEDs = %1000
PAUSE period
GOTO Main
' --- End of Listing ---
'{$STAMP BS2}
'{$PBASIC 2.5}
'
'
'
'
'
'
'
'
'
'
'
Filename: pattern_blink.bs2
Author:
For:
Date:
Gordon Hicks <[email protected]>
Interaccess Microcontroller Workshop
2005 Nov 18
Description:
- Four LEDs are connected each to P7, P8, P9, P10
- All four LEDs blink on and off in unison
- The off-time is 3 times longer than the on-time
' --- Constants --period CON 200
' milliseconds - sets speed of pattern
' --- Input/Output Assignments --LEDs VAR OUTC
DIRC = %1111
' 4 LEDs assigned to the output pins P8, P9, P10 and P11
' P8, P9, P10 and P11 are outputs
' --- Mainline --Main:
LEDs = %1111
PAUSE period
LEDs = %0000
PAUSE period*3
GOTO Main
' off time is 3 times the on time
' --- End of Listing ---
'{$STAMP BS2}
'{$PBASIC 2.5}
'
'
'
'
'
'
'
'
'
'
Filename: pattern_crawl.bs2
Author:
For:
Date:
Gordon Hicks <[email protected]>
Interaccess Microcontroller Workshop
2005 Nov 18
Description:
- Four LEDs are connected each to P7, P8, P9, P10
- Illuminated in a 'crawl' pattern
' --- Constants --period CON 50
' milliseconds - sets speed of pattern
' --- Input/Output Assignments --LEDs VAR OUTC
DIRC = %1111
' --- Mainline ---
' 4 LEDs assigned to the output pins P8, P9, P10 and P11
' P8, P9, P10 and P11 are outputs
Main:
LEDs = %0000
PAUSE period
LEDs = %1000
PAUSE period
LEDs = %1100
PAUSE period
LEDs = %1110
PAUSE period
LEDs = %1111
PAUSE period
LEDs = %0111
PAUSE period
LEDs = %0011
PAUSE period
LEDs = %0001
PAUSE period
GOTO Main
' --- End of Listing ---
'{$STAMP BS2}
'{$PBASIC 2.5}
'
'
'
'
'
'
'
'
'
'
'
Filename: button_detect.bs2
Author:
For:
Date:
Gordon Hicks <[email protected]>
Interaccess Microcontroller Workshop
2005 Nov 18
Description:
- A pushbutton input is attached to P7
- LED at P8 lights when button is pressed
- Debug echos button state
' --- Constants --period CON 200
' milliseconds; time between button checks
' --- Input/Output Assignments --Pushbutton
DIR7 = 0
VAR IN7
' pushbutton is attached to P7
' 0 --> input
LED
DIR8 = 1
VAR OUT8
' LED is attached to P8
' 1 --> output
' --- Mainline --Main:
LED = Pushbutton
' LED is given the same value as Pushbutton
'show button state in debug window:
IF (Pushbutton = 1) THEN
DEBUG "*** Pressed ***", CR
ELSE
DEBUG "---", CR
ENDIF
PAUSE period
GOTO Main
' --- End of Listing ---
'{$STAMP BS2}
'{$PBASIC 2.5}
'
'
'
'
'
'
'
'
'
Filename: button_toggle.bs2
Author:
For:
Date:
Gordon Hicks <[email protected]>
Interaccess Microcontroller Workshop
2005 Nov 18
Description:
- Pushing a button toggles the state of an LED
' --- Constants --period CON 50
' milliseconds between button checks
' --- Input/Output Assignments --Pushbutton VAR IN7
DIR7 = 0
' Current state of button
' 0 --> input
LED
DIR8 = 1
' 1 --> output
VAR OUT8
' --- Variable Assignments --Pushbutton_Prev VAR BIT ' Previous state of button
' --- Initialize --LED = 0
Pushbutton_Prev = 0
' --- Mainline --Main:
'is the pushbutton down? has it also just changed?
IF ((Pushbutton = 1) AND (Pushbutton <> Pushbutton_Prev)) THEN
' yes, so flip the LED state ( '~' means 'invert' )
LED = ~LED
ENDIF
' record current state as previous
Pushbutton_Prev = Pushbutton
PAUSE period
GOTO Main
' --- End of Listing ---
'{$STAMP BS2}
'{$PBASIC 2.5}
'
'
'
'
'
'
'
'
'
'
Filename: pattern_toggle.bs2
Author:
For:
Date:
Gordon Hicks <[email protected]>
Interaccess Microcontroller Workshop
2005 Nov 18
Description:
- A pushbutton input is attached to P7
- Pushing the button toggles between two different LED patterns
' --- Constants --period CON 50
' milliseconds - sets speed of pattern
' --- Input/Output Assignments --Pushbutton VAR IN7
DIR7 = 0
' Current state of pushbutton at P7
' P7 is an input
LEDs VAR OUTC
DIRC = %1111
' 4 LEDs assigned to the output pins P8, P9, P10 and P11
' P8, P9, P10 and P11 are outputs
' --- Variable Assignments --Pattern VAR BIT
' Current pattern number (0 or 1)
Pushbutton_Prev VAR BIT ' Previous pushbutton state
' --- Initialize --Pattern = 0
Pushbutton_Prev = 0
' --- Mainline --Main:
GOSUB Check_Button
IF (Pattern = 0 ) THEN
GOSUB Pattern_Blink
ELSE
GOSUB Pattern_Crawl
ENDIF
GOTO Main
' --- Subroutines --Check_Button:
'If the button has been pressed, increment Pattern
IF ((Pushbutton = 1) AND (Pushbutton <> Pushbutton_Prev)) THEN
Pattern = Pattern + 1
ENDIF
Pushbutton_Prev = Pushbutton
RETURN
Pattern_Blink:
LEDs = %1111
PAUSE period
LEDs = %0000
PAUSE period*3
RETURN
Pattern_Crawl:
LEDs = %0000
PAUSE period
LEDs = %1000
PAUSE period
LEDs = %1100
PAUSE period
LEDs = %1110
PAUSE period
LEDs = %1111
PAUSE period
LEDs = %0111
PAUSE period
LEDs = %0011
PAUSE period
LEDs = %0001
PAUSE period
RETURN
' --- End of Listing ---
'{$STAMP BS2}
'{$PBASIC 2.5}
'
'
'
'
'
'
'
'
'
Filename: pattern_select.bs2
Author:
For:
Date:
Gordon Hicks <[email protected]>
Interaccess Microcontroller Workshop
2005 Nov 18
Description:
Pushbutton steps through 5 different states: 3 patterns, all on, and all off
' --- Constants --period CON 50
' milliseconds - sets speed of pattern
' --- Input/Output Assignments --Pushbutton VAR IN7
DIR7 = 0
' Current state of pushbutton at P7
' P7 is an input
LEDs VAR OUTC
DIRC = %1111
' 4 LEDs assigned to the output pins P8, P9, P10 and P11
' P8, P9, P10 and P11 are outputs
' --- Variable Assignments --Pattern VAR NIB
' Pattern selector
Pushbutton_Prev VAR BIT ' Previous pushbutton state
' --- Initialize --Pattern = 0
Pushbutton_Prev = 0
' --- Mainline --Main:
GOSUB Check_Button
IF (Pattern = 1 ) THEN
GOSUB Pattern_On
ELSEIF (pattern = 2) THEN
GOSUB Pattern_Chase
ELSEIF (pattern = 3) THEN
GOSUB Pattern_Blink
ELSEIF (pattern = 4) THEN
GOSUB Pattern_Crawl
ELSE
GOSUB Pattern_Off
ENDIF
GOTO Main
' --- Subroutines --Check_Button:
'If the button has been pressed then increment Pattern
IF ((Pushbutton = 1) AND (Pushbutton <> Pushbutton_Prev)) THEN
' yes, so go to the next pattern
Pattern = Pattern + 1
IF (pattern >= 5) THEN
pattern = 0
ENDIF
ENDIF
Pushbutton_Prev = Pushbutton
RETURN
Pattern_Off:
LEDs = %0000
PAUSE period
RETURN
Pattern_On:
LEDs = %1111
PAUSE period
RETURN
Pattern_Chase:
LEDs = %0001
PAUSE period
LEDs = %0010
PAUSE period
LEDs = %0100
PAUSE period
LEDs = %1000
PAUSE period
RETURN
Pattern_Blink:
LEDs = %1111
PAUSE period
LEDs = %0000
PAUSE period*3
RETURN
Pattern_Crawl:
LEDs = %0000
PAUSE period
LEDs = %1000
PAUSE period
LEDs = %1100
PAUSE period
LEDs = %1110
PAUSE period
LEDs = %1111
PAUSE period
LEDs = %0111
PAUSE period
LEDs = %0011
PAUSE period
LEDs = %0001
'
'
'
PAUSE period
RETURN
' --- End of Listing ---