Subversion Repositories group.electronics

Rev

Blame | Last modification | View Log | RSS feed

#include <Wire.h>
#include "Adafruit_MCP23017.h"

// Basic pin reading and pullup test for the MCP23017 I/O expander
// public domain!

// Connect pin #12 of the expander to Analog 5 (i2c clock)
// Connect pin #13 of the expander to Analog 4 (i2c data)
// Connect pins #15, 16 and 17 of the expander to ground (address selection)
// Connect pin #9 of the expander to 5V (power)
// Connect pin #10 of the expander to ground (common ground)

// Output #0 is on pin 21 so connect an LED or whatever from that to ground

Adafruit_MCP23017 mcp;

uint8_t mcp_inthit = 0;
uint8_t leda_time = 0;
uint8_t ledon = 0;
  
void setup() {
  Serial.begin(9600);
  
  mcp.begin();      // use default address 0

  // Setup configuration
  mcp.writeReg(MCP23017_IOCONA, B01100000); // Dont mirror, disable sequential access
  mcp.writeReg(MCP23017_IOCONB, B01100000); // Dont mirror, disable sequential access
  //                             1-------  // BANK: 1 = Reg in different bank, 0 - Registers in seq 
  //                             -1------  // MIRROR: Mirror interrupts if true
  //                             --1-----  // SEQOP: 1 = Sequential access (AddrPtr does not increment)
  //                             ---1----  // DISSLW: Slew rate control for SDA
  //                             ----1---  // HAEN: Not used in MCP23017
  //                             -----1--  // ODR: 1 = INT pin as open-drain, 0 = Active driver
  //                             ------1-  // INTPOL: Set polarity of INT output 1 = HIGH, 0 = Low
  //                             -------1  // 
  
  // Setup pin directions
  mcp.writeReg(MCP23017_IODIRA, B00000000);
  mcp.writeReg(MCP23017_IODIRB, B10000000);
  
  // Enable pull-ups on ports
  mcp.writeReg(MCP23017_GPPUA, B00000000);
  mcp.writeReg(MCP23017_GPPUB, B10000000);
  
  // Setup pin polarity on ports
  mcp.writeReg(MCP23017_IPOLA, B00000000);
  mcp.writeReg(MCP23017_IPOLA, B10000000);
  
  // Set default values for pin on change
  mcp.writeReg(MCP23017_DEFVALA, B00000000);
  mcp.writeReg(MCP23017_DEFVALB, B00000000);

  // Enable 'default value detection' on ports
  mcp.writeReg(MCP23017_INTCONA, B00000000);
  mcp.writeReg(MCP23017_INTCONB, B00000000);

  // Enable interrupts on ports
  mcp.writeReg(MCP23017_GPINTENA, B00000000); //Enable interrupts on PORTA,0
  mcp.writeReg(MCP23017_GPINTENB, B10000000); // Enable interrupts on PORTB,7
  
  mcp.pinMode(7, OUTPUT);
  mcp.digitalWrite(7, LOW);
  mcp.pinMode(8, OUTPUT);
  mcp.digitalWrite(8, LOW);

  
  pinMode(13, OUTPUT);
  
  mcp.readReg(MCP23017_INTCAPA);
  mcp.readReg(MCP23017_INTCAPB);  
  
  attachInterrupt(0, mcp_int, FALLING);
}

void loop() {
  if (mcp_inthit) {
    mcp_inthit = 0;
    handleMCP_int();
  }
  
  if (leda_time>0)
    leda_time--;
       
  if (leda_time==0 && ledon) {
    mcp.digitalWrite(8, 0);
    ledon = 0;
  }
 
   delay(1);
}

void handleMCP_int() {  

  
  uint8_t srca = mcp.readReg(MCP23017_INTFA);
  uint8_t srcb = mcp.readReg(MCP23017_INTFB);
  
  uint8_t a = mcp.readReg(MCP23017_INTCAPA);
  uint8_t b = mcp.readReg(MCP23017_INTCAPB);
  
  if (bitRead(srcb, 7) && bitRead(b, 7)) {
    mcp.digitalWrite(8, 1);
    leda_time = 1000;
    ledon = 1;
  }
}

void mcp_int(void) {
  mcp_inthit = 1;
}