PU2CLR MCP23008 Arduino Library  1.0.3
Arduino Library for MCP23008 Device - By Ricardo Lima Caratti
/Users/rcaratti/Desenvolvimento/eu/Arduino/PU2CLR_MCP23008/pu2clr_mcp23008.h
Go to the documentation of this file.
1 /**
2  * @file mcp23008.h
3  *
4  * This library was built based on the Datasheet "MCP23008/MCP23S08 8-Bit I/O Expander with Serial Interface" from Microchip
5  *
6  * @author Ricardo LIma Caratti (pu2clr@gmail.com)
7  * @brief It is a Library to control the MCP23008 device.
8  * @date 2021-01-06
9  *
10  * This library can be freely distributed using the MIT Free Software model.
11  *
12  * @copyright Copyright (c) 2020 Ricardo Lima Caratti
13  */
14 
15 #include <Arduino.h>
16 
17 // registers
18 #define REG_IODIR 0x00 //!< Controls the direction of the data I/O. When a bit is set, the corresponding pin becomes an input. When a bit is clear, the corresponding pin becomes an output.
19 #define REG_IPOL 0x01 //!< The IPOL register allows the user to configure the polarity on the corresponding GPIO port bits.
20 #define REG_GPINTEN 0x02 //!< The GPINTEN register controls the interrupt-on-change feature for each pin.
21 #define REG_DEFVAL 0x03 //!< The default comparison value is configured in the DEFVAL register.
22 #define REG_INTCON 0x04 //!< The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature
23 #define REG_IOCON 0x05 //!< The IOCON register contains several bits for configuring the device. See method: setIoCon
24 #define REG_GPPU 0x06 //!< The GPPU register controls the pull-up resistors for the port pins.
25 #define REG_INTF 0x07 //!< The INTF register reflects the interrupt condition on the port pins of any pin that is enabled for interrupts via the GPINTEN register.
26 #define REG_INTCAP 0x08 //!< The INTCAP register captures the GPIO port value at the time the interrupt occurred.
27 #define REG_GPIO 0x09 //!< The GPIO register reflects the value on the port.
28 #define REG_OLAT 0x0A //!< The OLAT register provides access to the output latches.
29 
30 #define GPIO_INPUT 0xFF
31 #define GPIO_OUTPUT 0x00
32 
33 /**
34  * @brief IOCON register structure - I/O EXPANDER CONFIGURATION REGISTER (ADDR 0x05)
35  * @details The IOCON register contains several bits for configuring the device.
36  */
37 typedef union
38 {
39  struct
40  {
41  uint8_t DUMMY1 : 1; //!< Unimplemented
42  uint8_t INTPOL : 1; //!< This bit sets the polarity of the INT output pin. 1= Active-high. 0 = Active - low.
43  uint8_t ODR : 1; //!< This bit configures the INT pin as an open-drain output. 1 = Open-drain. 0 = Active driver.
44  uint8_t HAEN : 1; //!< Hardware Address Enable bit (MCP23S08 only). 1 = Enables.
45  uint8_t DISSLW : 1; //!< Slew Rate control bit for SDA output. 1= Slewratedisabled. 0= Slewrateenabled.
46  uint8_t SEQOP : 1; //!< Sequential Operation mode bit. 1 = Sequential operation disabled, address pointer does not increment.
47  uint8_t DUMMY2 : 2; //!< Unimplemented
48  } arg;
50 } mcp23008_ioncon;
51 
52 class MCP
53 {
54 
55 protected:
56  uint8_t i2cAddress = 0x20; // Default i2c address
57  uint8_t gpios = 0; // REG_GPIO shadow register
60 
61 public:
63  void setup(uint8_t i2c = 0x20, uint8_t io = GPIO_OUTPUT);
65  void setRegister(uint8_t reg, uint8_t value);
66  void turnGpioOn(uint8_t gpio);
67  void turnGpioOff(uint8_t gpio);
68  void pullUpGpioOn(uint8_t gpio);
69  void pullUpGpioOff(uint8_t gpio);
70  void setIoCon(uint8_t INTPOL, uint8_t ODR, uint8_t HAEN, uint8_t DISSLW, uint8_t SEQOP);
71  void invertGpioPolarity();
72  void setInterrupt(uint8_t polatity = 0, uint8_t openDrainOutput = 0);
73  void interruptGpioOn(uint8_t gpio, uint8_t bitCompare = 1) ;
74  bool gpioRead(uint8_t gpio);
75  void gpioWrite(uint8_t gpio, uint8_t value);
76 
77  /**
78  * @ingroup group01
79  * @brief Returns the current MCP GPIO pin levels
80  *
81  * @return uint8_t
82  */
83  inline uint8_t getGPIOS()
84  {
85  this->gpios = getRegister(REG_GPIO);
86  return this->gpios;
87  };
88 
89  /**
90  * @ingroup group01
91  * @brief Sets a value to the GPIO Register
92  * @details A direct way to set a given value to deal with the GPIOs pins.
93  * @param value (8 bits)
94  */
95  inline void setGPIOS(uint8_t value) {
96  this->setRegister(REG_GPIO, value);
97  }
98 
99  /**
100  * @ingroup group01
101  * @brief Returns the last value of INTCAP register (value immediately after the last interrupt event)
102  *
103  * @return uint8_t
104  */
106  {
107  this->intcap = getRegister(REG_INTCAP);
108  return this->intcap;
109  };
110 
111  /**
112  * @ingroup group01
113  * @brief Returns the last value of INTCAP register (value immediately after the last interrupt event)
114  * @details The INTF register reflects the interrupt condition on the port pins of any pin that is enabled for interrupts via the GPINTEN register. A ‘set’ bit indicates that the associated pin caused the interrupt.
115  * @return uint8_t
116  */
117  inline uint8_t getINTF()
118  {
119  this->intf = getRegister(REG_INTF);
120  return this->intf;
121  };
122 
123 };
MCP::setRegister
void setRegister(uint8_t reg, uint8_t value)
Sets a value to a given register.
Definition: pu2clr_mcp23008.cpp:84
REG_INTCAP
#define REG_INTCAP
The INTCAP register captures the GPIO port value at the time the interrupt occurred.
Definition: pu2clr_mcp23008.h:26
MCP::intcap
uint8_t intcap
Definition: pu2clr_mcp23008.h:58
MCP::gpioWrite
void gpioWrite(uint8_t gpio, uint8_t value)
Sets a given value (high(1) or low(0) ) to a given gpio pin.
Definition: pu2clr_mcp23008.cpp:144
REG_DEFVAL
#define REG_DEFVAL
The default comparison value is configured in the DEFVAL register.
Definition: pu2clr_mcp23008.h:21
MCP::setIoCon
void setIoCon(uint8_t INTPOL, uint8_t ODR, uint8_t HAEN, uint8_t DISSLW, uint8_t SEQOP)
Sets the IO Configurarion gerister.
Definition: pu2clr_mcp23008.cpp:203
MCP
Definition: pu2clr_mcp23008.h:52
MCP::getINTCAP
uint8_t getINTCAP()
Returns the last value of INTCAP register (value immediately after the last interrupt event)
Definition: pu2clr_mcp23008.h:105
MCP::getINTF
uint8_t getINTF()
Returns the last value of INTCAP register (value immediately after the last interrupt event)
Definition: pu2clr_mcp23008.h:117
MCP::interruptGpioOn
void interruptGpioOn(uint8_t gpio, uint8_t bitCompare=1)
Sets the interrupt-on-change feature to a given GPIO pin.
Definition: pu2clr_mcp23008.cpp:260
GPIO_OUTPUT
#define GPIO_OUTPUT
Definition: pu2clr_mcp23008.h:31
MCP::pullUpGpioOff
void pullUpGpioOff(uint8_t gpio)
Turns intenal pull up resistor OFF to a given GPIO PIN (low level)
Definition: pu2clr_mcp23008.cpp:175
MCP::intf
uint8_t intf
Definition: pu2clr_mcp23008.h:59
REG_GPIO
#define REG_GPIO
The GPIO register reflects the value on the port.
Definition: pu2clr_mcp23008.h:27
MCP::getRegister
uint8_t getRegister(uint8_t reg)
Gets the corrent register information.
Definition: pu2clr_mcp23008.cpp:68
MCP::setGPIOS
void setGPIOS(uint8_t value)
Sets a value to the GPIO Register.
Definition: pu2clr_mcp23008.h:95
REG_IOCON
#define REG_IOCON
The IOCON register contains several bits for configuring the device. See method: setIoCon.
Definition: pu2clr_mcp23008.h:23
MCP::gpioRead
bool gpioRead(uint8_t gpio)
Reads the status (high or low) or a given GPIO.
Definition: pu2clr_mcp23008.cpp:131
MCP::gpios
uint8_t gpios
Definition: pu2clr_mcp23008.h:57
MCP::getGPIOS
uint8_t getGPIOS()
Returns the current MCP GPIO pin levels.
Definition: pu2clr_mcp23008.h:83
MCP::turnGpioOff
void turnGpioOff(uint8_t gpio)
Turns a given GPIO port off (low level)
Definition: pu2clr_mcp23008.cpp:115
REG_INTF
#define REG_INTF
The INTF register reflects the interrupt condition on the port pins of any pin that is enabled for in...
Definition: pu2clr_mcp23008.h:25
MCP::setInterrupt
void setInterrupt(uint8_t polatity=0, uint8_t openDrainOutput=0)
Configures the MCP23008 interrupt feature.
Definition: pu2clr_mcp23008.cpp:240
MCP::lookForDevice
uint8_t lookForDevice()
Look for MCP23008 device I2C Address.
Definition: pu2clr_mcp23008.cpp:28
MCP::invertGpioPolarity
void invertGpioPolarity()
Inverts the polarity of the all GPIO port bits.
Definition: pu2clr_mcp23008.cpp:224
MCP::turnGpioOn
void turnGpioOn(uint8_t gpio)
Turns a given GPIO port on (high level)
Definition: pu2clr_mcp23008.cpp:99
REG_IPOL
#define REG_IPOL
The IPOL register allows the user to configure the polarity on the corresponding GPIO port bits.
Definition: pu2clr_mcp23008.h:19
REG_GPPU
#define REG_GPPU
The GPPU register controls the pull-up resistors for the port pins.
Definition: pu2clr_mcp23008.h:24
mcp23008_ioncon::raw
uint8_t raw
Definition: pu2clr_mcp23008.h:49
MCP::i2cAddress
uint8_t i2cAddress
Definition: pu2clr_mcp23008.h:56
MCP::setup
void setup(uint8_t i2c=0x20, uint8_t io=GPIO_OUTPUT)
Starts the MCP23008.
Definition: pu2clr_mcp23008.cpp:52
MCP::pullUpGpioOn
void pullUpGpioOn(uint8_t gpio)
Turns intenal pull up resistor ON to a given GPIO PIN (high level)
Definition: pu2clr_mcp23008.cpp:157
REG_IODIR
#define REG_IODIR
Controls the direction of the data I/O. When a bit is set, the corresponding pin becomes an input....
Definition: pu2clr_mcp23008.h:18
REG_GPINTEN
#define REG_GPINTEN
The GPINTEN register controls the interrupt-on-change feature for each pin.
Definition: pu2clr_mcp23008.h:20