forked from thaussma/OpenGarden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_MCP23017.h
92 lines (77 loc) · 3.22 KB
/
Adafruit_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
/*!
* @file Adafruit_MCP23017.h
*/
#ifndef _Adafruit_MCP23017_H_
#define _Adafruit_MCP23017_H_
// Don't forget the Wire library
#ifndef ARDUINO_AVR_GEMMA
// TinyWireM is now part of
// Adafruit version of Wire Library, so this
// will work with Adafruit ATtiny85's
// But Arduino Gemma doesn't use that library
// We do NOT want to include Wire if it's an arduino Gemma
#include <Wire.h>
#else
#include <TinyWireM.h>
#define Wire TinyWireM
#endif
/*!
* @brief MCP23017 main class
*/
class Adafruit_MCP23017 {
public:
void begin(uint8_t addr, TwoWire *theWire = &Wire);
void begin(TwoWire *theWire = &Wire);
void pinMode(uint8_t p, uint8_t d);
void digitalWrite(uint8_t p, uint8_t d);
void pullUp(uint8_t p, uint8_t d);
uint8_t digitalRead(uint8_t p);
void writeGPIOAB(uint16_t);
uint16_t readGPIOAB();
uint8_t readGPIO(uint8_t b);
void setupInterrupts(uint8_t mirroring, uint8_t open, uint8_t polarity);
void setupInterruptPin(uint8_t pin, uint8_t mode);
void disableInterruptPin(uint8_t pin);
uint8_t getLastInterruptPin();
uint8_t getLastInterruptPinValue();
private:
uint8_t i2caddr;
TwoWire *_wire; //!< pointer to a TwoWire object
uint8_t bitForPin(uint8_t pin);
uint8_t regForPin(uint8_t pin, uint8_t portAaddr, uint8_t portBaddr);
uint8_t readRegister(uint8_t addr);
void writeRegister(uint8_t addr, uint8_t value);
/**
* Utility private method to update a register associated with a pin (whether
* port A/B) reads its value, updates the particular bit, and writes its
* value.
*/
void updateRegisterBit(uint8_t p, uint8_t pValue, uint8_t portAaddr,
uint8_t portBaddr);
};
#define MCP23017_ADDRESS 0x20 //!< MCP23017 Address
// registers
#define MCP23017_IODIRA 0x00 //!< I/O direction register A
#define MCP23017_IPOLA 0x02 //!< Input polarity port register A
#define MCP23017_GPINTENA 0x04 //!< Interrupt-on-change pins A
#define MCP23017_DEFVALA 0x06 //!< Default value register A
#define MCP23017_INTCONA 0x08 //!< Interrupt-on-change control register A
#define MCP23017_IOCONA 0x0A //!< I/O expander configuration register A
#define MCP23017_GPPUA 0x0C //!< GPIO pull-up resistor register A
#define MCP23017_INTFA 0x0E //!< Interrupt flag register A
#define MCP23017_INTCAPA 0x10 //!< Interrupt captured value for port register A
#define MCP23017_GPIOA 0x12 //!< General purpose I/O port register A
#define MCP23017_OLATA 0x14 //!< Output latch register 0 A
#define MCP23017_IODIRB 0x01 //!< I/O direction register B
#define MCP23017_IPOLB 0x03 //!< Input polarity port register B
#define MCP23017_GPINTENB 0x05 //!< Interrupt-on-change pins B
#define MCP23017_DEFVALB 0x07 //!< Default value register B
#define MCP23017_INTCONB 0x09 //!< Interrupt-on-change control register B
#define MCP23017_IOCONB 0x0B //!< I/O expander configuration register B
#define MCP23017_GPPUB 0x0D //!< GPIO pull-up resistor register B
#define MCP23017_INTFB 0x0F //!< Interrupt flag register B
#define MCP23017_INTCAPB 0x11 //!< Interrupt captured value for port register B
#define MCP23017_GPIOB 0x13 //!< General purpose I/O port register B
#define MCP23017_OLATB 0x15 //!< Output latch register 0 B
#define MCP23017_INT_ERR 255 //!< Interrupt error
#endif