-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCP23008.cpp
143 lines (112 loc) · 2.83 KB
/
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
// Code by Adafruit Industries/Limor Fried
// License: LGPL
#include <Wire.h>
#include <avr/pgmspace.h>
#include "MCP23008.h"
#include <Arduino.h>
////////////////////////////////////////////////////////////////////////////////
// RTC_DS1307 implementation
void MCP23008::begin(uint8_t addr) {
if (addr > 7) {
addr = 7;
}
i2caddr = addr;
Wire.begin();
// set defaults!
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write(MCP23008_IODIR);
Wire.write(0xFF); // all inputs
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x00);
Wire.endTransmission();
}
void MCP23008::begin(void) {
begin(0);
}
void MCP23008::pinMode(uint8_t p, uint8_t d) {
uint8_t iodir;
// only 8 bits!
if (p > 7)
return;
// read the current IODIR
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write(MCP23008_IODIR);
Wire.endTransmission();
Wire.requestFrom(MCP23008_ADDRESS | i2caddr, 1);
iodir = Wire.read();
// set the pin and direction
if (d == INPUT) {
iodir |= 1 << p;
} else {
iodir &= ~(1 << p);
}
// write the new IODIR
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write(MCP23008_IODIR);
Wire.write(iodir);
Wire.endTransmission();
}
void MCP23008::digitalWrite(uint8_t p, uint8_t d) {
uint8_t gpio;
// only 8 bits!
if (p > 7)
return;
// read the current GPIO output latches
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write(MCP23008_OLAT);
Wire.endTransmission();
Wire.requestFrom(MCP23008_ADDRESS | i2caddr, 1);
gpio = Wire.read();
// set the pin and direction
if (d == HIGH) {
gpio |= 1 << p;
} else {
gpio &= ~(1 << p);
}
// write the new GPIO
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write(MCP23008_GPIO);
Wire.write(gpio);
Wire.endTransmission();
}
void MCP23008::pullUp(uint8_t p, uint8_t d) {
uint8_t gppu;
// only 8 bits!
if (p > 7)
return;
// read the current pullup resistor set
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write(MCP23008_GPPU);
Wire.endTransmission();
Wire.requestFrom(MCP23008_ADDRESS | i2caddr, 1);
gppu = Wire.read();
// set the pin and direction
if (d == HIGH) {
gppu |= 1 << p;
} else {
gppu &= ~(1 << p);
}
// write the new GPIO
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write(MCP23008_GPPU);
Wire.write(gppu);
Wire.endTransmission();
}
uint8_t MCP23008::digitalRead(uint8_t p) {
// only 8 bits!
if (p > 7)
return 0;
// read the current GPIO
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write(MCP23008_GPIO);
Wire.endTransmission();
Wire.requestFrom(MCP23008_ADDRESS | i2caddr, 1);
return (Wire.read() >> p) & 0x1;
}