0

MCP23017 Write to all registers
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MCP23017 Write to all registers

by jkarlbarlow on Thu Dec 17, 2020 11:03 am

I am in the planning stage of a project that will include 3 MCP23017 I/O expanders on a single I2C bus. I have looked through what documentation I can find and haven't found a clear answer as to whether you can write more than one bit of data at a time in a single instruction. For example, if you try to write 2 bits of data, will the library "overflow" the extra bit automatically into the next register? I have a feeling this does not work but I don't have parts yet to test and it seems like this would be useful.
I know I can use a FOR loop and write each register that way and this may be what I have to do. Just thought I would ask in case anyone else had tried this,
Thanks,

jkarl

jkarlbarlow
 
Posts: 8
Joined: Fri Feb 16, 2018 1:01 am

Re: MCP23017 Write to all registers

by dastels on Thu Dec 17, 2020 12:12 pm

Do you mean each output bit? Since you posted in an Arduino forum, I'm assuming you are using c++. The ADafruit library lets you write both A&B ports in one call (using 16-bit data) and read the 8 bits from either A or B. See https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library.

Dave

dastels
 
Posts: 5396
Joined: Tue Oct 20, 2015 3:22 pm

Re: MCP23017 Write to all registers

by jkarlbarlow on Thu Dec 17, 2020 12:30 pm

Thanks for the quick reply, that sounds exactly what what I was hoping for! Looking at the library you linked to, is writeGPIOAB the command I would use to write to all 16 outputs at once? I did not see any example that would read or write to more than one at a time. Something like:

Adafruit_MCP23017 mcp;

Code: Select all | TOGGLE FULL SIZE
void setup() { 
  mcp.begin();      // use default address 0

  for(int pin = 0, pin < 16, pin++) {
    mcp.pinMode(pin, OUTPUT);
  }
}

void loop() {
  mcp.writeGPIOAB(43690); //binary 1010101010101010
  delay(100);
  mcp.writeGPIOAB(21845); //binary 0101010101010101
  delay(100);
}


Josh

jkarlbarlow
 
Posts: 8
Joined: Fri Feb 16, 2018 1:01 am

Re: MCP23017 Write to all registers

by dastels on Thu Dec 17, 2020 12:41 pm

You are correct. Looking at the code, I believe the low 8 bits are port A, the high 8 are port B. That's easy to confirm experimentally.

Dave

dastels
 
Posts: 5396
Joined: Tue Oct 20, 2015 3:22 pm

Re: MCP23017 Write to all registers

by jkarlbarlow on Thu Dec 17, 2020 12:54 pm

Thanks, Dave! I will give it a try once I have the parts. You’re input is greatly appreciated.

jkarlbarlow
 
Posts: 8
Joined: Fri Feb 16, 2018 1:01 am

Please be positive and constructive with your questions and comments.