5: BASIC Stamp Command Reference - COUNT
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 89
COUNT
BS1 BS2 BS2e BS2sx BS2p
COUNT Pin, Period, Variable
Function
Count the number of cycles (0-1-0 or 1-0-1) on the specified pin during the
Period time frame and store that number in Variable.
• Pin is a variable/constant/expression (0 – 15) that specifies the I/O
pin to use. This pin will be set to input mode.
• Period is a variable/constant/expression (1 – 65535) specifying the
time during which to count. The unit of time for Period is described
in Table 5.3.
• Variable is a variable (usually a word) in which the count will be
stored.
Quick Facts
BS2 BS2e BS2sx BS2p
Units in Period
1 ms 1 ms 400 µs 287 µs
Period range
1 ms to
65.535 s
1 ms to
65.535 s
400 µs to
26.214 s
287 µs to
18.809 s
Minimum pulse
width
4.16 µs 4.16 µs 1.66 µs 1.20 µs
Maximum
frequency
(square wave)
120,000 Hz 120,000 Hz 300,000 Hz 416,700 Hz
Explanation
The COUNT instruction makes the Pin an input, then for the specified
period of time, counts cycles on that pin and stores the total in a variable.
A cycle is a change in state from 1 to 0 to 1, or from 0 to 1 to 0.
According to Table 5.3, COUNT on the BS2 can respond to transitions
(pulse widths) as small as 4.16 microseconds (µs). A cycle consists of two
transitions (e.g., 0 to 1, then 1 to 0), so COUNT (on the BS2) can respond to
square waves with periods as short as 8.32 µs; up to 120 kilohertz (kHz) in
frequency. For non-square waves (those whose high time and low time are
unequal), the shorter of the high and low times must be at least 4.16 µs in
width (on the BS2). Refer to Table 5.3 for data on other BASIC Stamps.
2
2
2
Table 5.3: COUNT Quick Facts.
NOTE: All timing values are
approximate.