26
26
27
27
MCP23017_IODIRA = 0x00
28
28
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
35
38
36
39
class Adafruit_MCP230XX (object ):
37
40
OUTPUT = 0
@@ -48,7 +51,7 @@ def __init__(self, address, num_gpios):
48
51
if num_gpios <= 8 :
49
52
self .i2c .write8 (MCP23017_IODIRA , 0xFF ) # all inputs on port A
50
53
self .direction = self .i2c .readU8 (MCP23017_IODIRA )
51
- self .i2c .write8 (MCP23017_GPPUA , 0x00 )
54
+ self .i2c .write8 (MCP23008_GPPUA , 0x00 )
52
55
elif num_gpios > 8 and num_gpios <= 16 :
53
56
self .i2c .write8 (MCP23017_IODIRA , 0xFF ) # all inputs on port A
54
57
self .i2c .write8 (MCP23017_IODIRB , 0xFF ) # all inputs on port B
@@ -76,7 +79,7 @@ def _readandchangepin(self, port, pin, value, currvalue = None):
76
79
77
80
def pullup (self , pin , value ):
78
81
if self .num_gpios <= 8 :
79
- return self ._readandchangepin (MCP23017_GPPUA , pin , value )
82
+ return self ._readandchangepin (MCP23008_GPPUA , pin , value )
80
83
if self .num_gpios <= 16 :
81
84
if (pin < 8 ):
82
85
return self ._readandchangepin (MCP23017_GPPUA , pin , value )
@@ -98,7 +101,7 @@ def config(self, pin, mode):
98
101
def output (self , pin , value ):
99
102
# assert self.direction & (1 << pin) == 0, "Pin %s not set to output" % pin
100
103
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 ))
102
105
if self .num_gpios <= 16 :
103
106
if (pin < 8 ):
104
107
self .outputvalue = self ._readandchangepin (MCP23017_GPIOA , pin , value , self .i2c .readU8 (MCP23017_OLATA ))
@@ -115,7 +118,7 @@ def input(self, pin):
115
118
assert pin >= 0 and pin < self .num_gpios , "Pin number %s is invalid, only 0-%s are valid" % (pin , self .num_gpios )
116
119
assert self .direction & (1 << pin ) != 0 , "Pin %s not set to input" % pin
117
120
if self .num_gpios <= 8 :
118
- value = self .i2c .readU8 (MCP23017_GPIOA )
121
+ value = self .i2c .readU8 (MCP23008_GPIOA )
119
122
elif self .num_gpios > 8 and self .num_gpios <= 16 :
120
123
value = self .i2c .readU16 (MCP23017_GPIOA )
121
124
temp = value >> 8
@@ -124,11 +127,11 @@ def input(self, pin):
124
127
return value & (1 << pin )
125
128
126
129
def readU8 (self ):
127
- result = self .i2c .readU8 (MCP23017_OLATA )
130
+ result = self .i2c .readU8 (MCP23008_OLATA )
128
131
return (result )
129
132
130
133
def readS8 (self ):
131
- result = self .i2c .readU8 (MCP23017_OLATA )
134
+ result = self .i2c .readU8 (MCP23008_OLATA )
132
135
if (result > 127 ): result -= 256
133
136
return result
134
137
@@ -146,7 +149,7 @@ def readS16(self):
146
149
return ((hi << 8 ) | lo )
147
150
148
151
def write8 (self , value ):
149
- self .i2c .write8 (MCP23017_OLATA , value )
152
+ self .i2c .write8 (MCP23008_OLATA , value )
150
153
151
154
def write16 (self , value ):
152
155
assert self .num_gpios >= 16 , "16bits required"
@@ -176,7 +179,11 @@ def pullup(self, pin, value):
176
179
177
180
178
181
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
+ # ***************************************************
180
187
181
188
# Set pins 0, 1 and 2 to output (you can set pins 0..15 this way)
182
189
mcp .config (0 , mcp .OUTPUT )
@@ -191,4 +198,4 @@ def pullup(self, pin, value):
191
198
# Python speed test on output 0 toggling at max speed
192
199
while (True ):
193
200
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