Write to register MCP lib and MCP23008 on ESP-12F

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
pewamipy
Posts: 2
Joined: Mon Jun 15, 2020 3:16 pm

Write to register MCP lib and MCP23008 on ESP-12F

Post by pewamipy » Mon Jun 15, 2020 5:44 pm

Hi guys

My first post here, and I am a bit stuck as a quite new programmer in Micropython

I am using a MCP23008 hooked up to a ESP-12F (8266) small blue board.

I have this lib MCP installed
https://github.com/ShrimpingIt/micropython-mcp230xx

Everything is working OK in the examples as this

Code: Select all

io = mcp.MCP23008()

# controls some output pins
outPins = list(range(0,7))
nextVals = {}
for pinNum in outPins:
    io.setup(pinNum, mcp.OUT)
    nextVals[pinNum] = True
io.output_pins(nextVals)
Hovever this is a loop thru pin 1 to 8 , in the code definition I se that this def io.output_pins takes a list but how is that working.

What I want to do is write to the OLAT or GPIO register with a whole byte like io.output_pins(5) to set pin 3 and 1 at once.
There is another def write_gpio() that seems to be able to write to the gpio register, but that is not working either. I can not
for example write io.write_gpio(5) gives an TypeError: object with buffer protocol required

What am I missing ?

pewamipy
Posts: 2
Joined: Mon Jun 15, 2020 3:16 pm

Re: Write to register MCP lib and MCP23008 on ESP-12F

Post by pewamipy » Fri Jul 24, 2020 9:05 am

SOLVED

I have to write str type argument like this

Code: Select all

m = MCP23008(i2c = i2c, address = 32)  
m.IODIR = '0'  # sets all GPIO to outputs 
m.OLAT = '3'   # sets pin 1 and 2 ON 

User avatar
jimmo
Posts: 2262
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Write to register MCP lib and MCP23008 on ESP-12F

Post by jimmo » Mon Jul 27, 2020 3:36 am

pewamipy wrote:
Fri Jul 24, 2020 9:05 am
SOLVED

I have to write str type argument like this

Code: Select all

m = MCP23008(i2c = i2c, address = 32)  
m.IODIR = '0'  # sets all GPIO to outputs 
m.OLAT = '3'   # sets pin 1 and 2 ON 
I'm surprised this works? IODIR and OLAT are just constants that tell it what register to use.
pewamipy wrote:
Mon Jun 15, 2020 5:44 pm
There is another def write_gpio() that seems to be able to write to the gpio register, but that is not working either. I can not
for example write io.write_gpio(5) gives an TypeError: object with buffer protocol required
This means you need to pass a "bytes" object (or something that looks like a byte string" to the function.

In the driver, self.gpio is that byte string (a bytearray). The output_list function modifies it, and the writes it directly to the register (via write_gpio / writeList).

So if you know exactly what values you want to write to the register, you can call write_gpio directly with your own bytes or bytearray. Something like...

Code: Select all

b = bytearray(1)
b[0] = <register value>
m.write_gpio(b)

User avatar
mcauser
Posts: 492
Joined: Mon Jun 15, 2015 8:03 am

Re: Write to register MCP lib and MCP23008 on ESP-12F

Post by mcauser » Sat Aug 22, 2020 2:36 pm

ie. to avoid the TypeError, wrap your value in a bytearray:

Code: Select all

# change
io.write_gpio(5)
# to
io.write_gpio(bytearray([5]))
The OLAT register address is 0x0A, but when the port pin is in output mode (pin's IODIR bit =0), you write to the GPIO register instead and it internally updates the OLAT register for the pins configured as output, skipping those set to input.

To set the first two pins high:

Code: Select all

io.write_iodir(bytearray([0])) # set all pins as output
io.write_gpio(bytearray([3])) # first 2 output high, rest output low
Difference between reading the GPIO and OLAT registers is that the OLAT register will only tell you which pins are set to output and are set to output high. If there are pins set to input, and the are wired to vcc, they will show as 1s in the GPIO register but 0s in the OLAT register. The GPIO register contains a mix of inputs and outputs on read and write.

I wrote a driver for the MCP23017: https://github.com/mcauser/micropython-mcp23017
I'll make a cut down version for the MCP23008. It's basically the same chip, but has 2x 8-bit ports instead of 1x.

Post Reply