Skip to content

Commit 1c1de9b

Browse files
committed
+ version 0.1.04 (0.1.03 = intermediate)
+ removed beginTransmission from read8() -> speed++ + removed endTransmission from read8() -> speed + changed address to uint8_t
1 parent b527f06 commit 1c1de9b

File tree

2 files changed

+83
-68
lines changed

2 files changed

+83
-68
lines changed

libraries/PCF8574/PCF8574.cpp

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,103 +2,118 @@
22
// FILE: PCF8574.cpp
33
// AUTHOR: Rob Tillaart
44
// DATE: 02-febr-2013
5-
// VERSION: 0.1.02
5+
// VERSION: 0.1.04
66
// PURPOSE: I2C PCF8574 library for Arduino
7-
// URL:
7+
// URL:
88
//
99
// HISTORY:
10+
// 0.1.04 2015-05-09 removed ambiguity in read8()
11+
// 0.1.03 2015-03-02 address int -> uint8_t
1012
// 0.1.02 replaced ints with uint8_t to reduce footprint;
1113
// added default value for shiftLeft() and shiftRight()
1214
// renamed status() to lastError();
13-
// 0.1.01 added value(); returns last read 8 bit value (cached);
15+
// 0.1.01 added value(); returns last read 8 bit value (cached);
1416
// value() does not always reflect the latest state of the pins!
1517
// 0.1.00 initial version
16-
//
18+
//
1719

1820
#include "PCF8574.h"
1921

2022
#include <Wire.h>
2123

22-
PCF8574::PCF8574(int address)
24+
PCF8574::PCF8574(uint8_t deviceAddress)
2325
{
24-
_address = address;
25-
Wire.begin();
26+
_address = deviceAddress;
27+
Wire.begin();
28+
// TWBR = 12; // 400KHz
2629
}
2730

31+
// removed Wire.beginTransmission(addr);
32+
// with @100KHz -> 265 micros()
33+
// without @100KHz -> 132 micros()
34+
// without @400KHz -> 52 micros()
35+
// TODO @800KHz -> ??
2836
uint8_t PCF8574::read8()
2937
{
30-
Wire.beginTransmission(_address);
31-
Wire.requestFrom(_address, 1);
38+
if (Wire.requestFrom(_address, (uint8_t)1) != 1)
39+
{
40+
_error = 10;
41+
return _data; // last value
42+
}
3243
#if (ARDUINO < 100)
33-
_data = Wire.receive();
44+
_data = Wire.receive();
3445
#else
35-
_data = Wire.read();
46+
_data = Wire.read();
3647
#endif
37-
_error = Wire.endTransmission();
38-
return _data;
48+
return _data;
3949
}
4050

4151
uint8_t PCF8574::value()
4252
{
43-
return _data;
53+
return _data;
4454
}
4555

4656
void PCF8574::write8(uint8_t value)
4757
{
48-
Wire.beginTransmission(_address);
49-
_data = value;
50-
Wire.write(_data);
51-
_error = Wire.endTransmission();
58+
_data = value;
59+
Wire.beginTransmission(_address);
60+
Wire.write(_data);
61+
_error = Wire.endTransmission();
5262
}
5363

64+
// pin should be 0..7
5465
uint8_t PCF8574::read(uint8_t pin)
5566
{
56-
PCF8574::read8();
57-
return (_data & (1<<pin)) > 0;
67+
PCF8574::read8();
68+
return (_data & (1<<pin)) > 0;
5869
}
5970

71+
// pin should be 0..7
6072
void PCF8574::write(uint8_t pin, uint8_t value)
6173
{
62-
PCF8574::read8();
63-
if (value == LOW)
64-
{
65-
_data &= ~(1<<pin);
66-
}
67-
else
68-
{
69-
_data |= (1<<pin);
70-
}
71-
PCF8574::write8(_data);
74+
PCF8574::read8();
75+
if (value == LOW)
76+
{
77+
_data &= ~(1<<pin);
78+
}
79+
else
80+
{
81+
_data |= (1<<pin);
82+
}
83+
PCF8574::write8(_data);
7284
}
7385

86+
// pin should be 0..7
7487
void PCF8574::toggle(uint8_t pin)
7588
{
76-
PCF8574::read8();
77-
_data ^= (1 << pin);
78-
PCF8574::write8(_data);
89+
PCF8574::read8();
90+
_data ^= (1 << pin);
91+
PCF8574::write8(_data);
7992
}
8093

94+
// n should be 0..7
8195
void PCF8574::shiftRight(uint8_t n)
8296
{
83-
if (n == 0 || n > 7 ) return;
84-
PCF8574::read8();
85-
_data >>= n;
86-
PCF8574::write8(_data);
97+
if (n == 0 || n > 7 ) return;
98+
PCF8574::read8();
99+
_data >>= n;
100+
PCF8574::write8(_data);
87101
}
88102

103+
// n should be 0..7
89104
void PCF8574::shiftLeft(uint8_t n)
90105
{
91-
if (n == 0 || n > 7) return;
92-
PCF8574::read8();
93-
_data <<= n;
94-
PCF8574::write8(_data);
106+
if (n == 0 || n > 7) return;
107+
PCF8574::read8();
108+
_data <<= n;
109+
PCF8574::write8(_data);
95110
}
96111

97112
int PCF8574::lastError()
98113
{
99-
int e = _error;
100-
_error = 0;
101-
return e;
114+
int e = _error;
115+
_error = 0;
116+
return e;
102117
}
103118
//
104119
// END OF FILE

libraries/PCF8574/PCF8574.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// FILE: PCF8574.H
33
// AUTHOR: Rob Tillaart
44
// DATE: 02-febr-2013
5-
// VERSION: 0.1.02
5+
// VERSION: 0.1.04
66
// PURPOSE: I2C PCF8574 library for Arduino
7-
// URL:
7+
// URL:
88
//
99
// HISTORY:
1010
// see PCF8574.cpp file
11-
//
11+
//
1212

1313
#ifndef _PCF8574_H
1414
#define _PCF8574_H
@@ -19,30 +19,30 @@
1919
#include "WProgram.h"
2020
#endif
2121

22-
#define PCF8574_LIB_VERSION "0.1.02"
22+
#define PCF8574_LIB_VERSION "0.1.04"
2323

2424
class PCF8574
2525
{
26-
public:
27-
PCF8574(int address);
28-
29-
uint8_t read8();
30-
uint8_t read(uint8_t pin);
31-
uint8_t value();
32-
33-
void write8(uint8_t value);
34-
void write(uint8_t pin, uint8_t value);
35-
36-
void toggle(uint8_t pin);
37-
void shiftRight(uint8_t n=1);
38-
void shiftLeft(uint8_t n=1);
39-
40-
int lastError();
41-
42-
private:
43-
int _address;
44-
uint8_t _data;
45-
int _error;
26+
public:
27+
PCF8574(uint8_t deviceAddress);
28+
29+
uint8_t read8();
30+
uint8_t read(uint8_t pin);
31+
uint8_t value();
32+
33+
void write8(uint8_t value);
34+
void write(uint8_t pin, uint8_t value);
35+
36+
void toggle(uint8_t pin);
37+
void shiftRight(uint8_t n=1);
38+
void shiftLeft(uint8_t n=1);
39+
40+
int lastError();
41+
42+
private:
43+
uint8_t _address;
44+
uint8_t _data;
45+
int _error;
4646
};
4747

4848
#endif

0 commit comments

Comments
 (0)