PU2CLR MCP23008 Arduino Library  1.0.1
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  * @version 1.0.0
9  * @date 2020-08-07
10  *
11  * This library can be freely distributed using the MIT Free Software model.
12  *
13  * @copyright Copyright (c) 2020 Ricardo Lima Caratti
14  */
15 
16 #include <Arduino.h>
17 
18 // registers
19 #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.
20 #define REG_IPOL 0x01 //!< The IPOL register allows the user to configure thepolarity on the corresponding GPIO port bits.
21 #define REG_GPINTEN 0x02 //!< The GPINTEN register controls the interrupt-on-change feature for each pin.
22 #define REG_DEFVAL 0x03 //!< The default comparison value is configured in the DEFVAL register.
23 #define REG_INTCON 0x04 //!< The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature
24 #define REG_IOCON 0x05 //!< The IOCON register contains several bits for configuring the device
25 #define REG_GPPU 0x06 //!< The GPPU register controls the pull-up resistors for the port pins.
26 #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.
27 #define REG_INTCAP 0x08 //!< The INTCAP register captures the GPIO port value at the time the interrupt occurred.
28 #define REG_GPIO 0x09 //!< The GPIO register reflects the value on the port.
29 #define REG_OLAT 0x0A //!< The OLAT register provides access to the output latches.
30 
31 #define GPIO_INPUT 0xFF
32 #define GPIO_OUTPUT 0x00
33 
34 
35 class MCP {
36 
37  protected:
38 
39  uint8_t i2cAddress = 0x20; // Default i2c address
40  uint8_t gpios = 0; // REG_GPIO shadow register
41 
42  public:
43  void setup(uint8_t i2c = 0x20, uint8_t io = GPIO_OUTPUT);
44  void setGPIOS(uint8_t value);
46  void setRegister(uint8_t reg, uint8_t value);
47  void turnGpioOn(uint8_t gpio);
48  void turnGpioOff(uint8_t gpio);
49  void pullUpGpioOn(uint8_t gpio);
50  void pullUpGpioOff(uint8_t gpio);
51 
52  /**
53  * @brief Return the current MCP GPIO pin levels
54  *
55  * @return uint8_t
56  */
57  inline uint8_t getGPIOS() { return this->gpios; };
58 
59 };
MCP::turnGpioOff
void turnGpioOff(uint8_t gpio)
Turns a given GPIO port off (low level)
Definition: pu2clr_mcp23008.cpp:81
MCP::pullUpGpioOff
void pullUpGpioOff(uint8_t gpio)
Turns intenal pull up resistor OFF to a given GPIO PIN (low level)
Definition: pu2clr_mcp23008.cpp:113
MCP::setGPIOS
void setGPIOS(uint8_t value)
Sets a value to the GPIO Register.
Definition: pu2clr_mcp23008.cpp:54
MCP
Definition: pu2clr_mcp23008.h:35
MCP::setRegister
void setRegister(uint8_t reg, uint8_t value)
Sets a value to a given register.
Definition: pu2clr_mcp23008.cpp:42
GPIO_OUTPUT
#define GPIO_OUTPUT
Definition: pu2clr_mcp23008.h:32
REG_GPIO
#define REG_GPIO
The GPIO register reflects the value on the port.
Definition: pu2clr_mcp23008.h:28
MCP::getGPIOS
uint8_t getGPIOS()
Return the current MCP GPIO pin levels.
Definition: pu2clr_mcp23008.h:57
MCP::pullUpGpioOn
void pullUpGpioOn(uint8_t gpio)
Turns intenal pull up resistor ON to a given GPIO PIN (high level)
Definition: pu2clr_mcp23008.cpp:96
MCP::turnGpioOn
void turnGpioOn(uint8_t gpio)
Turns a given GPIO port on (high level)
Definition: pu2clr_mcp23008.cpp:66
MCP::gpios
uint8_t gpios
Definition: pu2clr_mcp23008.h:40
REG_GPPU
#define REG_GPPU
The GPPU register controls the pull-up resistors for the port pins.
Definition: pu2clr_mcp23008.h:25
MCP::i2cAddress
uint8_t i2cAddress
Definition: pu2clr_mcp23008.h:39
MCP::getRegister
uint8_t getRegister(uint8_t reg)
Gets the corrent register information.
Definition: pu2clr_mcp23008.cpp:28
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:19
MCP::setup
void setup(uint8_t i2c=0x20, uint8_t io=GPIO_OUTPUT)
Starts the MCP23008.
Definition: pu2clr_mcp23008.cpp:13