Easiest way to write to a single port of MCP23017 - i2c 16 i
Re: Easiest way to write to a single port of MCP23017 - i2c
Re: Easiest way to write to a single port of MCP23017 - i2c
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.
Re: Easiest way to write to a single port of MCP23017 - i2c
void writeGPIOA(uint8_t);
void writeGPIOB(uint8_t);
/**
* Writes port A pins only. -wmr
*/
void Adafruit_MCP23017::writeGPIOA(uint8_t a) {
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(MCP23017_GPIOA);
wiresend(a);
Wire.endTransmission();
}
/**
* Writes port B pins only. -wmr
*/
void Adafruit_MCP23017::writeGPIOB(uint8_t b) {
// Serial.print("GPIOB b: ");Serial.println(b);
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(MCP23017_GPIOB);
wiresend(b);
Wire.endTransmission();
}
const unsigned int mcpRedButton = 5; // input-pullup
const unsigned int mcpBlackButton = 6; // "
mcp.pinMode(mcpRedButton, INPUT_PULLUP);
mcp.pinMode(mcpBlackButton, INPUT_PULLUP);
b1 = mcp.digitalRead(mcpBlackButton); // read the buttons
b2 = mcp.digitalRead(mcpRedButton);
Re: Easiest way to write to a single port of MCP23017 - i2c
#include <Wire.h>
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp; //GPIO expander initialize
const unsigned int mcpRedButton = 5; // input-pullup
const unsigned int mcpBlackButton = 6; // "
void setup() {
Serial.begin(2400);
mcp.begin();
mcp.pinMode(mcpRedButton, INPUT_PULLUP);
mcp.pinMode(mcpBlackButton, INPUT_PULLUP);
}
void loop() {
Serial.println("*");
Serial.println(mcp.digitalRead(mcpBlackButton)); // read the buttons
Serial.println(mcp.digitalRead(mcpRedButton));
}
Re: Easiest way to write to a single port of MCP23017 - i2c
Re: Easiest way to write to a single port of MCP23017 - i2c
Re: Easiest way to write to a single port of MCP23017 - i2c
Re: Easiest way to write to a single port of MCP23017 - i2c
Re: Easiest way to write to a single port of MCP23017 - i2c
kcl1s wrote:This line mcp.pinMode(mcpRedButton, INPUT_PULLUP); actually sets the pin as an output as the keyword INPUT_PULLUP has a value of 2 where the keyword INPUT has a value of 0. Since the library is doing a bitWrite with that passed value it interprets anything not 0 as a 1 which sets the pin to output.
Fellow hobbyist
Keith
Re: Easiest way to write to a single port of MCP23017 - i2c
Re: Easiest way to write to a single port of MCP23017 - i2c
adafruit_support_bill wrote:Interesting. We use these chips in several of our boards and have not run into difficulties like that with them that I am aware of.
I'll run this by some of the other engineers.
15:18:34.543 -> mcp:00
15:18:34.577 -> mcp2:11
15:18:35.050 -> mcp:00
15:18:35.083 -> mcp2:11
15:18:35.557 -> mcp:00
15:18:35.591 -> mcp2:10
15:18:36.068 -> mcp:00
15:18:36.104 -> mcp2:01
15:18:36.544 -> mcp:00
15:18:36.578 -> mcp2:11
15:18:37.052 -> mcp:00
15:18:37.087 -> mcp2:11
15:18:37.557 -> mcp:00
#include <Wire.h>
#include "Adafruit_MCP23017.h"
// Basic pin reading and pullup test for the MCP23017 I/O expander
// public domain!
Adafruit_MCP23017 mcp;
Adafruit_MCP23017 mcp2;
void setup() {
Serial.begin(2400);
mcp.begin(0); // use default address 0
mcp.pinMode(5, INPUT);
mcp.pullUp(5, HIGH); // turn on a 100K pullup internally
mcp.pinMode(6, INPUT);
mcp.pullUp(6, HIGH); // turn on a 100K pullup internally
mcp2.begin(1); // use default address 0
mcp2.pinMode(5, INPUT);
mcp2.pullUp(5, HIGH); // turn on a 100K pullup internally
mcp2.pinMode(6, INPUT);
mcp2.pullUp(6, HIGH); // turn on a 100K pullup internally
}
void loop() {
Serial.print("mcp:"); Serial.print(mcp.digitalRead(5)); Serial.println(mcp.digitalRead(6));
Serial.print("mcp2:"); Serial.print(mcp2.digitalRead(5)); Serial.println(mcp2.digitalRead(6));
delay(500);
}
Re: Easiest way to write to a single port of MCP23017 - i2c
Re: Easiest way to write to a single port of MCP23017 - i2c
adafruit2 wrote:i think start with one mcp chip, get that working perfectly the way you like - then add the second? its hard to debug when you have a lot going on :)
Re: Easiest way to write to a single port of MCP23017 - i2c
kcl1s wrote:No. Don't use INPUT_PULLUP use INPUT then mcp.pullUp on the next line as in the example.
Keith