0

Easiest way to write to a single port of MCP23017 - i2c 16 i
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Easiest way to write to a single port of MCP23017 - i2c 16 i

by wrichman on Fri Sep 13, 2019 1:12 pm

There's a "write all 16 bits" function in the library, but I need to just write to the B port all at once, which "pushes buttons" on a matrix keyboard just fine using analog multiplexers, but now I need to start using the other 8 bits, and there doesn't seem to be a function to write *just* the A port or *just* the B port. I guess I could keep the desired values for all 16 bits in a 16-bit variable and update the bits in that, and then dump the whole thing to the MCP23017 each time, but I was hoping there might be an easier way. I don't really want to take the time to delve too far into the library and the functionality of the chip if I don't have to, so I'm hoping someone can offer some insight. Any suggestions?

-Bill

wrichman
 
Posts: 9
Joined: Fri Apr 12, 2019 11:29 am

Re: Easiest way to write to a single port of MCP23017 - i2c

by adafruit_support_bill on Fri Sep 13, 2019 1:22 pm

There is no function in the library for that. But the library code is rather straightforward. It would not be difficult to hack for your needs:
https://github.com/adafruit/Adafruit-MC ... P23017.cpp

adafruit_support_bill
 
Posts: 74364
Joined: Sat Feb 07, 2009 10:11 am

Re: Easiest way to write to a single port of MCP23017 - i2c

by kcl1s on Fri Sep 13, 2019 4:03 pm

I could keep the desired values for all 16 bits in a 16-bit variable and update the bits in that, and then dump the whole thing to the MCP23017 each time, but I was hoping there might be an easier way.

I did this for a relay controller project where I wanted the relays to set at the same time. This page helped me with the bit logic. https://playground.arduino.cc/Code/BitMath/#common The quick reference at the bottom shows how to set a bit with a single line of code for (1) and another line of code for (0). I just made a small function and passed in a place value (n= 0 to 15) and a bool toggle value (state = 0 or 1) then a single if else to direct to the line of code to set a bit in my relays variable. I called the writeGPIOAB when I wanted to set all the relays.

Fellow hobbyist
Keith

kcl1s
 
Posts: 1256
Joined: Tue Aug 30, 2016 12:06 pm

Please be positive and constructive with your questions and comments.