Skip to content

Commit e04798d

Browse files
author
K. Townsend
committed
Fixed MCP23008 registers
1 parent f708b97 commit e04798d

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

Adafruit_MCP230xx/Adafruit_MCP230xx.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@
2626

2727
MCP23017_IODIRA = 0x00
2828
MCP23017_IODIRB = 0x01
29-
MCP23017_GPIOA = 0x12
30-
MCP23017_GPIOB = 0x13
31-
MCP23017_GPPUA = 0x0C
32-
MCP23017_GPPUB = 0x0D
33-
MCP23017_OLATA = 0x14
34-
MCP23017_OLATB = 0x15
29+
MCP23017_GPIOA = 0x12
30+
MCP23017_GPIOB = 0x13
31+
MCP23017_GPPUA = 0x0C
32+
MCP23017_GPPUB = 0x0D
33+
MCP23017_OLATA = 0x14
34+
MCP23017_OLATB = 0x15
35+
MCP23008_GPIOA = 0x09
36+
MCP23008_GPPUA = 0x06
37+
MCP23008_OLATA = 0x0A
3538

3639
class Adafruit_MCP230XX(object):
3740
OUTPUT = 0
@@ -48,7 +51,7 @@ def __init__(self, address, num_gpios):
4851
if num_gpios <= 8:
4952
self.i2c.write8(MCP23017_IODIRA, 0xFF) # all inputs on port A
5053
self.direction = self.i2c.readU8(MCP23017_IODIRA)
51-
self.i2c.write8(MCP23017_GPPUA, 0x00)
54+
self.i2c.write8(MCP23008_GPPUA, 0x00)
5255
elif num_gpios > 8 and num_gpios <= 16:
5356
self.i2c.write8(MCP23017_IODIRA, 0xFF) # all inputs on port A
5457
self.i2c.write8(MCP23017_IODIRB, 0xFF) # all inputs on port B
@@ -76,7 +79,7 @@ def _readandchangepin(self, port, pin, value, currvalue = None):
7679

7780
def pullup(self, pin, value):
7881
if self.num_gpios <= 8:
79-
return self._readandchangepin(MCP23017_GPPUA, pin, value)
82+
return self._readandchangepin(MCP23008_GPPUA, pin, value)
8083
if self.num_gpios <= 16:
8184
if (pin < 8):
8285
return self._readandchangepin(MCP23017_GPPUA, pin, value)
@@ -98,7 +101,7 @@ def config(self, pin, mode):
98101
def output(self, pin, value):
99102
# assert self.direction & (1 << pin) == 0, "Pin %s not set to output" % pin
100103
if self.num_gpios <= 8:
101-
self.outputvalue = self._readandchangepin(MCP23017_GPIOA, pin, value, self.i2c.readU8(MCP23017_OLATA))
104+
self.outputvalue = self._readandchangepin(MCP23008_GPIOA, pin, value, self.i2c.readU8(MCP23008_OLATA))
102105
if self.num_gpios <= 16:
103106
if (pin < 8):
104107
self.outputvalue = self._readandchangepin(MCP23017_GPIOA, pin, value, self.i2c.readU8(MCP23017_OLATA))
@@ -115,7 +118,7 @@ def input(self, pin):
115118
assert pin >= 0 and pin < self.num_gpios, "Pin number %s is invalid, only 0-%s are valid" % (pin, self.num_gpios)
116119
assert self.direction & (1 << pin) != 0, "Pin %s not set to input" % pin
117120
if self.num_gpios <= 8:
118-
value = self.i2c.readU8(MCP23017_GPIOA)
121+
value = self.i2c.readU8(MCP23008_GPIOA)
119122
elif self.num_gpios > 8 and self.num_gpios <= 16:
120123
value = self.i2c.readU16(MCP23017_GPIOA)
121124
temp = value >> 8
@@ -124,11 +127,11 @@ def input(self, pin):
124127
return value & (1 << pin)
125128

126129
def readU8(self):
127-
result = self.i2c.readU8(MCP23017_OLATA)
130+
result = self.i2c.readU8(MCP23008_OLATA)
128131
return(result)
129132

130133
def readS8(self):
131-
result = self.i2c.readU8(MCP23017_OLATA)
134+
result = self.i2c.readU8(MCP23008_OLATA)
132135
if (result > 127): result -= 256
133136
return result
134137

@@ -146,7 +149,7 @@ def readS16(self):
146149
return((hi << 8) | lo)
147150

148151
def write8(self, value):
149-
self.i2c.write8(MCP23017_OLATA, value)
152+
self.i2c.write8(MCP23008_OLATA, value)
150153

151154
def write16(self, value):
152155
assert self.num_gpios >= 16, "16bits required"
@@ -176,7 +179,11 @@ def pullup(self, pin, value):
176179

177180

178181
if __name__ == '__main__':
179-
mcp = Adafruit_MCP230XX(address = 0x20, num_gpios = 16)
182+
mcp = Adafruit_MCP230XX(address = 0x20, num_gpios = 8)
183+
184+
# ***************************************************
185+
# Set num_gpios to 8 for MCP23008 or 16 for MCP23017!
186+
# ***************************************************
180187

181188
# Set pins 0, 1 and 2 to output (you can set pins 0..15 this way)
182189
mcp.config(0, mcp.OUTPUT)
@@ -191,4 +198,4 @@ def pullup(self, pin, value):
191198
# Python speed test on output 0 toggling at max speed
192199
while (True):
193200
mcp.output(0, 1) # Pin 0 High
194-
mcp.output(0, 0) # Pin 1 Low
201+
mcp.output(0, 0) # Pin 0 Low

0 commit comments

Comments
 (0)