Skip to content

Commit bb15c05

Browse files
authored
Update readme.md (interrupts) + examples (#49)
- Update readme with advanced interrupts insights - kudos to ddowling for testing. - add example - fix URL examples - add Wire1 example (ESP32 + RP2040)
1 parent b89253b commit bb15c05

File tree

22 files changed

+465
-176
lines changed

22 files changed

+465
-176
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ 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.4.1] - 2023-09-23
10+
- Update readme with advanced interrupts insights
11+
- kudos to ddowling for testing.
12+
- add example
13+
- fix URL examples
14+
- add Wire1 example (ESP32 + RP2040)
15+
16+
917
## [0.4.0] - 2023-09-23
1018
- refactor API, begin()
1119
- update readme.md

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2013-2023 Rob Tillaart
3+
Copyright (c) 2013-2024 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

PCF8574.cpp

Lines changed: 1 addition & 1 deletion
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.4.0
5+
// VERSION: 0.4.1
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

PCF8574.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// FILE: PCF8574.h
44
// AUTHOR: Rob Tillaart
55
// DATE: 02-febr-2013
6-
// VERSION: 0.4.0
6+
// VERSION: 0.4.1
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
@@ -13,7 +13,7 @@
1313
#include "Wire.h"
1414

1515

16-
#define PCF8574_LIB_VERSION (F("0.4.0"))
16+
#define PCF8574_LIB_VERSION (F("0.4.1"))
1717

1818
#ifndef PCF8574_INITIAL_VALUE
1919
#define PCF8574_INITIAL_VALUE 0xFF
@@ -50,7 +50,7 @@ class PCF8574
5050

5151

5252
// added 0.1.07/08 Septillion
53-
uint8_t readButton8() { return PCF8574::readButton8(_buttonMask); }
53+
uint8_t readButton8() { return PCF8574::readButton8(_buttonMask); }
5454
uint8_t readButton8(const uint8_t mask);
5555
uint8_t readButton(const uint8_t pin);
5656
void setButtonMask(const uint8_t mask) { _buttonMask = mask; };

README.md

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
# PCF8574
1313

14-
Arduino library for PCF8574 - 8 channel I2C IO expander
14+
Arduino library for PCF8574 - 8 channel I2C IO expander.
1515

1616

1717
## Description
@@ -36,17 +36,52 @@ The library allows to read and write both single pins or 8 pins at once.
3636
Furthermore some additional functions are implemented that are playful and useful.
3737

3838

39-
#### Interrupts
39+
#### Interrupts intro
4040

4141
The PCF8574 has an interrupt output line (INT) to notify an MCU that one of the input lines has changed.
4242
This can be used to prevent active polling of the PCF8574, which can be more efficient.
4343

