joeyoung / arduino_keypads

arduino libraries for keypad interface on I2C bus
GNU General Public License v3.0
153 stars 102 forks source link

MCP23017? #1

Closed chrisatomix closed 10 years ago

chrisatomix commented 10 years ago

Is there any way of getting this library to work with the MCP23017? I've attempted to modify the library myself without success:

#define GP0 0x12        //MCP23017 GPIO reg
#define GP1 0x13        //MCP23017 GPIO reg
#define IODIR0 0x00     //MCP23017 I/O direction register
#define IODIR1 0x01     //MCP23017 I/O direction register
#define IOCON0 0x0A     //MCP23017 I/O configuration register
#define IOCON1 0x0B     //MCP23017 I/O configuration register

//#define GP0 0x00      //MCP23016 GPIO reg
//#define GP1 0x01      //MCP23016 GPIO reg
//#define IODIR0 0x06       //MCP23016 I/O direction register
//#define IODIR1 0x07       //MCP23016 I/O direction register
//#define IOCON0 0x0a   //MCP23016 I/O configuration register
//#define IOCON1 0x0b   //MCP23016 I/O configuration register

The values are based on what I found in Adafruit's library, but I'm not completely sure if I've added the right ones: https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library

The Adafruit library works exactly as it's supposed to, however my modifications to the keypad library result in a single digit appearing my Arduino console output, followed by nothing else (even when keys are pressed on the attached keypad).

I believed that there weren't many differences between the MCP23016 and the MCP23017 so the library changes wouldn't be difficult, but I could be wrong.

joeyoung commented 10 years ago

I don't have a MCP23017 to work with, so can't be completely sure what is causing the problem. The ..17 has features that support it's use as two independent 8-bit ports more completely than does the ..16, but it looks like it can be made compatible with the register access mode which is ASSUMED by the Keypad_MC16 library. That is, the library assumes auto-incrementing of the register address pointer so that two-byte accesses go to gp0 and gp1 as if they were a single 16-bit register. The ..17 datasheet identifies IOCON.BANK = 0 as the mode control bit that makes two-byte writes/reads act as 16-bit register access. This condition should be the power-up or reset default value, and the library setup should be leaving it alone. But maybe you need to also modify Keypad_MC16::begin( ) to explicitly write 0x00 (the library now writes 0x01 to IOCON0).

From my quick skim of the ..17 datasheet, it's not clear to me if the access sequence used by the library (write to the address pointer, followed by two more bytes on write, or followed by two-byte read) works the same way as it does for the ..16 -- it seems that setting the register pointer may not be needed for the ..17, so maybe doing it every time messes something up.

chrisatomix commented 10 years ago

Thankyou for such a speedy and detailed response! I'll try copying the MCP23016 library and create my own standalone MCP23017 version to see how far I can get with what you've posted above. If I get stuck I'll see if someone from Adafruit can help.

If I do manage to get it working I'll happily send you a pull request if you want to include it in your library, I've seen several other forum posts asking for the same thing so I know I'm not alone.

Thanks again for trying to set me onto the right track :)

joeyoung commented 10 years ago

I noticed one more item that might be giving you trouble with Keypad_MC16: The ..17 chip has a control register for pull-up resistors on pins configured as inputs, and the power-up default for this register(pair) is 0x0000 which means no pullups. Keypad_MC16::begin( ) does not set pullups for inputs, nor does the Keypad_MC16::pin_mode( ) function turn them on. The library's read operations expect the input pins to be pulled up. The ..16 chip does not have the internal pullups and the library's using instructions show all the pins with external pullups.

...Joe

On 17/01/14 03:18 PM, Chris Atomix wrote:

Thankyou for such a speedy and detailed response! I'll try copying the MCP23016 library and create my own standalone MCP23017 version to see how far I can get with what you've posted above. If I get stuck I'll see if someone from Adafruit can help.

If I do manage to get it working I'll happily send you a pull request if you want to include it in your library, I've seen several other forum posts asking for the same thing so I know I'm not alone.

Thanks again for trying to set me onto the right track :)

— Reply to this email directly or view it on GitHub https://github.com/joeyoung/arduino_keypads/issues/1#issuecomment-32662053.