Skip to content

Commit c00dc3c

Browse files
author
K. Townsend
committed
Updated example code
1 parent 930e0f2 commit c00dc3c

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

Adafruit_MCP230xx/Adafruit_MCP230xx.py

+20-29
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ def config(self, pin, mode):
100100
return self.direction
101101

102102
def output(self, pin, value):
103-
assert self.direction & (1 << pin) == 0, "Pin %s not set to output" % pin
104-
103+
# assert self.direction & (1 << pin) == 0, "Pin %s not set to output" % pin
105104
if self.num_gpios <= 8:
106105
self.outputvalue = self._readandchangepin(MCP23008_GPIO, pin, value. self.i2c.readU8(MCP23008_OLAT))
107106
if self.num_gpios <= 16:
@@ -153,30 +152,22 @@ def pullup(self, pin, value):
153152

154153

155154
if __name__ == '__main__':
156-
157-
# input test
158-
# for i in range(16):
159-
# mcp.pullup(i, 1)
160-
# while (True):
161-
# for i in range(16):
162-
# print "%d: %x" % (i, mcp.input(i) >> i)
163-
164-
# output test
165-
# for i in range(16):
166-
# mcp.config(i, OUTPUT)
167-
# while (True):
168-
# for i in range(16):
169-
# mcp.output(i, 0)
170-
# time.sleep(0.1)
171-
# for i in range(16):
172-
# mcp.output(i, 1)
173-
# time.sleep(0.1)
174-
175-
176-
mcp = Adafruit_MCP230XX(busnum = 1, address = 0x20, num_gpios = 16)
177-
mcp.config(8, OUTPUT)
178-
mcp.config(7, OUTPUT)
179-
mcp.config(6, OUTPUT)
180-
mcp.output(8, 1)
181-
mcp.output(7, 1)
182-
mcp.output(6, 1)
155+
# Use busnum = 0 for older Raspberry Pi's (pre 512MB)
156+
mcp = Adafruit_MCP230XX(busnum = 0, address = 0x20, num_gpios = 16)
157+
# Use busnum = 1 for new Raspberry Pi's (512MB)
158+
# mcp = Adafruit_MCP230XX(busnum = 1, address = 0x20, num_gpios = 16)
159+
160+
# Set pins 0, 1 and 2 to output (you can set pins 0..15 this way)
161+
mcp.config(0, OUTPUT)
162+
mcp.config(1, OUTPUT)
163+
mcp.config(2, OUTPUT)
164+
165+
# Set pin 3 to input with the pullup resistor enabled
166+
mcp.pullup(3, 1)
167+
# Read pin 3 and display the results
168+
print "%d: %x" % (3, mcp.input(3) >> 3)
169+
170+
# Python speed test on output 0 toggling at max speed
171+
while (True):
172+
mcp.output(0, 1) # Pin 0 High
173+
mcp.output(0, 0) # Pin 1 Low

0 commit comments

Comments
 (0)