44+
From the datasheet:
45+
46+
_An interrupt is generated by any rising or falling edge of the port inputs in the input mode.
47+
After time, (Tiv), INT is valid. Resetting and reactivating the interrupt circuit is achieved
48+
when data on the port is **changed to the original setting** or data is **read from**, or
49+
**written to**, the port that generated the interrupt.
50+
Resetting occurs in the read mode at the acknowledge bit after the rising edge of the SCL signal,
51+
or in the write mode at the acknowledge bit after the high-to-low transition of the SCL signal._
52+
53+
So there are three scenarios how the INT is reset.
54+
55+
1. pins revert to original state (lesser known).
56+
2. read from the device (well known)
57+
3. write to the device (well known)
58+
59+
This implies that polling the PCF8574 can miss an INT in scenario 1. (see #48)
60+
In practice if you have faster polling than your signals changes this would not
61+
be a problem. E.g. tactile switches and a polling frequency > 100 Hz will work.
62+
63+
64+
#### Interrupts library
65+
4466
The library cannot handle the PCF8574 interrupts as it has no code for it.
45-
The user should catch the interrupt in his own code and can use the library to see which line has changed.
67+
The user should catch the interrupt in his own code to set a flag and can use
68+
the library to see which line has changed.
4669

47-
There are two examples to show how interrupts can be used:
48-
- PCF8574_interrupt.ino
49-
- PCF8574_rotaryEncoder.ino
70+
There are two examples to show how interrupts can be handled:
71+
72+
- **PCF8574_interrupt.ino**
73+
- **PCF8574_rotaryEncoder.ino**
74+
75+
A more advanced interrupt handler would not set a boolean flag in the interrupt
76+
routine but increase a counter (uint8_t or larger).
77+
Then it would be possible to see that:
78+
79+
1. an interrupt occurred. (counter > 0)
80+
2. if one or more interrupts are not handled (counter > 1)
81+
82+
A minimal example that shows catching missed interrupts:
83+
84+
- **PCF8574_interrupt_advanced.ino**
5085

5186

5287
#### 0.4.0 Breaking change
@@ -194,7 +229,8 @@ It is advised to use pull-up or pull-down resistors so the lines have a defined
194229

195230
#### Must
196231

197-
- keep in sync with PCF8575
232+
- update documentation.
233+
- keep in sync with PCF8575 (as far as meaningful)
198234

199235
#### Should
200236

@@ -214,3 +250,4 @@ donate through PayPal or GitHub sponsors.
214250

215251
Thank you,
216252

253+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
platforms:
2+
rpipico:
3+
board: rp2040:rp2040:rpipico
4+
package: rp2040:rp2040
5+
gcc:
6+
features:
7+
defines:
8+
- ARDUINO_ARCH_RP2040
9+
warnings:
10+
flags:
11+
12+
packages:
13+
rp2040:rp2040:
14+
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
15+
16+
compile:
17+
# Choosing to run compilation tests on 2 different Arduino platforms
18+
platforms:
19+
# - uno
20+
# - due
21+
# - zero
22+
# - leonardo
23+
# - m4
24+
- esp32
25+
# - esp8266
26+
# - mega2560
27+
- rpipico
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// FILE: PCF8574_Wire1.ino
3+
// AUTHOR: Rob Tillaart
4+
// DATE: 2016-04-30
5+
// PURPOSE: demo
6+
// URL: https://github.com/RobTillaart/PCF8574
7+
8+
9+
#include "PCF8574.h"
10+
11+
// adjust addresses if needed
12+
PCF8574 PCF(0x39, &Wire1);
13+
14+
15+
void doHigh()
16+
{
17+
PCF.write(4, HIGH);
18+
int x = PCF.read8();
19+
Serial.print("Read ");
20+
Serial.println(x, HEX);
21+
}
22+
23+
24+
void doLow()
25+
{
26+
PCF.write(4, LOW);
27+
int x = PCF.read8();
28+
Serial.print("Read ");
29+
Serial.println(x, HEX);
30+
}
31+
32+
33+
void doToggle()
34+
{
35+
PCF.toggle(4);
36+
int x = PCF.read8();
37+
Serial.print("Read ");
38+
Serial.println(x, HEX);
39+
}
40+
41+
42+
void setup()
43+
{
44+
Serial.begin(115200);
45+
Serial.println(__FILE__);
46+
Serial.print("PCF8574_LIB_VERSION:\t");
47+
Serial.println(PCF8574_LIB_VERSION);
48+
49+
Wire1.begin();
50+
51+
if (!PCF.begin())
52+
{
53+
Serial.println("could not initialize...");
54+
}
55+
if (!PCF.isConnected())
56+
{
57+
Serial.println("=> not connected");
58+
while(1);
59+
}
60+
61+
int x = PCF.read8();
62+
Serial.print("Read ");
63+
Serial.println(x, HEX);
64+
delay(1000);
65+
}
66+
67+
68+
void loop()
69+
{
70+
Serial.println("HLT");
71+
while (Serial.available() == 0);
72+
switch (Serial.read())
73+
{
74+
case 'H': doHigh(); break;
75+
case 'L': doLow(); break;
76+
case 'T': doToggle(); break;
77+
}
78+
}
79+
80+
81+
// -- END OF FILE --
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1+
platforms:
2+
rpipico:
3+
board: rp2040:rp2040:rpipico
4+
package: rp2040:rp2040
5+
gcc:
6+
features:
7+
defines:
8+
- ARDUINO_ARCH_RP2040
9+
warnings:
10+
flags:
11+
12+
packages:
13+
rp2040:rp2040:
14+
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
15+
116
compile:
217
# Choosing to run compilation tests on 2 different Arduino platforms
318
platforms:
4-
# example made for Teensy a.o.
5-
#
619
# - uno
7-
# - leonardo
820
# - due
921
# - zero
22+
# - leonardo
23+
# - m4
24+
# - esp32
25+
# - esp8266
26+
# - mega2560
27+
# - rpipico

examples/PCF8574_Wire2/PCF8574_Wire2.ino

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
1-
//
1+
//
22
// FILE: PCF8574_Wire2.ino
33
// AUTHOR: Rob Tillaart
44
// DATE: 2016-04-30
55
// PURPOSE: demo
6+
// URL: https://github.com/RobTillaart/PCF8574
67

78

89
#include "PCF8574.h"
910

1011
// adjust addresses if needed
11-
PCF8574 PCF(0x39, &Wire2);
12+
PCF8574 PCF(0x39, &Wire2); // Wire2 ==> Teensy
13+
14+
15+
void doHigh()
16+
{
17+
PCF.write(4, HIGH);
18+
int x = PCF.read8();
19+
Serial.print("Read ");
20+
Serial.println(x, HEX);
21+
}
22+
23+
24+
void doLow()
25+
{
26+
PCF.write(4, LOW);
27+
int x = PCF.read8();
28+
Serial.print("Read ");
29+
Serial.println(x, HEX);
30+
}
31+
32+
33+
void doToggle()
34+
{
35+
PCF.toggle(4);
36+
int x = PCF.read8();
37+
Serial.print("Read ");
38+
Serial.println(x, HEX);
39+
}
1240

1341

1442
void setup()
@@ -50,32 +78,4 @@ void loop()
5078
}
5179

5280

53-
void doHigh()
54-
{
55-
PCF.write(4, HIGH);
56-
int x = PCF.read8();
57-
Serial.print("Read ");
58-
Serial.println(x, HEX);
59-
}
60-
61-
62-
void doLow()
63-
{
64-
PCF.write(4, LOW);
65-
int x = PCF.read8();
66-
Serial.print("Read ");
67-
Serial.println(x, HEX);
68-
}
69-
70-
71-
void doToggle()
72-
{
73-
PCF.toggle(4);
74-
int x = PCF.read8();
75-
Serial.print("Read ");
76-
Serial.println(x, HEX);
77-
}
78-
79-
8081
// -- END OF FILE --
81-

examples/PCF8574_interrupt/PCF8574_interrupt.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// AUTHOR: Rob Tillaart
44
// DATE: 2020-12-07
55
// PURPOSE: test PCF8574 library
6+
// URL: https://github.com/RobTillaart/PCF8574
67
//
78
// TEST SETUP
89
// Connect INT pin of the PCF8574 to UNO pin 2

0 commit comments

Comments
 (0)