MCP23017/MCP23S17 16-Bit I/O Expander with Serial Interface (I2C/SPI)

Used Products:

1. 4.7K
2. Arduino UNO
3. MCP23017-E/SP

Content:

Çeşme: https://www.youtube.com/watch?v=0Y8RDt3y2W0 http://arduinolearning.com/code/arduino-and-mcp23017-example.php

Used Libraries:

1. MCP23017

Code:

************************************************** WITH LIBRARY ************************************************** #include <Wire.h> #include <MCP23017.h> int current = 0; MCP23017 GPIOchip; void setup() { Serial.begin(9600); Serial.println("Serial Started"); Wire.begin(); // Wire must be started! GPIOchip.begin(B000); // Address set to zero. GPIOchip.init(); for (int i = 0; i < 16; i++) { GPIOchip.pinMode(i, OUTPUT); } } void loop() { for (int i = 0; i < 16; i++) { GPIOchip.digitalWrite(i, current==i); } if (++current > 15) { current = 0; } delay(100); } ************************************************** WITHOUT LIBRARY ************************************************** #include "Wire.h" byte b; unsigned int pattern[17] = { 0b0000000000000000, 0b0000000000000001, 0b0000000000000010, 0b0000000000000100, 0b0000000000001000, 0b0000000000010000, 0b0000000000100000, 0b0000000001000000, 0b0000000010000000, 0b0000000100000000, 0b0000001000000000, 0b0000010000000000, 0b0000100000000000, 0b0001000000000000, 0b0010000000000000, 0b0100000000000000, 0b1000000000000000 }; void setup() { Serial.begin(9600); Wire.begin(); // wake up I2C bus // set I/O pins to outputs Wire.beginTransmission(0x20); Wire.write(0x00); // IODIRA register Wire.write(0x00); // set all of port A to outputs Wire.endTransmission(); } void loop() { for (int i = 0; i < 9*2; i++) { b = highByte(pattern[i]); Wire.beginTransmission(0x20); Wire.write(0x12); // address bank B Wire.write((byte)b); Wire.endTransmission(); b = lowByte(pattern[i]); Serial.print(b, BIN); Serial.println(); Wire.beginTransmission(0x20); Wire.write(0x13); // address bank A Wire.write((byte)b); Wire.endTransmission(); delay(100); } }