I'm trying to add to the Python code to be able read data from the LCD namely: Read Busy Flag and Address Counter and
Read Data from RAM as described on pg. 24 and 58 in the data sheet HD44780.pdf
I have not been successful yet. It may be that I don't understand setting pins of the MCP23017 as input or output.
Any suggestions or references would be appreciated. Thanks.
In Adafruit_ChartLCDPlate.py I have added code similar to write4 but for reading. I've experimented with various combinations
of making pins input or output, but so far no luck. Also the hi and low 4 bits always turn out the same even though I give the enable
pulse twice. Also there is a bug related to the saving of the direction information (fix at end of this post)
Attempted routine to read
- Code: Select all
def readBFandACv(self):
""" Send command 01 to LCD to read busy flag and Address counter """
self.mcp.output(self.pin_rs, 0)
self.mcp.output(self.pin_rw, 1) #set rw to 1
# Configure pins for input optionally
makeinput = 1 #1 to make input, 0 to leave as output pin
self.pulseEnable() #send E pulse to set pins to 4 high bits BF,AC6,AC5,AC4
if makeinput:
for pin in self.pins_db:
self.mcp.config(pin, self.INPUT)
bits = range(8)
# get the pins values
for i in range(4):
bt = self.mcp.inputnocheck(self.pins_db[::-1][i])
bits[i]=bt
# print i,bt,bits[i]
# restore to pins to output (tried with and without)
for pin in self.pins_db:
self.mcp.config(pin, self.OUTPUT)
self.pulseEnable() #send E pulse to set pins to next 4 bits AC3,AC2,AC1,AC0
if makeinput:
for pin in self.pins_db:
self.mcp.config(pin, self.INPUT)
# get the pins values
for i in range(4,8):
bt = self.mcp.inputnocheck(self.pins_db[::-1][i-4])
bits[i]=bt
# print i,bt,bits[i]
# restore to pins to output and rw to 0
for pin in self.pins_db:
self.mcp.config(pin, self.OUTPUT)
self.mcp.output(self.pin_rw, 0) # return rw to 0
# print "Last :", bin(self.mcp.direction)[2:].zfill(16)
return bits;
In Adafruit_MCP230xx.py added a version of input with assertion removed so can input from any
combination of input/output pins
- Code: Select all
def inputnocheck(self, pin):
assert pin >= 0 and pin < self.num_gpios, "Pin number %s is invalid, only 0-%s are valid" % (pin, self.num_gpios)
#assert self.direction & (1 << pin) != 0, "Pin %s not set to input" % pin
print self.num_gpios, "pin ", pin
if self.num_gpios <= 8:
value = self.i2c.readU8(MCP23008_GPIOA)
elif self.num_gpios > 8 and self.num_gpios <= 16:
value = self.i2c.readU16(MCP23017_GPIOA)
print value, bin(value)[2:].zfill(16)
temp = value >> 8
value <<= 8
value |= temp
#bb fix return value & (1 << pin)
return (value & (1 << pin))>>pin # to make 0 or 1
Bug in Adafruit_ChartLCDPlate.py
- Code: Select all
def config(self, pin, mode):
if self.num_gpios <= 8:
self.direction = self._readandchangepin(MCP23017_IODIRA, pin, mode)
# this code produces only 8 bits and loses the information about the input/output state of the
#other pins - the MCP23012_IODIRA and B are set correctly however
#the code that follows is my correction
# if self.num_gpios <= 16:
# if (pin < 8):
# self.direction = self._readandchangepin(MCP23017_IODIRA, pin, mode)
# else:
# self.direction = self._readandchangepin(MCP23017_IODIRB, pin-8, mode)
if self.num_gpios <= 16:
if (pin < 8):
# replace low bits
self.direction = (self.direction >> 8)<<8 | self._readandchangepin(MCP23017_IODIRA, pin, mode)
else:
# replace hi bits
self.direction = (self.direction & 0xff) | self._readandchangepin(MCP23017_IODIRB, pin-8, mode) << 8
# print "config ", pin, mode, self.direction, bin(self.direction)[2:].zfill(16)
return self.direction

