-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMCP23008.cpp
72 lines (59 loc) · 1.28 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
#include "MCP23008.h"
bool MCP23008::TryInitialize(byte address) {
bool result = false;
m_address = address;
Write8(MCP23008_IODIR, 0xAA);
if(Read8(MCP23008_IODIR) == 0xAA) {
Write8(MCP23008_IODIR, 0x55);
if(Read8(MCP23008_IODIR) == 0x55) {
result = true;
}
}
if(result) {
// Inputs with pull up
Write8(MCP23008_IODIR, 0xFF);
Write8(MCP23008_GPPU, 0xFF);
}
return result;
}
void MCP23008::PinMode(byte port, byte mode) {
byte iodir;
iodir = Read8(MCP23008_IODIR);
if (mode == INPUT) {
iodir |= 1 << port;
} else {
iodir &= ~(1 << port);
}
Write8(MCP23008_IODIR, iodir);
}
byte MCP23008::GetGPIOs() {
return Read8(MCP23008_GPIO);
}
void MCP23008::SetGPIOs(byte gpio) {
Write8(MCP23008_GPIO, gpio);
}
void MCP23008::PullUp(byte port, byte direction) {
byte gppu = Read8(MCP23008_GPPU);
if (direction == HIGH) {
gppu |= 1 << port;
}
else {
gppu &= ~(1 << port);
}
Write8(MCP23008_GPPU, gppu);
}
byte MCP23008::DigitalRead(byte port) {
return (GetGPIOs() >> port) & 0x1;
}
void MCP23008::DigitalWrite(byte port, byte data) {
if (port < 8) {
byte gpio = GetGPIOs();
if (data == HIGH) {
gpio |= 1 << port;
}
else {
gpio &= ~(1 << port);
}
SetGPIOs(gpio);
}
}