-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_MCP23017.cpp
235 lines (189 loc) · 4.74 KB
/
Adafruit_MCP23017.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/***************************************************
This is a library for the MCP23017 i2c port expander
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Wire.h>
#include <avr/pgmspace.h>
#include "Adafruit_MCP23017.h"
#ifdef __AVR__
#define WIRE Wire
#else // Arduino Due
#define WIRE Wire1
#endif
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// minihelper
static inline void wiresend(uint8_t x) {
#if ARDUINO >= 100
WIRE.write((uint8_t)x);
#else
WIRE.send(x);
#endif
}
static inline uint8_t wirerecv(void) {
#if ARDUINO >= 100
return WIRE.read();
#else
return WIRE.receive();
#endif
}
////////////////////////////////////////////////////////////////////////////////
void Adafruit_MCP23017::begin(uint8_t addr) {
if (addr > 7) {
addr = 7;
}
i2caddr = addr;
WIRE.begin();
// set defaults!
WIRE.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(MCP23017_IODIRA);
wiresend(0xFF); // all inputs on port A
WIRE.endTransmission();
WIRE.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(MCP23017_IODIRB);
wiresend(0xFF); // all inputs on port B
WIRE.endTransmission();
}
void Adafruit_MCP23017::begin(void) {
begin(0);
}
void Adafruit_MCP23017::pinMode(uint8_t p, uint8_t d) {
uint8_t iodir;
uint8_t iodiraddr;
// only 16 bits!
if (p > 15)
return;
if (p < 8)
iodiraddr = MCP23017_IODIRA;
else {
iodiraddr = MCP23017_IODIRB;
p -= 8;
}
// read the current IODIR
WIRE.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(iodiraddr);
WIRE.endTransmission();
WIRE.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
iodir = wirerecv();
// set the pin and direction
if (d == INPUT) {
iodir |= 1 << p;
} else {
iodir &= ~(1 << p);
}
// write the new IODIR
WIRE.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(iodiraddr);
wiresend(iodir);
WIRE.endTransmission();
}
uint16_t Adafruit_MCP23017::readGPIOAB() {
uint16_t ba = 0;
uint8_t a;
// read the current GPIO output latches
WIRE.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(MCP23017_GPIOA);
WIRE.endTransmission();
WIRE.requestFrom(MCP23017_ADDRESS | i2caddr, 2);
a = wirerecv();
ba = wirerecv();
ba <<= 8;
ba |= a;
return ba;
}
void Adafruit_MCP23017::writeGPIOAB(uint16_t ba) {
WIRE.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(MCP23017_GPIOA);
wiresend(ba & 0xFF);
wiresend(ba >> 8);
WIRE.endTransmission();
}
void Adafruit_MCP23017::digitalWrite(uint8_t p, uint8_t d) {
uint8_t gpio;
uint8_t gpioaddr, olataddr;
// only 16 bits!
if (p > 15)
return;
if (p < 8) {
olataddr = MCP23017_OLATA;
gpioaddr = MCP23017_GPIOA;
} else {
olataddr = MCP23017_OLATB;
gpioaddr = MCP23017_GPIOB;
p -= 8;
}
// read the current GPIO output latches
WIRE.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(olataddr);
WIRE.endTransmission();
WIRE.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
gpio = wirerecv();
// set the pin and direction
if (d == HIGH) {
gpio |= 1 << p;
} else {
gpio &= ~(1 << p);
}
// write the new GPIO
WIRE.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(gpioaddr);
wiresend(gpio);
WIRE.endTransmission();
}
void Adafruit_MCP23017::pullUp(uint8_t p, uint8_t d) {
uint8_t gppu;
uint8_t gppuaddr;
// only 16 bits!
if (p > 15)
return;
if (p < 8)
gppuaddr = MCP23017_GPPUA;
else {
gppuaddr = MCP23017_GPPUB;
p -= 8;
}
// read the current pullup resistor set
WIRE.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(gppuaddr);
WIRE.endTransmission();
WIRE.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
gppu = wirerecv();
// set the pin and direction
if (d == HIGH) {
gppu |= 1 << p;
} else {
gppu &= ~(1 << p);
}
// write the new GPIO
WIRE.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(gppuaddr);
wiresend(gppu);
WIRE.endTransmission();
}
uint8_t Adafruit_MCP23017::digitalRead(uint8_t p) {
uint8_t gpioaddr;
// only 16 bits!
if (p > 15)
return 0;
if (p < 8)
gpioaddr = MCP23017_GPIOA;
else {
gpioaddr = MCP23017_GPIOB;
p -= 8;
}
// read the current GPIO
WIRE.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(gpioaddr);
WIRE.endTransmission();
WIRE.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
return (wirerecv() >> p) & 0x1;
}