Difference between revisions of "I/O Expander Shield with MCP23017"
Qian.zhang (talk | contribs) |
|||
Line 1: | Line 1: | ||
+ | ==Introduction== | ||
+ | |||
[[File:IOExpand shield 1.jpg|400px]] | [[File:IOExpand shield 1.jpg|400px]] | ||
[[File:IOExpand shield 2.jpg|400px]] | [[File:IOExpand shield 2.jpg|400px]] | ||
+ | |||
+ | ==Schematic== | ||
+ | |||
+ | |||
+ | |||
+ | ==Application Ideas== | ||
+ | |||
+ | '''test''' | ||
+ | <syntaxhighlight lang="c"> | ||
+ | |||
+ | #include <Wire.h> | ||
+ | |||
+ | const byte mcp_address=0x20; // I2C Address of MCP23017 Chip | ||
+ | const byte GPIOA=0x12; // Register Address of Port A | ||
+ | const byte GPIOB=0x13; // Register Address of Port B | ||
+ | const byte IODIRA=0x00; // IODIRA register | ||
+ | const byte IODIRB=0x01; // IODIRB register | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | //Send settings to MCP device | ||
+ | Wire.begin(); // join i2c bus (address optional for master) | ||
+ | Wire.beginTransmission(mcp_address); | ||
+ | Wire.write((byte)IODIRA); // IODIRA register | ||
+ | Wire.write((byte)0x03); // set GPIOA-0/GPIOA-1 to inputs | ||
+ | Wire.endTransmission(); | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | Wire.beginTransmission(mcp_address); | ||
+ | Wire.write((byte)GPIOA); // set MCP23017 memory pointer to GPIOB address | ||
+ | Wire.endTransmission(); | ||
+ | Wire.requestFrom(0x20, 1); // request one byte of data from MCP20317 | ||
+ | int inputs=Wire.read(); // store the incoming byte into "inputs" | ||
+ | |||
+ | if((inputs&0x01)==0) | ||
+ | { | ||
+ | Wire.beginTransmission(mcp_address); | ||
+ | Wire.write(GPIOA); // address bank A | ||
+ | Wire.write((byte)0x04); // value to send GPIOA-2 HIGH | ||
+ | Wire.endTransmission(); | ||
+ | } | ||
+ | if((inputs&0x02)==0) | ||
+ | { | ||
+ | Wire.beginTransmission(mcp_address); | ||
+ | Wire.write(GPIOA); // address bank A | ||
+ | Wire.write((byte)0x08); // value to send GPIOA-3 HIGH | ||
+ | Wire.endTransmission(); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Wire.beginTransmission(mcp_address); | ||
+ | Wire.write(GPIOA); // address bank A | ||
+ | Wire.write((byte)0x00); // value to send GPIOA LOW | ||
+ | Wire.endTransmission(); | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==Resources== | ||
+ | *[http://cnlearn.linksprite.com/?p=5598 Turial] |
Revision as of 06:35, 10 April 2014
Introduction
Schematic
Application Ideas
test <syntaxhighlight lang="c">
- include <Wire.h>
const byte mcp_address=0x20; // I2C Address of MCP23017 Chip const byte GPIOA=0x12; // Register Address of Port A const byte GPIOB=0x13; // Register Address of Port B const byte IODIRA=0x00; // IODIRA register const byte IODIRB=0x01; // IODIRB register
void setup() {
//Send settings to MCP device Wire.begin(); // join i2c bus (address optional for master) Wire.beginTransmission(mcp_address); Wire.write((byte)IODIRA); // IODIRA register Wire.write((byte)0x03); // set GPIOA-0/GPIOA-1 to inputs Wire.endTransmission();
}
void loop() {
Wire.beginTransmission(mcp_address); Wire.write((byte)GPIOA); // set MCP23017 memory pointer to GPIOB address Wire.endTransmission(); Wire.requestFrom(0x20, 1); // request one byte of data from MCP20317 int inputs=Wire.read(); // store the incoming byte into "inputs" if((inputs&0x01)==0) { Wire.beginTransmission(mcp_address); Wire.write(GPIOA); // address bank A Wire.write((byte)0x04); // value to send GPIOA-2 HIGH Wire.endTransmission(); } if((inputs&0x02)==0) { Wire.beginTransmission(mcp_address); Wire.write(GPIOA); // address bank A Wire.write((byte)0x08); // value to send GPIOA-3 HIGH Wire.endTransmission(); } else { Wire.beginTransmission(mcp_address); Wire.write(GPIOA); // address bank A Wire.write((byte)0x00); // value to send GPIOA LOW Wire.endTransmission(); }
} </syntaxhighlight>