-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCP23017.h
112 lines (94 loc) · 2.55 KB
/
MCP23017.h
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
/*
* MCP23017.h
*
* Created on: 20.02.2017
* Author: cybaer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef MCP23017_H_
#define MCP23017_H_
#define TWI_FREQ 400000L
#include "Wire.h"
#define MCP23017_ADDRESS 0x20
// registers
#define MCP23017_IODIRA 0x00
#define MCP23017_IPOLA 0x02
#define MCP23017_GPINTENA 0x04
#define MCP23017_DEFVALA 0x06
#define MCP23017_INTCONA 0x08
#define MCP23017_IOCONA 0x0A
#define MCP23017_GPPUA 0x0C
#define MCP23017_INTFA 0x0E
#define MCP23017_INTCAPA 0x10
#define MCP23017_GPIOA 0x12
#define MCP23017_OLATA 0x14
#define MCP23017_IODIRB 0x01
#define MCP23017_IPOLB 0x03
#define MCP23017_GPINTENB 0x05
#define MCP23017_DEFVALB 0x07
#define MCP23017_INTCONB 0x09
#define MCP23017_IOCONB 0x0B
#define MCP23017_GPPUB 0x0D
#define MCP23017_INTFB 0x0F
#define MCP23017_INTCAPB 0x11
#define MCP23017_GPIOB 0x13
#define MCP23017_OLATB 0x15
#define MCP23017_INT_ERR 255
class MCP23017 {
public:
void init(uint8_t addr)
{
if (addr > 7) {
addr = 7;
}
i2caddr = addr;
Wire.begin();
// port A and B all outputs
writeRegister(MCP23017_IODIRA,0);
writeRegister(MCP23017_IODIRB,0);
}
void writeGPIOAB(uint16_t ba)
{
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
Wire.write(MCP23017_GPIOA);
Wire.write(ba & 0xFF);
Wire.write(ba >> 8);
Wire.endTransmission();
}
void writeGPIOA(uint8_t data)
{
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
Wire.write(MCP23017_GPIOA);
Wire.write(data);
Wire.endTransmission();
}
void writeGPIOB(uint8_t data)
{
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
Wire.write(MCP23017_GPIOB);
Wire.write(data);
Wire.endTransmission();
}
private:
uint8_t i2caddr;
void writeRegister(uint8_t regAddr, uint8_t regValue)
{
// Write the register
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
Wire.write(regAddr);
Wire.write(regValue);
Wire.endTransmission();
}
};
#endif /* MCP23017_H_ */