27220 Parallax Inc, 27220 Datasheet - Page 48

BOOK STAMPWORKS

27220

Manufacturer Part Number
27220
Description
BOOK STAMPWORKS
Manufacturer
Parallax Inc
Datasheet

Specifications of 27220

Accessory Type
Booklet
Product
Microcontroller Accessories
Lead Free Status / RoHS Status
Not applicable / Not applicable
For Use With/related Products
StampWorks
Lead Free Status / RoHS Status
Lead free / RoHS Compliant, Not applicable / Not applicable
Behind the Scenes
This experiment demonstrates the ability to directly manipulate the BASIC Stamp
output pins just as we could any other variable. This program also demonstrates
conditional looping by adding pre- and post-loop tests to DO-LOOP.
The program starts by initializing the LEDs to %00000001 – this turns on the LED
connected to P0. Then we drop into the first DO-LOOP where the value of LEDs is
immediately tested.
%10000000 then the code within the DO-LOOP is allowed to run, otherwise the
program continues at the line that follows LOOP.
Since LEDs is initially less than the test value, the program drops into the loop where
it runs a small PAUSE, then the lit LED is moved to the left with the << (shift-left)
operator. Shifting left by one bit performs the same function as multiplying by two,
albeit far more efficiently. After the shift the program goes back to the DO WHILE
line where the value of LEDs (now %00000010) is tested again.
After seven passes through the upper loop, LEDs will have a value of %10000000
and the test will fail (result will be False); this will force the program to jump to the
top of the second DO-LOOP.
The second DO-LOOP is nearly identical to the first except that the value of LEDs is
shifted right one bit with >> (same as dividing by two), and the test occurs at the
end of the loop. Note that when the test is placed at the end of the DO-LOOP
structure, the loop code will run at least one time. After seven iterations of the
bottom loop the test will fail and the code will drop to the GOTO Main line which
takes us back to the top of the program.
Beginning programmers will often ask, “When should I use WHILE versus UNTIL in a loop
test?”
It is in fact possible to write functionally equivalent code using WHILE or UNTIL. That said,
your programs will be easier to others to follow (and for you to pick up later) if the listing
reads logically. To that end, it is suggested that WHILE is used to run the loop while a
condition is true; and UNTIL is used to run the loop until a condition becomes true.
If the value of LEDs (currently %00000001) is less than

Related parts for 27220