Page 1 of 1

mcp23017

Posted: Sun Mar 01, 2015 7:23 pm
by harmsobuk
hi there,

i finally figured out the i²c bus/mcp23017 (i asked a couple of questions on this forum, but i didn't know you had to write a binary number on the bus :) ).
i managed to control a stepper motor using i²c, but only with the a0-7 ports. i did some research, but i still can't figure out how to access the b0-7 ports. i use this command to control the a-ports:

bus = smbus.SMBus(1)
bus.write_byte_data(0x20,0x00,value)

0x20 is the adress, and 0x00 makes it work with the a-ports. i read online 0x01 should work with the b ports, but that doesn't seem to work. does anyone know what i am doing wrong?

thanks in advance,
Harm

Re: mcp23017

Posted: Sun Mar 01, 2015 7:38 pm
by DougieLawson
ADDR = 0x20
IODIRA = 0x00
IODIRB = 0x01
GPIOA = 0x12
GPIOB = 0x13
OLATA = 0x14
OLATB = 0x15

With the IODIR registers a 0 bit sets the port as output, a 1 bit set the port as input.

You read input data on the GPIO registers, you write output data on the OLAT registers.

Re: mcp23017

Posted: Sun Mar 01, 2015 8:01 pm
by Richard-TX
bus.write_byte_data(address,regaddr,value)

address = i2c address of mcp23017 (0x20, etc)

where regaddr = 0x13 or 0x14 0x13 = port "B" 0x14 = port "A"
value = 8 bit value for turning on/off outputs.

You can use the "&" function to turn bits on or off.

port 1 - 0x01
port 2 = 0x02
port 3 = 0x04
port 4 = 0x08
port 5 = 0x10
port 6 = 0x20
port 7 = 0x40
port 8 = 0x80


to set all ports to outputs
bus.write_byte_data(address,0x00,0x00)
bus.write_byte_data(address,0x01,0x00)

enjoy!