Stamp
Remember that we don’t have to invert the active-low
button inputs, as the MCP23016 has been set up to do that
for us (I love this feature). We could save a bit of variable
space by aliasing btns to the NIB0 of lcdIO, but I decided
not to so that things don’t get mixed up, as lcdIO is used
in so many other places.
Back to the button demo code — now that we have the
current status, we can display each of them on the second
line using LOOKUP again. Here, we’ll use LOOKUP to
select the character; as it stands, a dash means that the
button is not pressed and an asterisk indicates that a
button is pressed. I decided to do this because 0 and 1 are
boring — but if that’s what you want to use for one of your
apps, you can change the LOOKUP line to this:
Finally, here are a couple of subroutines to update the
LED, as required by the demo code:
lcdIO = “0” + btns.LOWBIT(idx)
Alright, we’re almost home — the last thing we need to
test is the LED. Just for fun, let’s make it light when all of
the buttons are pressed.
Update_LED:
IF (btns = %1111) THEN
GOSUB LED_On
ELSE
GOSUB LED_Off
ENDIF
LED_On:
I2CIN SDA, Rd23016, GP1, [lcdCtrl]
IF (lcdLED = Is_Off) THEN
lcdLED = Is_On
I2COUT SDA, Wr23016, GP1, [lcdCtrl]
ENDIF
RETURN
LED_Off:
I2CIN SDA, Rd23016, GP1, [lcdCtrl]
IF (lcdLED = Is_On) THEN
lcdLED = Is_Off
I2COUT SDA, Wr23016, GP1, [lcdCtrl]
ENDIF
RETURN
Set_LED:
I2CIN SDA, Rd23016, GP1, [lcdCtrl]
IF (lcdLED <> ledStatus) THEN
lcdLED = ledStatus
I2COUT SDA, Wr23016, GP1, [lcdCtrl]
ENDIF
RETURN
F
o
r
E
l
e
c
t
r
o
n
i
c
s
NUTS & VOLTS
E
v
e
r
y
t
h
i
n
g
All of these routines are quite simple; we read the
status of GP1, check the LED control bit, then update it —
if required — and send the port data back. While our demo
doesn’t actually use the Set_LED routine, it’s included
because it will be useful when we want the terminal LED to
follow a status bit from elsewhere in our application (that
we’ve aliased as ledStatus).
Do note that none of these routines write the LED
status bit to the MCP23016 unless a change is actually
required.
Okay, we’re done. How about that for a simple — yet
abundantly useful — project? As I suggested some time
back, I’ve become a very big fan of the I2C buss and the
MCP23016 is a great part to use with it.
A couple of final notes: Yes, you can control the
MCP23016 with the BS2, BS2e, and BS2sx. You’ll need to
use manual I2C code, since those BASIC Stamps don’t
have the I2CIN and I2COUT instructions. We did that back
in the May 2002 issue (you can find that article online as a
PDF on the Parallax website — www.parallax.com). Finally
(I promise), the MCP23016 uses the same device address
as the PCF8574A — so you can’t mix them on the same
SDA buss pin (but you can mix the MCP23016 with the
PCF8574AP).
Have fun with the MCP23016; it’s a great part — and,
until next time, Happy Stamping. NV
RjweJo
illia
Pa
wwsn
ms
ra
wo W
@p
lla
.paruillia
ara
x,
allarm
lla
In
x.ccs
x.c
c.
omeoms
80
MAY 2004