//##################################################################################################################################### // IOX-16 Arduino Output Demo program - writes test pattern to LEDs on both ports // 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 will turn the LEDs on and off in a set of patterns. The LEDs can be connected to either port. // // The IOX-16 must be set up as follows: // * The address is set up to 0 // * The output pins are connected to LEDs with appropriate current limit resistors // // Port pin ----/\/\/\----------------->|----- Ground // 330 ohm resistor LED // // Duplicate the above circuit for each Port pin (0-7) // // Revision 1.00 4/19/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 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 IODIRB = 0x01; // Port B direction register const byte GPIOA = 0x12; // Register Address of Port A const byte GPIOB = 0x13; // Register Address of Port B #define MAXPAT 15 //Number of LED test patterns // the testPat array holds the patterns to output to turn the LEDs on and off. A 1 will turn the LED on. const byte testPat[] = {0xff,0x00,0xaa,0x55,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x0f}; void setup() { Wire.begin(); // join i2c bus //The port pins can be either inputs or outputs. Set both ports to all outputs 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)0x00); // set all of bank A to outputs. 0 = output, 1 = input Wire.write((byte)0x00); // set all of bank B to outputs. If we do multiple writes inside a single I2C command, the port address increments Wire.endTransmission(); // all I2C commands must end with this statement } /*** end of setup() ***/ void loop() { //Each pass through the loop will incremend idx which is used to index the pattern in the testPat[] array for(byte idx = 0;idx < MAXPAT; idx++) { Wire.beginTransmission(IOX_BASE_ADR); //All accesses to the expander start with this statement to the chip address Wire.write(GPIOA); // address bank A Wire.write((byte)testPat[idx]); // value to send Wire.endTransmission(); //after sending the pattern to Port A, repeat to Port B Wire.beginTransmission(IOX_BASE_ADR); Wire.write(GPIOB); // address bank B Wire.write((byte)testPat[idx]); // value to send Wire.endTransmission(); delay(300); // wait 300 miliseconds before changing to the next pattern } } /*** endof loop() ***/