forked from sumotoy/gpio_expander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcp23008.cpp
176 lines (144 loc) · 3.36 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
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
#include <stdio.h>
#include <inttypes.h>
#include <Arduino.h>
#include "mcp23008.h"
#include <../Wire/Wire.h>//this chip uses wire
mcp23008::mcp23008(){
}
mcp23008::mcp23008(const uint8_t adrs){
postSetup(adrs);
}
void mcp23008::postSetup(const uint8_t adrs){
if (adrs >= 0x20 && adrs <= 0x27){// 0x20...0x27
_adrs = adrs;
_error = false;
} else {
_error = true;
}
//setup register values for this chip
IOCON = 0x05;
IODIR = 0x00;
GPPU = 0x06;
GPIO = 0x09;
GPINTEN = 0x02;
IPOL = 0x01;
DEFVAL = 0x03;
INTF = 0x07;
INTCAP = 0x08;
OLAT = 0x0A;
INTCON = 0x04;
}
void mcp23008::begin(bool protocolInitOverride) {
if (!protocolInitOverride && !_error) {
Wire.begin();
#if ARDUINO >= 157
Wire.setClock(400000UL); // Set I2C frequency to 400kHz
#else
TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
#endif
}
delay(100);
writeByte(IOCON,0b00100000);
_gpioDirection = 0xFF;//all in
_gpioState = 0x00;//all low
}
uint8_t mcp23008::readAddress(byte addr){
byte low_byte = 0;
if (!_error){
Wire.beginTransmission(_adrs);
Wire.write(addr);
Wire.endTransmission();
Wire.requestFrom((uint8_t)_adrs,(uint8_t)1);
low_byte = Wire.read();
}
return low_byte;
}
void mcp23008::gpioPinMode(uint8_t mode){
if (mode == INPUT){
_gpioDirection = 0xFF;
} else if (mode == OUTPUT){
_gpioDirection = 0x00;
_gpioState = 0x00;
} else {
_gpioDirection = mode;
}
writeByte(IODIR,_gpioDirection);
}
void mcp23008::gpioPinMode(uint8_t pin, bool mode){
if (pin < 8){//0...7
mode == INPUT ? _gpioDirection |= (1 << pin) :_gpioDirection &= ~(1 << pin);
writeByte(IODIR,_gpioDirection);
}
}
void mcp23008::gpioPort(uint8_t value){
if (value == HIGH){
_gpioState = 0xFF;
} else if (value == LOW){
_gpioState = 0x00;
} else {
_gpioState = value;
}
writeByte(GPIO,_gpioState);
}
uint8_t mcp23008::readGpioPort(){
return readAddress(GPIO);
}
uint8_t mcp23008::readGpioPortFast(){
return _gpioState;
}
int mcp23008::gpioDigitalReadFast(uint8_t pin){
int temp = 0;
if (pin < 8) temp = bitRead(_gpioState,pin);//0...7
return temp;
}
void mcp23008::portPullup(uint8_t data) {
if (data == HIGH){
_gpioState = 0xFF;
} else if (data == LOW){
_gpioState = 0x00;
} else {
_gpioState = data;
}
writeByte(GPPU, _gpioState);
}
void mcp23008::gpioDigitalWrite(uint8_t pin, bool value){
if (pin < 8){//0...7
value == HIGH ? _gpioState |= (1 << pin) : _gpioState &= ~(1 << pin);
writeByte(GPIO,_gpioState);
}
}
void mcp23008::gpioDigitalWriteFast(uint8_t pin, bool value){
if (pin < 8){//0...7
value == HIGH ? _gpioState |= (1 << pin) : _gpioState &= ~(1 << pin);
}
}
void mcp23008::gpioPortUpdate(){
writeByte(GPIO,_gpioState);
}
int mcp23008::gpioDigitalRead(uint8_t pin){
if (pin < 8) return (int)(readAddress(GPIO) & 1 << pin);
return 0;
}
uint8_t mcp23008::gpioRegisterReadByte(byte reg){
uint8_t data = 0;
if (!_error){
Wire.beginTransmission(_adrs);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom((uint8_t)_adrs,(uint8_t)1);
data = Wire.read();
}
return data;
}
void mcp23008::gpioRegisterWriteByte(byte reg,byte data){
writeByte(reg,(byte)data);
}
/* ------------------------------ Low Level ----------------*/
void mcp23008::writeByte(byte addr, byte data){
if (!_error){
Wire.beginTransmission(_adrs);
Wire.write(addr);
Wire.write(data);
Wire.endTransmission();
}
}