encoder value in our top-level program.
return (encoder ~> 2)
Remember that situation with the detented
encoders and the 4x multiplier? We multiplied the
limits and preset value by four to accommodate the
changes between clicks. Well, if we’re using a
detented encoder we can’t simply shift the value
right and return it to the intended range. Doing this
on a negative number would cause it to go positive
(because a 0 would be shifted into bit 31 — the sign
bit). No worries. Spin has a really cool operator call
Shift Arithmetic Right (~>). This operator does a right shift
while maintaining the sign of the value, allowing the use
of positive and negative numbers with no muss or fuss.
■ FIGURE 3. 74x165 Connections.
MORE EASY INPUT EXPANSION
The product I mentioned in the opening is a 16-
channel DMX controller that my business partner [and
former Parallax engineer John Barrowman] and I designed.
Since control of the outputs is paramount, we use P0..P15
for the output channels. With the RS-485 circuitry, a two-position mode switch, and other I/O requirements, we just
didn’t have enough pins on the Propeller to accommodate
the nine-bit DMX address (like I did in my small DMX
project last November).
The solution? Dirt easy! Use a 74x165 input shift
register. With three pins, we get eight inputs; for the DMX
address we simply connected bit 8 of the address switch
directly to the Propeller. For my Propeller platform add-on,
I used an eight-bit switch and a 2x8 header (to allow off-board switches); see Figure 3 for the schematic.
If you’ve used the 74x165 with PBASIC or SX/B, you
probably used SHIFTIN to read it. We can’t do that with
the Propeller directly anyway, as there is no built-in
SHIFTIN instruction. You might be wondering why. For all
its power, the Propeller has to squeeze the Spin
interpreter into a single cog and that’s tough work — so
some niceties from PBASIC are not included.
No problem! We’ll just write our own method,
and no assembly (PASM) is required. The code that
follows is very similar to PBASIC’s SHIFTIN, reading a
single byte in MSBFIRST mode — though it is set up to
accommodate the Shift/Load line of the 74x165.
tmp165 := (tmp165 << 1) | ina[do]
As with PBASIC and SX/B SHIFTIN, this method takes
care of setting the I/O pins used to achieve the required
states. We start by making the Shift/Load pin an output
and high; the Clock pin an output and low; and the Data
Out (from the x165) an input.
The Shift/Load line is blipped low, then back high; this
latches the present state of the eight inputs to an internal
register. With the data latched, it can be shifted into the
Propeller using a repeat loop.
The first line of the repeat loop does all the hard
work; it preps the work value by shifting it left one bit
and then OR’ing the state of the DO pin to the value
(in bit 0). After the bit is moved into the work variable,
◗ BILL OF MATERIALS
pub in165(ld, do, clk) | tmp165
May 2010 17