forked from rstanislav/MCP23017-Arduino-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCP23017.h
105 lines (79 loc) · 3.13 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
/* MCP23017 library for Arduino
Copyright (C) 2009 David Pye <[email protected]>
Copyright (C) 2012 Kasper Skårhøj <[email protected]>
Copyright (C) 2014 Dustin Kerber <[email protected]>
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
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
/**
Version 1.0.0
(Semantic Versioning)
**/
// IMPORTANT on byte order:
// Note on Writing words: The MSB is for GPA7-0 and the LSB is for GPB7-0
// Pinnumbers 0-7 = GPB0-7, 8-15 = GPA0-7
// This comes across as slightly un-intuitive when programming
#include "Wire.h"
//Register defines from data sheet - we set IOCON.BANK to 0
//as it is easier to manage the registers sequentially.
#define MCP23017_IODIR 0x00
#define MCP23017_IPOL 0x2
#define MCP23017_GPINTEN 0x04
#define MCP23017_GPPU 0x0C
#define MCP23017_GPIO 0x12
#define MCP23017_I2C_BASE_ADDRESS 0x40
class MCP23017
{
public:
//NB the i2c address here is the value of the A0, A1, A2 pins ONLY
//as the class takes care of its internal base address.
//so i2cAddress should be between 0 and 7
MCP23017();
void begin(int i2cAddress);
bool init();
//These functions provide an 'arduino'-like functionality for accessing
//pin states/pullups etc.
void pinMode(int pin, int mode);
void digitalWrite(int pin, int val);
int digitalRead(int pin);
//These provide a more advanced mapping of the chip functionality
//See the data sheet for more information on what they do
//Returns a word with the current pin states (ie contents of the GPIO register)
word digitalGPIOWordRead();
//Allows you to write a word to the GPIO register
void digitalGPIOWordWrite(word w);
//Sets up the polarity mask that the MCP23017 supports
//if set to 1, it will flip the actual pin value.
void inputPolarityMask(word mask);
//Sets which pins are inputs or outputs (1 = input, 0 = output) NB Opposite to arduino's
//definition for these
void inputOutputMask(word mask);
//Allows enabling of the internal 100k pullup resisters (1 = enabled, 0 = disabled)
void internalPullupMask(word mask);
//Allows enabling of the pin interrupts (1 = enabled, 0 = disabled)
void interruptMask(word mask);
private:
void writeRegister(int regaddress, byte val);
word readRegister(int regaddress);
void writeRegister(int regaddress, word val);
//Our actual i2c address
byte _i2cAddress;
//Cached copies of the register vales
word _GPIO, _IODIR, _GPPU, _GPINTEN;
};
#endif