-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_MCP23008.cpp
161 lines (130 loc) · 3.42 KB
/
Adafruit_MCP23008.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
#include "Adafruit_MCP23008.h"
// This is a library for the MCP23008 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
////////////////////////////////////////////////////////////////////////////////
// RTC_DS1307 implementation
void Adafruit_MCP23008::begin(uint8_t addr) {
if (addr > 7) {
addr = 7;
}
i2caddr = addr;
Wire.begin();
delayMicroseconds(40);
// set defaults!
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
delayMicroseconds(40);
Wire.write((byte)MCP23008_IODIR);
delayMicroseconds(40);
Wire.write((byte)0xFF); // all inputs
delayMicroseconds(40);
Wire.write((byte)0x00);
delayMicroseconds(40);
Wire.write((byte)0x00);
delayMicroseconds(40);
Wire.write((byte)0x00);
delayMicroseconds(40);
Wire.write((byte)0x00);
delayMicroseconds(40);
Wire.write((byte)0x00);
delayMicroseconds(40);
Wire.write((byte)0x00);
delayMicroseconds(40);
Wire.write((byte)0x00);
delayMicroseconds(40);
Wire.write((byte)0x00);
delayMicroseconds(40);
Wire.write((byte)0x00);
delayMicroseconds(40);
Wire.endTransmission();
delayMicroseconds(40);
}
void Adafruit_MCP23008::begin(void) {
begin(0);
}
void Adafruit_MCP23008::pinMode(uint8_t p, uint8_t d) {
uint8_t iodir;
// only 8 bits!
if (p > 7)
return;
iodir = read8(MCP23008_IODIR);
// set the pin and direction
if (d == INPUT) {
iodir |= 1 << p;
} else {
iodir &= ~(1 << p);
}
// write the new IODIR
write8(MCP23008_IODIR, iodir);
}
uint8_t Adafruit_MCP23008::readGPIO(void) {
// read the current GPIO input
return read8(MCP23008_GPIO);
}
void Adafruit_MCP23008::writeGPIO(uint8_t gpio) {
write8(MCP23008_GPIO, gpio);
}
void Adafruit_MCP23008::digitalWrite(uint8_t p, uint8_t d) {
uint8_t gpio;
// only 8 bits!
if (p > 7)
return;
// read the current GPIO output latches
gpio = readGPIO();
// set the pin and direction
if (d == HIGH) {
gpio |= 1 << p;
} else {
gpio &= ~(1 << p);
}
// write the new GPIO
writeGPIO(gpio);
}
void Adafruit_MCP23008::pullUp(uint8_t p, uint8_t d) {
uint8_t gppu;
// only 8 bits!
if (p > 7)
return;
gppu = read8(MCP23008_GPPU);
// set the pin and direction
if (d == HIGH) {
gppu |= 1 << p;
} else {
gppu &= ~(1 << p);
}
// write the new GPIO
write8(MCP23008_GPPU, gppu);
}
uint8_t Adafruit_MCP23008::digitalRead(uint8_t p) {
// only 8 bits!
if (p > 7)
return 0;
// read the current GPIO
return (readGPIO() >> p) & 0x1;
}
uint8_t Adafruit_MCP23008::read8(uint8_t addr) {
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
delayMicroseconds(40);
Wire.write((byte)addr);
delayMicroseconds(40);
Wire.endTransmission();
delayMicroseconds(40);
Wire.requestFrom(MCP23008_ADDRESS | i2caddr, 1);
delayMicroseconds(40);
return Wire.read();
}
void Adafruit_MCP23008::write8(uint8_t addr, uint8_t data) {
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
delayMicroseconds(40);
Wire.write((byte)addr);
delayMicroseconds(40);
Wire.write((byte)data);
delayMicroseconds(40);
Wire.endTransmission();
delayMicroseconds(40);
}