|
2 | 2 | // FILE: PCF8574.cpp
|
3 | 3 | // AUTHOR: Rob Tillaart
|
4 | 4 | // DATE: 02-febr-2013
|
5 |
| -// VERSION: 0.1.02 |
| 5 | +// VERSION: 0.1.04 |
6 | 6 | // PURPOSE: I2C PCF8574 library for Arduino
|
7 |
| -// URL: |
| 7 | +// URL: |
8 | 8 | //
|
9 | 9 | // HISTORY:
|
| 10 | +// 0.1.04 2015-05-09 removed ambiguity in read8() |
| 11 | +// 0.1.03 2015-03-02 address int -> uint8_t |
10 | 12 | // 0.1.02 replaced ints with uint8_t to reduce footprint;
|
11 | 13 | // added default value for shiftLeft() and shiftRight()
|
12 | 14 | // 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); |
14 | 16 | // value() does not always reflect the latest state of the pins!
|
15 | 17 | // 0.1.00 initial version
|
16 |
| -// |
| 18 | +// |
17 | 19 |
|
18 | 20 | #include "PCF8574.h"
|
19 | 21 |
|
20 | 22 | #include <Wire.h>
|
21 | 23 |
|
22 |
| -PCF8574::PCF8574(int address) |
| 24 | +PCF8574::PCF8574(uint8_t deviceAddress) |
23 | 25 | {
|
24 |
| - _address = address; |
25 |
| - Wire.begin(); |
| 26 | + _address = deviceAddress; |
| 27 | + Wire.begin(); |
| 28 | + // TWBR = 12; // 400KHz |
26 | 29 | }
|
27 | 30 |
|
| 31 | +// removed Wire.beginTransmission(addr); |
| 32 | +// with @100KHz -> 265 micros() |
| 33 | +// without @100KHz -> 132 micros() |
| 34 | +// without @400KHz -> 52 micros() |
| 35 | +// TODO @800KHz -> ?? |
28 | 36 | uint8_t PCF8574::read8()
|
29 | 37 | {
|
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 | + } |
32 | 43 | #if (ARDUINO < 100)
|
33 |
| - _data = Wire.receive(); |
| 44 | + _data = Wire.receive(); |
34 | 45 | #else
|
35 |
| - _data = Wire.read(); |
| 46 | + _data = Wire.read(); |
36 | 47 | #endif
|
37 |
| - _error = Wire.endTransmission(); |
38 |
| - return _data; |
| 48 | + return _data; |
39 | 49 | }
|
40 | 50 |
|
41 | 51 | uint8_t PCF8574::value()
|
42 | 52 | {
|
43 |
| - return _data; |
| 53 | + return _data; |
44 | 54 | }
|
45 | 55 |
|
46 | 56 | void PCF8574::write8(uint8_t value)
|
47 | 57 | {
|
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(); |
52 | 62 | }
|
53 | 63 |
|
| 64 | +// pin should be 0..7 |
54 | 65 | uint8_t PCF8574::read(uint8_t pin)
|
55 | 66 | {
|
56 |
| - PCF8574::read8(); |
57 |
| - return (_data & (1<<pin)) > 0; |
| 67 | + PCF8574::read8(); |
| 68 | + return (_data & (1<<pin)) > 0; |
58 | 69 | }
|
59 | 70 |
|
| 71 | +// pin should be 0..7 |
60 | 72 | void PCF8574::write(uint8_t pin, uint8_t value)
|
61 | 73 | {
|
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); |
72 | 84 | }
|
73 | 85 |
|
| 86 | +// pin should be 0..7 |
74 | 87 | void PCF8574::toggle(uint8_t pin)
|
75 | 88 | {
|
76 |
| - PCF8574::read8(); |
77 |
| - _data ^= (1 << pin); |
78 |
| - PCF8574::write8(_data); |
| 89 | + PCF8574::read8(); |
| 90 | + _data ^= (1 << pin); |
| 91 | + PCF8574::write8(_data); |
79 | 92 | }
|
80 | 93 |
|
| 94 | +// n should be 0..7 |
81 | 95 | void PCF8574::shiftRight(uint8_t n)
|
82 | 96 | {
|
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); |
87 | 101 | }
|
88 | 102 |
|
| 103 | +// n should be 0..7 |
89 | 104 | void PCF8574::shiftLeft(uint8_t n)
|
90 | 105 | {
|
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); |
95 | 110 | }
|
96 | 111 |
|
97 | 112 | int PCF8574::lastError()
|
98 | 113 | {
|
99 |
| - int e = _error; |
100 |
| - _error = 0; |
101 |
| - return e; |
| 114 | + int e = _error; |
| 115 | + _error = 0; |
| 116 | + return e; |
102 | 117 | }
|
103 | 118 | //
|
104 | 119 | // END OF FILE
|
|
0 commit comments