Here I have changed a module (PCA9554.PY, __init__, akael "ported" by theshade, 02.09.2019) a little bit.
When starting main.py I get the following error message:
AttributeError: 'module'object has no attribute'PCA9554
I looked it up on the net, but did not find a solution.
I attach all three files if possible.
The examples from Mike Causer for the MCP23017 and PCF8574 work. I can switch one LED on or off at a time.
What have I missed or what is wrong?
Code: Select all
[/# 23.10.2020
from machine import Pin , I2C
import pca9554
import time
i2c = I2C ( scl = Pin ( 5 ), sda = Pin ( 4 ), freq = 100000)
pca = pca9554 . PCA9554 (i2c, 0x25)
pca.setoutput()
pca.writebyte(0x00)
while True:
pca.writebyte(0x00)
time.sleep (1)
pca.writebyte(0xff)
time.sleep (1)]
[code][/"""
MicroPython PCA9554 8-Bit I2C I/O Expander with Interrupt
Quelle:
################################################################################
# Author : akael "ported" by theshade
# Creation date : 02.02.2019
# Langage : microPython
# Filename : pca9554.py
# Target : Pycom pyscan
# Description : PCA9554A GPIO Expander
################################################################################
23.10.2020
Anpassung an PCA9554 (Init)
"""
import time
from maschine import I2C
class PCA9554():
#Define register
Pca9554_IN_REG = const(0x00) #Input register
Pca9554_OUT_REG = const(0x01) #Output register
Pca9554_POL_REG = const(0x02) #Polarity inversion register (1=data inverted)
Pca9554_DIR_REG = const(0x03) #Config register (0=output, 1=input)
Pca9554_I2CADDR = const(0x25)
def __init__(self, i2c, address=0x25):
self._i2c = i2c
self._address = address
self._port = bytearray(1)
if i2c.scan().count(address) == 0:
raise OSError('PCA9554 not found at I2C address {:#x}'.format(address))
#"""set bit as output"""
def setoutput(self):
print('setting output to')
currentvalue = self.i2c.readfrom_mem(Pca9554_I2CADDR , Pca9554_DIR_REG, 1)
print(currentvalue[0] | (0x01<<self.line))
self.i2c.writeto_mem(Pca9554_I2CADDR, Pca9554_DIR_REG , currentvalue[0] & 255-(1<<self.line))
#"""set bit as input"""
def setinput(self):
print('setting input to')
currentvalue = self.i2c.readfrom_mem(Pca9554_I2CADDR , Pca9554_DIR_REG, 1)
print(currentvalue[0] | (0x01<<self.line))
self.i2c.writeto_mem(Pca9554_I2CADDR, Pca9554_DIR_REG ,currentvalue[0] | (0x01<<self.line))
#self.i2c.writeto_mem(Pca9554_I2CADDR, Pca9554_DIR_REG , self.line)
def writebyte(self,value):
#"""write output byte value"""
self.i2c.writeto_mem(Pca9554_I2CADDR, Pca9554_OUT_REG, value)
return
def readbyte(self):
#"""read input byte value"""
return self.i2c.readfrom_mem(Pca9554_I2CADDR, Pca9554_IN_REG,1)
def set(self):
#"""set output bit at 1"""
currentvalue = self.i2c.readfrom_mem(Pca9554_I2CADDR, Pca9554_OUT_REG,1)
#self.i2c.writeto_mem(Pca9554_I2CADDR, Pca9554_OUT_REG, 1<<2)
self.i2c.writeto_mem(Pca9554_I2CADDR, Pca9554_OUT_REG, currentvalue[0] | 1<<self.line)
def reset(self):
#"""reset output bit at 0"""
currentvalue = self.i2c.readfrom_mem(Pca9554_I2CADDR, Pca9554_OUT_REG,1)
self.i2c.writeto_mem(Pca9554_I2CADDR, Pca9554_OUT_REG, currentvalue[0] & (255-(1<<self.line)))
return
def get(self):
#"""read input bit value"""
linevalue = self.i2c.readfrom_mem(Pca9554_I2CADDR, Pca9554_IN_REG,1)
ret = ((linevalue >> self.line) & 1 )
return ret ]