PU2CLR PCF8574 Arduino Library  1.0.3
Arduino Library for PCF8574 Device - By Ricardo Lima Caratti
/Users/rcaratti/Desenvolvimento/eu/Arduino/PU2CLR_PCF8574T/PCF8574/pu2clr_pcf8574.h
Go to the documentation of this file.
1 /**
2  * @mainpage PCF8574 Arduino Library implementation
3  * @details This library was built based on the Datasheet "PCF8574 8-Bit I/O Expander with Serial Interface" from Microchip
4  * @details The PCF8574 device provides 8-bit, general purpose, parallel I/O expansion. It can be controlled via I²C bus applications. It is a great and inexpensive device that allow you to add more peripherals to be controlled by your Arduino board via I²C protocol.
5  * @details Some Arduino projects may require more pins than the Arduino actually has. In these cases, you can use up to 8 PCF8574 devices using only the I²C bus (two Arduino pins) and add up to 64 input / output ports to your project.
6  * @details This library can be freely distributed using the MIT Free Software model.
7  * @details This library uses the I²C communication protocol and implements most important functions offered by PCF8574 device from MicroChip. It also has primitive functions that make it easier to implement commands that may not have been implemented yet. The main features implemented can be seen below:
8  * @details 1) GPIO individual control (8 I/O pins)
9  * @details 2) I²C address customization
10  * @author Ricardo LIma Caratti (pu2clr@gmail.com)
11  * @brief It is a Library to control the PCF8574 device.
12  * @date 2021-01-20
13  * @copyright Copyright (c) 2021 Ricardo Lima Caratti
14  */
15 
16 #include <Arduino.h>
17 #include <Wire.h>
18 
19 #define DEFAULT_I2C_ADDRESS 0x20 // Default I2C Address
20 
21 #define PCF_GPIO0 0
22 #define PCF_GPIO1 1
23 #define PCF_GPIO2 2
24 #define PCF_GPIO3 3
25 #define PCF_GPIO4 4
26 #define PCF_GPIO5 5
27 #define PCF_GPIO6 6
28 #define PCF_GPIO7 7
29 
30 #define CHECK_BIT_HIGH(x, y) (x & (1 << y)) //!< Check if a bit is high. Returns 0 or != 0
31 
32 class PCF
33 {
34 protected:
35  uint8_t i2c_address = DEFAULT_I2C_ADDRESS; //!< Default i2c address
36  int reset_pin = -1; //!< Digital Arduino pin to control the PCF8574 RESET
37  bool io_error = false;
38 
39 public:
41  void reset();
42  void setup(uint8_t i2c = DEFAULT_I2C_ADDRESS);
43  void write(uint8_t value);
44  uint8_t read();
46  void digitalWrite(uint8_t port, uint8_t value);
47 
48  /**
49  * @ingroup group02
50  * @brief Returns the last wrire or read status
51  * @details Return false when the last read or write operation is not ok
52  * @return true or false
53  */
54  inline bool error(){return this->io_error;};
55 
56  /**
57  * @ingroup group01
58  * @brief Sets I2C bus to a given frequency
59  * @details 50000 = 50kHz; 100000 = 100kHz; etc
60  * @details Plese check the property I2C bus frequency/speed of your board
61  */
62  inline void setClock(long freq)
63  {
64  Wire.setClock(freq);
65  };
66 
67 };
PCF::io_error
bool io_error
Definition: pu2clr_pcf8574.h:37
PCF::i2c_address
uint8_t i2c_address
Default i2c address.
Definition: pu2clr_pcf8574.h:35
PCF::reset_pin
int reset_pin
Digital Arduino pin to control the PCF8574 RESET.
Definition: pu2clr_pcf8574.h:36
DEFAULT_I2C_ADDRESS
#define DEFAULT_I2C_ADDRESS
Definition: pu2clr_pcf8574.h:19
PCF::lookForDevice
uint8_t lookForDevice()
Look for PCF8574 device I2C Address.
Definition: pu2clr_pcf8574.cpp:28
PCF::digitalWrite
void digitalWrite(uint8_t port, uint8_t value)
Writes HIGH or LOW in a given port (pin)
Definition: pu2clr_pcf8574.cpp:106
PCF::reset
void reset()
PCF::setClock
void setClock(long freq)
Sets I2C bus to a given frequency.
Definition: pu2clr_pcf8574.h:62
PCF
Definition: pu2clr_pcf8574.h:32
PCF::setup
void setup(uint8_t i2c=DEFAULT_I2C_ADDRESS)
Starts the PCF8574.
Definition: pu2clr_pcf8574.cpp:50
PCF::digitalRead
uint8_t digitalRead(uint8_t port)
Reads a given port content (HIGH or LOW)
Definition: pu2clr_pcf8574.cpp:95
PCF::read
uint8_t read()
Gets the corrent register information.
Definition: pu2clr_pcf8574.cpp:64
PCF::write
void write(uint8_t value)
Sets a value to a given register.
Definition: pu2clr_pcf8574.cpp:78
PCF::error
bool error()
Returns the last wrire or read status.
Definition: pu2clr_pcf8574.h:54