-
Notifications
You must be signed in to change notification settings - Fork 29
/
mcp23018.cpp
213 lines (176 loc) · 4.26 KB
/
mcp23018.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdio.h>
#include <inttypes.h>
#include <Arduino.h>
#include "mcp23018.h"
#include <../Wire/Wire.h>//this chip uses wire
mcp23018::mcp23018(){
}
mcp23018::mcp23018(const uint8_t adrs){
postSetup(adrs);
}
void mcp23018::postSetup(const uint8_t adrs){
if (adrs >= 0x20 && adrs <= 0x27){//HAEN works between 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 mcp23018::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);//read datasheet for details!
_gpioDirection = 0xFFFF;//all in
_gpioState = 0x0000;//all low
}
uint16_t mcp23018::readAddress(byte addr){
byte low_byte = 0;
byte high_byte = 0;
if (!_error){
Wire.beginTransmission(_adrs);
Wire.write(addr);
Wire.endTransmission();
Wire.requestFrom((uint8_t)_adrs,(uint8_t)2);
low_byte = Wire.read();
high_byte = Wire.read();
}
return byte2word(high_byte,low_byte);
}
void mcp23018::gpioPinMode(uint16_t mode){
if (mode == INPUT){
_gpioDirection = 0xFFFF;
} else if (mode == OUTPUT){
_gpioDirection = 0x0000;
_gpioState = 0x0000;
} else {
_gpioDirection = mode;
}
writeWord(IODIR,_gpioDirection);
}
void mcp23018::gpioPinMode(uint8_t pin, bool mode){
if (pin < 16){//0...15
mode == INPUT ? _gpioDirection |= (1 << pin) :_gpioDirection &= ~(1 << pin);
writeWord(IODIR,_gpioDirection);
}
}
void mcp23018::gpioPort(uint16_t value){
if (value == HIGH){
_gpioState = 0xFFFF;
} else if (value == LOW){
_gpioState = 0x0000;
} else {
_gpioState = value;
}
writeWord(GPIO,_gpioState);
}
void mcp23018::gpioPort(byte lowByte, byte highByte){
_gpioState = byte2word(highByte,lowByte);
writeWord(GPIO,_gpioState);
}
uint16_t mcp23018::readGpioPort(){
return readAddress(GPIO);
}
uint16_t mcp23018::readGpioPortFast(){
return _gpioState;
}
int mcp23018::gpioDigitalReadFast(uint8_t pin){
int temp = 0;
if (pin < 16) temp = bitRead(_gpioState,pin);
return temp;
}
void mcp23018::portPullup(uint16_t data) {
if (data == HIGH){
_gpioState = 0xFFFF;
} else if (data == LOW){
_gpioState = 0x0000;
} else {
_gpioState = data;
}
writeWord(GPPU, _gpioState);
}
void mcp23018::gpioDigitalWrite(uint8_t pin, bool value){
if (pin < 16){//0...15
value == HIGH ? _gpioState |= (1 << pin) : _gpioState &= ~(1 << pin);
writeWord(GPIO,_gpioState);
}
}
void mcp23018::gpioDigitalWriteFast(uint8_t pin, bool value){
if (pin < 16){//0...15
value == HIGH ? _gpioState |= (1 << pin) : _gpioState &= ~(1 << pin);
}
}
void mcp23018::gpioPortUpdate(){
writeWord(GPIO,_gpioState);
}
int mcp23018::gpioDigitalRead(uint8_t pin){
if (pin < 16) return (int)(readAddress(GPIO) & 1 << pin);
return 0;
}
uint8_t mcp23018::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;
}
uint16_t mcp23018::gpioRegisterReadWord(byte reg){
uint16_t data = 0;
if (!_error){
Wire.beginTransmission(_adrs);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom((uint8_t)_adrs,(uint8_t)1);
data = Wire.read();
data += Wire.read() << 8;//Ironicster
}
return data;
}
void mcp23018::gpioRegisterWriteByte(byte reg,byte data){
writeByte(reg,data);
}
void mcp23018::gpioRegisterWriteWord(byte reg,word data){
writeWord(reg,data);
}
/* ------------------------------ Low Level ----------------*/
void mcp23018::writeByte(byte addr, byte data){
if (!_error){
Wire.beginTransmission(_adrs);
Wire.write(addr);
Wire.write(data);
Wire.endTransmission();
}
}
void mcp23018::writeWord(byte addr, uint16_t data){
if (!_error){
Wire.beginTransmission(_adrs);
Wire.write(addr);
//Wire.write(word2lowByte(data));
//Wire.write(word2highByte(data));
Wire.write(data >> 8);
Wire.write(data & 0xFF);
Wire.endTransmission();
}
}