SPIN ZONE
ADVENTURES IN PROPELLER PROGRAMMING
■ BY JON WILLIAMS
THE JOY OF JOYSTICKS
I think it’s fair to say that the joystick may be the
first user input device connected to a computer
that took us beyond the keyboard – games are far
more fun with a stick in our hand than hunting
for keys, right? The first joysticks, of course, were
simply made up of switches pressed by a plate
connected to the stick. Then came analog joysticks which were built up with
two potentiometers mechanically linked at a 90-degree angle; one pot for
each axis. The original PC joysticks were easy to connect to microcontrollers
without hacking. With the proliferation of USB ports, though, analog joysticks
changed their interface and are no longer microcontroller-friendly. Darn ...
■ FIGURE 1. PP
Servo Player.
GAME ON!
If you’ve played on any video game console in the last
10 years, you’ve no doubt had the opportunity to place
your thumbs on the controller’s mini joysticks and go
crazy with your friends. Until recently, those mini joysticks
with their cute, mushroom-shaped caps weren’t very easy
to come by and (when you could find them) they were
pricey — a few years ago, I paid about $10 each.
Well, that’s not the case anymore. Parallax offers a
version (#27800) that we can plug into solderless
breadboards, and Nick over at Gadget Gangster is offering
them as raw parts that we can build into projects. So, the
hardware this month is a Propeller Platform module that
holds two of those mini joysticks, an ADC to read them,
and some other bits that we can use in a variety of
projects. Figure 1 shows my completed prototype.
USING THE MCP3204
The mini joysticks are, in fact, two 10K potentiometers
that are mechanically linked together such that one
changes with X-axis movement; the other changes with Y-axis movement. With two joysticks, we’ll need a four-channel ADC. We could use an ADC0834 but the
MCP3204 seems to be popular among Propeller users so I
thought I’d give it a whirl.
Figure 2 shows the connections between the joysticks
and the MCP3204 — a four-channel, 12-bit ADC. The
MCP3204 has four pins that form the communication buss
to the host microcontroller, but the addition of a 2.2K
14 September 2010
resistor allows us to cut that down to three. The resistor
protects the Propeller pin from a short circuit when DOUT
is an output, the I/O pin is an output, and the two are in
opposite states (one high, one low).
The process of reading a channel value from the
MCP3204 requires these steps:
1. Take the /CS line low.
2. Output a five-bit configuration word.
3. Read 13 bits from the ADC (null plus 12 bits).
4. Return the /CS line high to deselect.
The MCP3204 will handle very high speed
transactions and we could write a driver in assembly but,
really, there’s no need for most applications. I’ve created a
simple object written completely in Spin that will handle
reading any of the channels in the two modes that are
supported (single-ended and differential).
The MCP3204 object has methods to set up the I/O
pins used by the circuit and two variants that will read the
value of a channel. The init() method takes care of the
I/O pins and places the ‘3204 in the deselected state:
pub init(cspin, clkpin, diopin)
cs := cspin
outa[cs] := 1
dira[cs] := 1
clk := clkpin
outa[clk] := 0
dira[clk] := 1
dio := diopin