'''
python-periphery mcp23017 sample
2G_IOT---MCP23017
-----------------
Pin#3----SDA
Pin#5----SCK
---------GPA0--- LED1
---------GPA1--- LED2
---------GPA2--- LED3
---------GPA3--- LED4
---------GPA4--- LED5
---------GPA5--- LED6
---------GPA6--- LED7
---------GPA7--- LED8
GND------A0
GND------A1
GND------A2
3.3V-----RESET
3.3V-----VDD
GND------VSS
'''
#!/usr/bin/python
#-*- encoding: utf-8 -*-
from periphery import I2C
import time
import sys
argv = sys.argv
argc = len(argv)
if (argc == 1):
device = "/dev/i2c-0"
if (argc == 2 and int(argv[1]) == 0):
device = "/dev/i2c-0"
if (argc == 2 and int(argv[1]) == 1):
device = "/dev/i2c-1"
if (argc == 2 and int(argv[1]) == 2):
device = "/dev/i2c-2"
print "device={0:s}".format(device)
# Open i2c-0 controller
#i2c = I2C("/dev/i2c-0")
i2c = I2C(device)
# Write data to MCP23017
msgs = [I2C.Message([0x00]), I2C.Message([0x00],
read=False)]
i2c.transfer(0x20, msgs)
msgs = [I2C.Message([0x01]), I2C.Message([0x00],
read=False)]
i2c.transfer(0x20, msgs)
msgs = [I2C.Message([0x12]), I2C.Message([0x00],
read=False)]
i2c.transfer(0x20, msgs)
msgs = [I2C.Message([0x13]), I2C.Message([0x00],
read=False)]
i2c.transfer(0x20, msgs)
byte=0
for var in range(0,8):
byte=byte << 1
byte=byte+1
print 'byte=0x{0:02x}'.format(byte)
msgs = [I2C.Message([0x12]), I2C.Message([byte],
read=False)]
i2c.transfer(0x20, msgs)
time.sleep(1.0);
for var in range(0,8):
byte=byte >> 1
print 'byte=0x{0:02x}'.format(byte)
msgs = [I2C.Message([0x12]), I2C.Message([byte],
read=False)]
i2c.transfer(0x20, msgs)
time.sleep(1.0);
i2c.close() |