harmsobuk
Posts: 27
Joined: Thu Dec 04, 2014 1:27 pm

mcp23017

Sun Mar 01, 2015 7:23 pm

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

User avatar
DougieLawson
Posts: 39611
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: mcp23017

Sun Mar 01, 2015 7:38 pm

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: mcp23017

Sun Mar 01, 2015 8:01 pm

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!
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

Return to “Interfacing (DSI, CSI, I2C, etc.)”