ADVENTURES IN PROPELLER PROGRAMMING
Post comments on this article and find any associated files and/or downloads at
www.nutsvolts.com/index.php?/magazine/article/october2015_SpinZone.
■ FIGURE 3.
Accelerometer
Axes.
■ FIGURE 2. Parallax Conference Badges.
we do toaster-over reflow soldering. No worries! If you
don't yet have a Parallax Convention Badge, Seeed Studio
makes an experimenter's board for less than ten bucks.
There are others, too, and I've listed them in the Resources
block. Just remember that the MMA7660FC is a 3.3V
device; if you want to use it with a BASIC Stamp or
Arduino, you'll need a proper power supply and level-shifters on the I2C pins.
The MMA7660FC is deliberately simple, and working
with it is too. Let's jump in and set it up for typical use:
pub start(sclpin, sdapin)
i2c.setupx(sclpin, sdapin)
The MMA7660FC is designed for devices like cell
phones, smart wearables, and gaming controls. At its core,
it provides four key pieces of information: X axis g-force; Y
axis g-force; Z axis g-force; and tilt status bits derived from
the other three registers. It can be configured to control an
interrupt pin, but I tend not to use that (an interrupt input
is part of the badge design so you can experiment with it).
write reg(MODE, $00)
write reg(INTSU, $00)
write reg(SR, $00)
write reg(PDET, $6C)
write reg(PD, $08)
write reg(MODE, $C1)
Figure 3 illustrates the axes behavior of the
MMA7660FC. Imagine it installed in a cell phone, and the
cell phone laying flat on a table. With the face of the cell
phone pointed away from the Earth's core, the Z axis
would read +1g (with X and Y at zero). If we stand the
phone up so the top is pointed skyward, the X axis would
read -1g.
We start by connecting to the device via I2C. With the
Conference Badge, we're using pins 28 (SCL) and 29
(SDA) — the I2C pins used by the EEPROM. For stand-alone boards, we can use any set of free pins.
If we now rotate the phone counterclockwise so that
the right side (as we're looking at it) is pointed skyward,
the Y axis will read -1g. Think of it this way: The illustration
shows the behavior of an axis that is pointed skyward.
When not moving, we will tend to see between -1
and +1 on any axis, but the device has a range of -1.5g to
+1.5g. If we shake our phone, for example, we can exert
more than 1g on it. In fact, a neat feature of the
MMA7660FC is a shake alarm; this is set whenever any
axis is greater than 1.3g.
The MMA7660FC also does some filtering and can
provide “tap” status if we choose. The idea behind a tap is
to simulate a button press.
The write_reg() method takes care of the nitty-gritty
I2C transaction details for writing to a single register. The
first thing to do is suspend g-force measurements by
clearing the MODE register; this is necessary to set the
sample rate. In simple apps, we won't tend to use the
interrupt output pin so that register (INTSU) is cleared,
too. Clearing the SR register sets the sample rate to 120
readings per second; this rate is required for tap detection.
To enable tap detection, we set up the PDET register;
in this case, we're looking for a tap on the Z axis with a
threshold of 12. The PD register sets the tap debounce
count. You may have to experiment with this value based
on your application and device — I'm using a
recommendation from a Freescale app note and it seems
to work well when I tap the badge with my forefinger.
Finally, we activate the MMA7660FC by writing a 1 to bit
0 of the MODE register (the other bits configure the
interrupt output — when used — for push-pull/active-high
output as required by the badge).
October 2015 11