Skip to content

Commit e130e62

Browse files
authored
Add select functions (#35)
* add select() functions * add example
1 parent d60077e commit e130e62

File tree

10 files changed

+171
-26
lines changed

10 files changed

+171
-26
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.3.5] - 2022-06-17
10+
11+
### Added
12+
- add select(), selectN(), selectNone() and selectAll()
13+
convenience wrappers
14+
15+
916
## [0.3.4] - 2022-04-11
1017

1118
### Added
1219
- add CHANGELOG.md
1320

14-
### Changed
1521

1622
### Fixed
1723
- **begin(int sda, int scl)** int parameters for ESP alike.

PCF8574.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// FILE: PCF8574.cpp
33
// AUTHOR: Rob Tillaart
44
// DATE: 02-febr-2013
5-
// VERSION: 0.3.4
5+
// VERSION: 0.3.5
66
// PURPOSE: Arduino library for PCF8574 - 8 channel I2C IO expander
77
// URL: https://github.com/RobTillaart/PCF8574
88
// http://forum.arduino.cc/index.php?topic=184800
@@ -27,7 +27,7 @@ PCF8574::PCF8574(const uint8_t deviceAddress, TwoWire *wire)
2727
#if defined (ESP8266) || defined(ESP32)
2828
bool PCF8574::begin(int dataPin, int clockPin, uint8_t value)
2929
{
30-
_wire = &Wire;
30+
_wire = &Wire;
3131
if ((dataPin < 255) && (clockPin < 255))
3232
{
3333
_wire->begin(dataPin, clockPin);
@@ -224,5 +224,22 @@ uint8_t PCF8574::readButton(const uint8_t pin)
224224
}
225225

226226

227+
void PCF8574::select(const uint8_t pin)
228+
{
229+
uint8_t n = 0x00;
230+
if (pin < 8) n = 1 << pin;
231+
write8(n);
232+
};
233+
234+
235+
void PCF8574::selectN(const uint8_t pin)
236+
{
237+
uint8_t n = 0xFF;
238+
if (pin < 8) n = (2 << pin) - 1;
239+
write8(n);
240+
};
241+
242+
243+
227244
// -- END OF FILE --
228245

PCF8574.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@
33
// FILE: PCF8574.h
44
// AUTHOR: Rob Tillaart
55
// DATE: 02-febr-2013
6-
// VERSION: 0.3.4
6+
// VERSION: 0.3.5
77
// PURPOSE: Arduino library for PCF8574 - 8 channel I2C IO expander
88
// URL: https://github.com/RobTillaart/PCF8574
99
// http://forum.arduino.cc/index.php?topic=184800
1010
//
11-
// HISTORY:
12-
// see PCF8574.cpp file
13-
//
1411

1512

1613
#include "Arduino.h"
1714
#include "Wire.h"
1815

1916

20-
#define PCF8574_LIB_VERSION (F("0.3.4"))
17+
#define PCF8574_LIB_VERSION (F("0.3.5"))
2118

2219
#ifndef PCF8574_INITIAL_VALUE
2320
#define PCF8574_INITIAL_VALUE 0xFF
@@ -57,11 +54,11 @@ class PCF8574
5754

5855

5956
//added 0.1.07/08 Septillion
60-
inline uint8_t readButton8() { return PCF8574::readButton8(_buttonMask); }
61-
uint8_t readButton8(const uint8_t mask);
62-
uint8_t readButton(const uint8_t pin);
63-
inline void setButtonMask(const uint8_t mask) { _buttonMask = mask; };
64-
uint8_t getButtonMask() { return _buttonMask; };
57+
uint8_t readButton8() { return PCF8574::readButton8(_buttonMask); }
58+
uint8_t readButton8(const uint8_t mask);
59+
uint8_t readButton(const uint8_t pin);
60+
void setButtonMask(const uint8_t mask) { _buttonMask = mask; };
61+
uint8_t getButtonMask() { return _buttonMask; };
6562

6663

6764
// rotate, shift, toggle, reverse expect all lines are output
@@ -74,6 +71,12 @@ class PCF8574
7471
void reverse();
7572

7673

74+
void select(const uint8_t pin);
75+
void selectN(const uint8_t pin);
76+
void selectNone() { write8(0x00); };
77+
void selectAll() { write8(0xFF); };
78+
79+
7780
int lastError();
7881

7982

README.md

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,27 @@ These chips are identical in behaviour although there are two distinct address r
2525

2626
So you can connect up to 16 PCF8574 on one I2C bus, giving access
2727
to 16 x 8 = 128 IO lines. To maximize IO lines combine 8 x PCF8575 + 8 x PCF8574A giving
28-
128 + 64 = 192 IO lines. Be sure to have a well dimensioned power supply.
28+
128 + 64 = 192 IO lines.
29+
Be sure to have a well dimensioned power supply.
2930

3031
The library allows to read and write both single pins or 8 pins at once.
31-
Furthermore some additional functions are implemented that are playful but useful.
32+
Furthermore some additional functions are implemented that are playful and useful.
33+
34+
35+
## I2C Clock
36+
37+
Tested on UNO with **PCF8574_performance** showed that the PCF8574 still works at 500 KHz and failed at 600 KHz.
38+
These values are outside the specs of the datasheet so they are not recommended.
39+
However when performance is needed you can try to overclock the chip.
40+
41+
| clock speed | Read | Write | Notes |
42+
|:-----------:|:------:|:-------:|:------------------|
43+
| 100000 | 236 | 240 | spec datasheet |
44+
| 200000 | 132 | 140 |
45+
| 300000 | 104 | 108 |
46+
| 400000 | 96 | 96 | max advised speed |
47+
| 500000 | 92 | 92 | not recommended |
48+
| 600000 | crash | crash |
3249

3350

3451
## Interface
@@ -59,7 +76,7 @@ in the class this is faster than reread the pins.
5976
- **void write8(const uint8_t value)** writes all 8 pins at once. This one does the actual writing.
6077
- **uint8_t write(const uint8_t pin, const uint8_t value)** writes a single pin; pin = 0..7;
6178
value is HIGH(1) or LOW (0)
62-
- **valueOut()** returns the last written data.
79+
- **uint8_t valueOut()** returns the last written data.
6380

6481

6582
### Button
@@ -90,12 +107,26 @@ Fills the higher lines with zero's.
90107
Fills the lower lines with zero's.
91108
- **void rotateRight(const uint8_t n = 1)** rotates output channels to right, moving lowest line to highest line.
92109
- **void rotateLeft(const uint8_t n = 1)** rotates output channels to left, moving highest line to lowest line.
93-
- **void reverse()** reverse the "bit pattern" of the lines, swapping pin 7 with 0, 6 with 1, 5 with 2 and 4 with 3.
110+
- **void reverse()** reverse the "bit pattern" of the lines, swapping pin 7 with 0, 6 with 1, 5 with 2 etc.
111+
112+
113+
### Select
114+
115+
Some convenience wrappers.
116+
117+
- **void select(const uint8_t pin)** sets a single pin to HIGH, all others are set to LOW.
118+
If pin > 7 all pins are set to LOW.
119+
Can be used to select one of n devices.
120+
- **void selectN(const uint8_t pin)** sets pins 0..pin to HIGH, all others are set to LOW.
121+
If pin > 7 all pins are set to LOW.
122+
This can typical be used to implement a VU meter.
123+
- **void selectNone()** sets all pins to LOW.
124+
- **void selectAll()** sets all pins to HIGH.
94125

95126

96-
### Misc
127+
### Miscellaneous
97128

98-
- **int lastError()** returns the last error from the lib. (see .h file)
129+
- **int lastError()** returns the last error from the lib. (see .h file).
99130

100131

101132
## Error codes
@@ -109,13 +140,13 @@ Fills the lower lines with zero's.
109140

110141
## Operation
111142

112-
See examples
143+
See examples.
113144

114145
It is advised to use pull-up or pull-down resistors so the lines have a defined state at startup.
115146

116147

117148
## Future
118149

119-
-
150+
-
120151

121152

examples/PCF8574_performance/PCF8574_performance.ino

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void setup()
2222
PCF.begin();
2323
Serial.println(PCF.isConnected());
2424

25-
for (long clk = 100000; clk < 500000; clk += 50000)
25+
for (long clk = 100000; clk < 800000; clk += 100000)
2626
{
2727
Serial.println(clk);
2828
Wire.setClock(clk);
@@ -49,4 +49,3 @@ void loop()
4949

5050

5151
// -- END OF FILE --
52-
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
...\PCF8574_performance.ino
2+
PCF8574_LIB_VERSION: 0.3.5
3+
1
4+
100000
5+
Read: 236
6+
Write: 240
7+
200000
8+
Read: 132
9+
Write: 140
10+
300000
11+
Read: 104
12+
Write: 108
13+
400000
14+
Read: 96
15+
Write: 96
16+
500000
17+
Read: 92
18+
Write: 92
19+
600
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// FILE: PCF8574_select.ino
3+
// AUTHOR: Rob Tillaart
4+
// DATE: 2022-06-18
5+
// PUPROSE: demo PCF8574 library select functions
6+
7+
8+
9+
#include "PCF8574.h"
10+
11+
PCF8574 PCF(0x38);
12+
13+
uint32_t start, stop;
14+
15+
16+
void setup()
17+
{
18+
Serial.begin(115200);
19+
Serial.println(__FILE__);
20+
Serial.print("PCF8574_LIB_VERSION:\t");
21+
Serial.println(PCF8574_LIB_VERSION);
22+
23+
PCF.begin();
24+
Serial.println(PCF.isConnected());
25+
Serial.println();
26+
27+
PCF.selectAll();
28+
delay(1000);
29+
PCF.selectNone();
30+
delay(1000);
31+
32+
// VU meter up
33+
for (int i = 0; i < 7; i++)
34+
{
35+
PCF.selectN(i);
36+
delay(100);
37+
}
38+
39+
// VU meter down
40+
for (int i = 7; i >= 0; i--)
41+
{
42+
PCF.selectN(i);
43+
delay(100);
44+
}
45+
}
46+
47+
48+
void loop()
49+
{
50+
// night rider
51+
for (int i = 0; i < 7; i++)
52+
{
53+
PCF.select(i);
54+
delay(100);
55+
}
56+
for (int i = 7; i >= 0; i--)
57+
{
58+
PCF.select(i);
59+
delay(100);
60+
}
61+
}
62+
63+
64+
// -- END OF FILE --

keywords.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@ toggle KEYWORD2
2727
toggleMask KEYWORD2
2828
shiftRight KEYWORD2
2929
shiftLeft KEYWORD2
30+
3031
rotateRight KEYWORD2
3132
rotateLeft KEYWORD2
3233
reverse KEYWORD2
33-
lastError KEYWORD2
34+
35+
select KEYWORD2
36+
selectN KEYWORD2
37+
selectNone KEYWORD2
38+
selectAll KEYWORD2
3439

3540

3641
# Constants ( LITERAL1)
3742
PCF8574_LIB_VERSION LITERAL1
43+
PCF8574_INITIAL_VALUE LITERAL1
3844
PCF8574_OK LITERAL1
3945
PCF8574_PIN_ERROR LITERAL1
4046
PCF8574_I2C_ERROR LITERAL1

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/PCF8574.git"
1717
},
18-
"version": "0.3.4",
18+
"version": "0.3.5",
1919
"license": "MIT",
2020
"frameworks": "arduino",
2121
"platforms": "*",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=PCF8574
2-
version=0.3.4
2+
version=0.3.5
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library for PCF8574 - 8 channel I2C IO expander

0 commit comments

Comments
 (0)