121 |
pfowler |
1 |
#include <Wire.h>
|
|
|
2 |
#include "Adafruit_MCP23017.h"
|
|
|
3 |
|
|
|
4 |
// Basic pin reading and pullup test for the MCP23017 I/O expander
|
|
|
5 |
// public domain!
|
|
|
6 |
|
|
|
7 |
// Connect pin #12 of the expander to Analog 5 (i2c clock)
|
|
|
8 |
// Connect pin #13 of the expander to Analog 4 (i2c data)
|
|
|
9 |
// Connect pins #15, 16 and 17 of the expander to ground (address selection)
|
|
|
10 |
// Connect pin #9 of the expander to 5V (power)
|
|
|
11 |
// Connect pin #10 of the expander to ground (common ground)
|
|
|
12 |
|
|
|
13 |
// Output #0 is on pin 21 so connect an LED or whatever from that to ground
|
|
|
14 |
|
|
|
15 |
Adafruit_MCP23017 mcp;
|
|
|
16 |
|
|
|
17 |
uint8_t mcp_inthit = 0;
|
|
|
18 |
uint8_t leda_time = 0;
|
|
|
19 |
uint8_t ledon = 0;
|
|
|
20 |
|
|
|
21 |
void setup() {
|
|
|
22 |
Serial.begin(9600);
|
|
|
23 |
|
|
|
24 |
mcp.begin(); // use default address 0
|
|
|
25 |
|
|
|
26 |
// Setup configuration
|
|
|
27 |
mcp.writeReg(MCP23017_IOCONA, B01100000); // Dont mirror, disable sequential access
|
|
|
28 |
mcp.writeReg(MCP23017_IOCONB, B01100000); // Dont mirror, disable sequential access
|
|
|
29 |
// 1------- // BANK: 1 = Reg in different bank, 0 - Registers in seq
|
|
|
30 |
// -1------ // MIRROR: Mirror interrupts if true
|
|
|
31 |
// --1----- // SEQOP: 1 = Sequential access (AddrPtr does not increment)
|
|
|
32 |
// ---1---- // DISSLW: Slew rate control for SDA
|
|
|
33 |
// ----1--- // HAEN: Not used in MCP23017
|
|
|
34 |
// -----1-- // ODR: 1 = INT pin as open-drain, 0 = Active driver
|
|
|
35 |
// ------1- // INTPOL: Set polarity of INT output 1 = HIGH, 0 = Low
|
|
|
36 |
// -------1 //
|
|
|
37 |
|
|
|
38 |
// Setup pin directions
|
|
|
39 |
mcp.writeReg(MCP23017_IODIRA, B00000000);
|
|
|
40 |
mcp.writeReg(MCP23017_IODIRB, B10000000);
|
|
|
41 |
|
|
|
42 |
// Enable pull-ups on ports
|
|
|
43 |
mcp.writeReg(MCP23017_GPPUA, B00000000);
|
|
|
44 |
mcp.writeReg(MCP23017_GPPUB, B10000000);
|
|
|
45 |
|
|
|
46 |
// Setup pin polarity on ports
|
|
|
47 |
mcp.writeReg(MCP23017_IPOLA, B00000000);
|
|
|
48 |
mcp.writeReg(MCP23017_IPOLA, B10000000);
|
|
|
49 |
|
|
|
50 |
// Set default values for pin on change
|
|
|
51 |
mcp.writeReg(MCP23017_DEFVALA, B00000000);
|
|
|
52 |
mcp.writeReg(MCP23017_DEFVALB, B00000000);
|
|
|
53 |
|
|
|
54 |
// Enable 'default value detection' on ports
|
|
|
55 |
mcp.writeReg(MCP23017_INTCONA, B00000000);
|
|
|
56 |
mcp.writeReg(MCP23017_INTCONB, B00000000);
|
|
|
57 |
|
|
|
58 |
// Enable interrupts on ports
|
|
|
59 |
mcp.writeReg(MCP23017_GPINTENA, B00000000); //Enable interrupts on PORTA,0
|
|
|
60 |
mcp.writeReg(MCP23017_GPINTENB, B10000000); // Enable interrupts on PORTB,7
|
|
|
61 |
|
|
|
62 |
mcp.pinMode(7, OUTPUT);
|
|
|
63 |
mcp.digitalWrite(7, LOW);
|
|
|
64 |
mcp.pinMode(8, OUTPUT);
|
|
|
65 |
mcp.digitalWrite(8, LOW);
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
pinMode(13, OUTPUT);
|
|
|
69 |
|
|
|
70 |
mcp.readReg(MCP23017_INTCAPA);
|
|
|
71 |
mcp.readReg(MCP23017_INTCAPB);
|
|
|
72 |
|
|
|
73 |
attachInterrupt(0, mcp_int, FALLING);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
void loop() {
|
|
|
77 |
if (mcp_inthit) {
|
|
|
78 |
mcp_inthit = 0;
|
|
|
79 |
handleMCP_int();
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
if (leda_time>0)
|
|
|
83 |
leda_time--;
|
|
|
84 |
|
|
|
85 |
if (leda_time==0 && ledon) {
|
|
|
86 |
mcp.digitalWrite(8, 0);
|
|
|
87 |
ledon = 0;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
delay(1);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
void handleMCP_int() {
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
uint8_t srca = mcp.readReg(MCP23017_INTFA);
|
|
|
97 |
uint8_t srcb = mcp.readReg(MCP23017_INTFB);
|
|
|
98 |
|
|
|
99 |
uint8_t a = mcp.readReg(MCP23017_INTCAPA);
|
|
|
100 |
uint8_t b = mcp.readReg(MCP23017_INTCAPB);
|
|
|
101 |
|
|
|
102 |
if (bitRead(srcb, 7) && bitRead(b, 7)) {
|
|
|
103 |
mcp.digitalWrite(8, 1);
|
|
|
104 |
leda_time = 1000;
|
|
|
105 |
ledon = 1;
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
void mcp_int(void) {
|
|
|
110 |
mcp_inthit = 1;
|
|
|
111 |
}
|
|
|
112 |
|