1
- /* **************************************************
2
- This is a library for the MCP23008 i2c port expander
3
-
4
- These displays use I2C to communicate, 2 pins are required to
5
- interface
6
- Adafruit invests time and resources providing this open source code,
7
- please support Adafruit and open-source hardware by purchasing
8
- products from Adafruit!
9
-
10
- Written by Limor Fried/Ladyada for Adafruit Industries.
11
- BSD license, all text above must be included in any redistribution
12
- ****************************************************/
1
+ /* !
2
+ * @file Adafruit_MCP23008.cpp
3
+ *
4
+ * @mainpage Adafruit MCP23008 Library
5
+ *
6
+ * @section intro_sec Introduction
7
+ *
8
+ * This is a library for the MCP23008 i2c port expander
9
+ *
10
+ * These displays use I2C to communicate, 2 pins are required to
11
+ * interface
12
+ * Adafruit invests time and resources providing this open source code,
13
+ * please support Adafruit and open-source hardware by purchasing
14
+ * products from Adafruit!
15
+ *
16
+ * @section author Author
17
+ *
18
+ * Written by Limor Fried/Ladyada for Adafruit Industries.
19
+ *
20
+ * @section license License
21
+ *
22
+ * BSD license, all text above must be included in any redistribution
23
+ */
13
24
14
25
#if ARDUINO >= 100
15
- #include " Arduino.h"
26
+ #include " Arduino.h"
16
27
#else
17
- #include " WProgram.h"
28
+ #include " WProgram.h"
18
29
#endif
19
30
#ifdef __AVR_ATtiny85__
20
- #include < TinyWireM.h>
21
- #define Wire TinyWireM
31
+ #include < TinyWireM.h>
32
+ #define Wire TinyWireM
22
33
#else
23
- #include < Wire.h>
34
+ #include < Wire.h>
24
35
#endif
25
36
26
37
#ifdef __AVR
27
- #include < avr/pgmspace.h>
38
+ #include < avr/pgmspace.h>
28
39
#elif defined(ESP8266)
29
- #include < pgmspace.h>
40
+ #include < pgmspace.h>
30
41
#endif
31
42
#include " Adafruit_MCP23008.h"
32
43
33
-
34
44
// //////////////////////////////////////////////////////////////////////////////
35
45
// RTC_DS1307 implementation
36
46
@@ -46,7 +56,8 @@ void Adafruit_MCP23008::begin(uint8_t addr) {
46
56
Wire.beginTransmission (MCP23008_ADDRESS | i2caddr);
47
57
#if ARDUINO >= 100
48
58
Wire.write ((byte)MCP23008_IODIR);
49
- Wire.write ((byte)0xFF ); // all inputs
59
+ Wire.write ((byte)0xFF ); // all inputs
60
+ Wire.write ((byte)0x00 );
50
61
Wire.write ((byte)0x00 );
51
62
Wire.write ((byte)0x00 );
52
63
Wire.write ((byte)0x00 );
@@ -55,10 +66,10 @@ void Adafruit_MCP23008::begin(uint8_t addr) {
55
66
Wire.write ((byte)0x00 );
56
67
Wire.write ((byte)0x00 );
57
68
Wire.write ((byte)0x00 );
58
- Wire.write ((byte)0x00 );
59
69
#else
60
70
Wire.send (MCP23008_IODIR);
61
- Wire.send (0xFF ); // all inputs
71
+ Wire.send (0xFF ); // all inputs
72
+ Wire.send (0x00 );
62
73
Wire.send (0x00 );
63
74
Wire.send (0x00 );
64
75
Wire.send (0x00 );
@@ -67,29 +78,24 @@ void Adafruit_MCP23008::begin(uint8_t addr) {
67
78
Wire.send (0x00 );
68
79
Wire.send (0x00 );
69
80
Wire.send (0x00 );
70
- Wire.send (0x00 );
71
81
#endif
72
82
Wire.endTransmission ();
73
-
74
83
}
75
84
76
- void Adafruit_MCP23008::begin (void ) {
77
- begin (0 );
78
- }
85
+ void Adafruit_MCP23008::begin (void ) { begin (0 ); }
79
86
80
87
void Adafruit_MCP23008::pinMode (uint8_t p, uint8_t d) {
81
88
uint8_t iodir;
82
-
83
89
84
90
// only 8 bits!
85
91
if (p > 7 )
86
92
return ;
87
-
93
+
88
94
iodir = read8 (MCP23008_IODIR);
89
95
90
96
// set the pin and direction
91
97
if (d == INPUT) {
92
- iodir |= 1 << p;
98
+ iodir |= 1 << p;
93
99
} else {
94
100
iodir &= ~(1 << p);
95
101
}
@@ -99,18 +105,15 @@ void Adafruit_MCP23008::pinMode(uint8_t p, uint8_t d) {
99
105
}
100
106
101
107
uint8_t Adafruit_MCP23008::readGPIO (void ) {
102
- // read the current GPIO input
108
+ // read the current GPIO input
103
109
return read8 (MCP23008_GPIO);
104
110
}
105
111
106
- void Adafruit_MCP23008::writeGPIO (uint8_t gpio) {
107
- write8 (MCP23008_GPIO, gpio);
108
- }
109
-
112
+ void Adafruit_MCP23008::writeGPIO (uint8_t gpio) { write8 (MCP23008_GPIO, gpio); }
110
113
111
114
void Adafruit_MCP23008::digitalWrite (uint8_t p, uint8_t d) {
112
115
uint8_t gpio;
113
-
116
+
114
117
// only 8 bits!
115
118
if (p > 7 )
116
119
return ;
@@ -120,7 +123,7 @@ void Adafruit_MCP23008::digitalWrite(uint8_t p, uint8_t d) {
120
123
121
124
// set the pin and direction
122
125
if (d == HIGH) {
123
- gpio |= 1 << p;
126
+ gpio |= 1 << p;
124
127
} else {
125
128
gpio &= ~(1 << p);
126
129
}
@@ -131,15 +134,15 @@ void Adafruit_MCP23008::digitalWrite(uint8_t p, uint8_t d) {
131
134
132
135
void Adafruit_MCP23008::pullUp (uint8_t p, uint8_t d) {
133
136
uint8_t gppu;
134
-
137
+
135
138
// only 8 bits!
136
139
if (p > 7 )
137
140
return ;
138
141
139
142
gppu = read8 (MCP23008_GPPU);
140
143
// set the pin and direction
141
144
if (d == HIGH) {
142
- gppu |= 1 << p;
145
+ gppu |= 1 << p;
143
146
} else {
144
147
gppu &= ~(1 << p);
145
148
}
@@ -159,9 +162,9 @@ uint8_t Adafruit_MCP23008::digitalRead(uint8_t p) {
159
162
uint8_t Adafruit_MCP23008::read8 (uint8_t addr) {
160
163
Wire.beginTransmission (MCP23008_ADDRESS | i2caddr);
161
164
#if ARDUINO >= 100
162
- Wire.write ((byte)addr);
165
+ Wire.write ((byte)addr);
163
166
#else
164
- Wire.send (addr);
167
+ Wire.send (addr);
165
168
#endif
166
169
Wire.endTransmission ();
167
170
Wire.requestFrom (MCP23008_ADDRESS | i2caddr, 1 );
@@ -173,14 +176,13 @@ uint8_t Adafruit_MCP23008::read8(uint8_t addr) {
173
176
#endif
174
177
}
175
178
176
-
177
179
void Adafruit_MCP23008::write8 (uint8_t addr, uint8_t data) {
178
180
Wire.beginTransmission (MCP23008_ADDRESS | i2caddr);
179
181
#if ARDUINO >= 100
180
182
Wire.write ((byte)addr);
181
183
Wire.write ((byte)data);
182
184
#else
183
- Wire.send (addr);
185
+ Wire.send (addr);
184
186
Wire.send (data);
185
187
#endif
186
188
Wire.endTransmission ();
0 commit comments