27220 Parallax Inc, 27220 Datasheet - Page 106

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
Page 96 · StampWorks
workhorse of this experiment is the subroutine Get_Buttons. As presented, it will
accommodate four normally-open, active-low inputs but it can easily be modified for
any number of inputs from two to 16.
The purpose of Get_Buttons is to ensure that the inputs stay pressed for at least
25 milliseconds with no contact “bouncing.” Debounced inputs will be returned in the
btns
variable,
, with a valid input represented by a “1” in the respective button
position.
The Get_Buttons routine starts by assuming that all button inputs will be valid, so
all the bits of btns variable are set to one. Then, using a FOR-NEXT loop, the inputs
are scanned and compared to the previous state. Since the inputs are active-low
(zero when pressed), the Invert operator (~) flips them. The And operator (&) is
used to update the current state. For a button to be valid, it must remain pressed
through the entire FOR-NEXT loop.
Here’s how the debouncing technique works: When a button is pressed, the input to
the BASIC Stamp will be zero. The Invert operator will flip zero to one. One “Anded”
with one is still one, so that button remains valid. If the button is not pressed, the
raw input to the BASIC Stamp will be one (because of the 10K pull-up to Vdd). One
is inverted to zero. Zero “Anded” with any number is zero and will cause the button
to remain invalid through the entire debounce loop.
The debounced button inputs are displayed in a DEBUG window with the IBIN4
modifier so that the value (state, pressed = “1”) of each button is clearly displayed.
Write Code like a Pro
Many programs will require the ability to “single shot” a button input, that is, to
activate some event or process only after the change-of-state of a button.
By
keeping track of the last scan value we can report to the program which buttons
changed between the current scan and the last.

Related parts for 27220