//#################################################################################################################################### // IOX-16 Arduino Input Demo program - Read the values of the inputs and send the results to the Arduino Serial Monitor // This program is a simple example to show how to set up the IOX-16 or other Arduino shield that uses a Microchip MCP23017 I/O expander IC. // It reads Port A and prints the the results in the Serial Monitor. // // The IOX-16 must be set up as follows: // * The address is set up to 0 // * For a simple test Port A data pins can be connected to ground through switches // // Port A pin ----- \--------------- Ground // Switch // // Duplicate the above circuit for each Port A pin (0-7) // // Enter this program and Upload to the Arduino. Once upload is completed, open the Serial Monitor in the Tools tab. If a switch is closed // the corresponding pin will show up as a 1 on the Serial Monitor display // Revision 1.00 4/20/2013 Gary C. Sutcliffe and Unified Microsystems www.unifiedmicro.com // This program is released to the public domain //************************************************************************************************************************************ #include //The I2C communications uses the Wire library. You might need to download from Arduino.cc and put into your library directory const byte IOX_BASE_ADR =0x20; // Base Address of MCP23017 Chip with address lines all set to zero (grounded) // Address for second MCP23017 would be 0x21, the next 0x22, etc. //MCP23017 internal registers - Not all registers included. See the Microchip MCP23017 datasheet for full list const byte IODIRA = 0x00; // Port A direction register. Write a 0 to make a pin an output, a 1 to make it an input const byte GPIOA = 0x12; // Register Address of Port A - read data from or write output data to this port const byte GPPUA = 0x0c; // Register to enable the internal pull up resistors on Port A. 1 = pull up enabled void setup() { Serial.begin(9600); //configure the serial port so we can use the Serial Monitor Wire.begin(); // join i2c bus //The port pins can be either inputs or outputs. Set Port A pins as all inputs Wire.beginTransmission(IOX_BASE_ADR); //all I2C commands begin with this statement and the address of the chip. Wire.write((byte)IODIRA); // select a write to the IODIRA register Wire.write((byte)0xff); // set all of bank A to inputs. 0 = output, 1 = input Wire.endTransmission(); // all I2C commands must end with this statement // This sets the pull ups on the port. Don't do this if port is actively driven or circuit has external pull ups. Wire.beginTransmission(IOX_BASE_ADR); Wire.write((byte)GPPUA); // Select port A pull up register Wire.write((byte)0xff); //turn all the pull ups on. 1=ON, 0=OFF Wire.endTransmission(); } /*** end of setup() ***/ void loop() { //Port A will be read each pass through the loop and the values on Port A will be displayed in the Serial Monitor byte readData; // location to store data from Port A Wire.beginTransmission(IOX_BASE_ADR); //need to set up the proper register before reading it Wire.write(GPIOA); // Even though we are doing a read, we first have to write the address of the register the read will be from Wire.endTransmission(); Wire.requestFrom(IOX_BASE_ADR,(byte)1); //Now we start the actual read while (Wire.available()==0); //wait unit the data gets back readData = ~Wire.read(); // get the data, note bitwise inversion to make it easier to tell what switch is closed Wire.endTransmission(); Serial.println(readData,BIN); //Print out results on Serial Monitor. delay(500); // wait 500 miliseconds before checking again } /*** end of loop() ***/