mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
+ version 0.1.04 (0.1.03 = intermediate)
+ removed beginTransmission from read8() -> speed++ + removed endTransmission from read8() -> speed + changed address to uint8_t
This commit is contained in:
parent
b527f06da5
commit
1c1de9bfeb
@ -2,103 +2,118 @@
|
||||
// FILE: PCF8574.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 02-febr-2013
|
||||
// VERSION: 0.1.02
|
||||
// VERSION: 0.1.04
|
||||
// PURPOSE: I2C PCF8574 library for Arduino
|
||||
// URL:
|
||||
// URL:
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.04 2015-05-09 removed ambiguity in read8()
|
||||
// 0.1.03 2015-03-02 address int -> uint8_t
|
||||
// 0.1.02 replaced ints with uint8_t to reduce footprint;
|
||||
// added default value for shiftLeft() and shiftRight()
|
||||
// renamed status() to lastError();
|
||||
// 0.1.01 added value(); returns last read 8 bit value (cached);
|
||||
// 0.1.01 added value(); returns last read 8 bit value (cached);
|
||||
// value() does not always reflect the latest state of the pins!
|
||||
// 0.1.00 initial version
|
||||
//
|
||||
//
|
||||
|
||||
#include "PCF8574.h"
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
PCF8574::PCF8574(int address)
|
||||
PCF8574::PCF8574(uint8_t deviceAddress)
|
||||
{
|
||||
_address = address;
|
||||
Wire.begin();
|
||||
_address = deviceAddress;
|
||||
Wire.begin();
|
||||
// TWBR = 12; // 400KHz
|
||||
}
|
||||
|
||||
// removed Wire.beginTransmission(addr);
|
||||
// with @100KHz -> 265 micros()
|
||||
// without @100KHz -> 132 micros()
|
||||
// without @400KHz -> 52 micros()
|
||||
// TODO @800KHz -> ??
|
||||
uint8_t PCF8574::read8()
|
||||
{
|
||||
Wire.beginTransmission(_address);
|
||||
Wire.requestFrom(_address, 1);
|
||||
if (Wire.requestFrom(_address, (uint8_t)1) != 1)
|
||||
{
|
||||
_error = 10;
|
||||
return _data; // last value
|
||||
}
|
||||
#if (ARDUINO < 100)
|
||||
_data = Wire.receive();
|
||||
_data = Wire.receive();
|
||||
#else
|
||||
_data = Wire.read();
|
||||
_data = Wire.read();
|
||||
#endif
|
||||
_error = Wire.endTransmission();
|
||||
return _data;
|
||||
return _data;
|
||||
}
|
||||
|
||||
uint8_t PCF8574::value()
|
||||
{
|
||||
return _data;
|
||||
return _data;
|
||||
}
|
||||
|
||||
void PCF8574::write8(uint8_t value)
|
||||
{
|
||||
Wire.beginTransmission(_address);
|
||||
_data = value;
|
||||
Wire.write(_data);
|
||||
_error = Wire.endTransmission();
|
||||
_data = value;
|
||||
Wire.beginTransmission(_address);
|
||||
Wire.write(_data);
|
||||
_error = Wire.endTransmission();
|
||||
}
|
||||
|
||||
// pin should be 0..7
|
||||
uint8_t PCF8574::read(uint8_t pin)
|
||||
{
|
||||
PCF8574::read8();
|
||||
return (_data & (1<<pin)) > 0;
|
||||
PCF8574::read8();
|
||||
return (_data & (1<<pin)) > 0;
|
||||
}
|
||||
|
||||
// pin should be 0..7
|
||||
void PCF8574::write(uint8_t pin, uint8_t value)
|
||||
{
|
||||
PCF8574::read8();
|
||||
if (value == LOW)
|
||||
{
|
||||
_data &= ~(1<<pin);
|
||||
}
|
||||
else
|
||||
{
|
||||
_data |= (1<<pin);
|
||||
}
|
||||
PCF8574::write8(_data);
|
||||
PCF8574::read8();
|
||||
if (value == LOW)
|
||||
{
|
||||
_data &= ~(1<<pin);
|
||||
}
|
||||
else
|
||||
{
|
||||
_data |= (1<<pin);
|
||||
}
|
||||
PCF8574::write8(_data);
|
||||
}
|
||||
|
||||
// pin should be 0..7
|
||||
void PCF8574::toggle(uint8_t pin)
|
||||
{
|
||||
PCF8574::read8();
|
||||
_data ^= (1 << pin);
|
||||
PCF8574::write8(_data);
|
||||
PCF8574::read8();
|
||||
_data ^= (1 << pin);
|
||||
PCF8574::write8(_data);
|
||||
}
|
||||
|
||||
// n should be 0..7
|
||||
void PCF8574::shiftRight(uint8_t n)
|
||||
{
|
||||
if (n == 0 || n > 7 ) return;
|
||||
PCF8574::read8();
|
||||
_data >>= n;
|
||||
PCF8574::write8(_data);
|
||||
if (n == 0 || n > 7 ) return;
|
||||
PCF8574::read8();
|
||||
_data >>= n;
|
||||
PCF8574::write8(_data);
|
||||
}
|
||||
|
||||
// n should be 0..7
|
||||
void PCF8574::shiftLeft(uint8_t n)
|
||||
{
|
||||
if (n == 0 || n > 7) return;
|
||||
PCF8574::read8();
|
||||
_data <<= n;
|
||||
PCF8574::write8(_data);
|
||||
if (n == 0 || n > 7) return;
|
||||
PCF8574::read8();
|
||||
_data <<= n;
|
||||
PCF8574::write8(_data);
|
||||
}
|
||||
|
||||
int PCF8574::lastError()
|
||||
{
|
||||
int e = _error;
|
||||
_error = 0;
|
||||
return e;
|
||||
int e = _error;
|
||||
_error = 0;
|
||||
return e;
|
||||
}
|
||||
//
|
||||
// END OF FILE
|
||||
|
@ -2,13 +2,13 @@
|
||||
// FILE: PCF8574.H
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 02-febr-2013
|
||||
// VERSION: 0.1.02
|
||||
// VERSION: 0.1.04
|
||||
// PURPOSE: I2C PCF8574 library for Arduino
|
||||
// URL:
|
||||
// URL:
|
||||
//
|
||||
// HISTORY:
|
||||
// see PCF8574.cpp file
|
||||
//
|
||||
//
|
||||
|
||||
#ifndef _PCF8574_H
|
||||
#define _PCF8574_H
|
||||
@ -19,30 +19,30 @@
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#define PCF8574_LIB_VERSION "0.1.02"
|
||||
#define PCF8574_LIB_VERSION "0.1.04"
|
||||
|
||||
class PCF8574
|
||||
{
|
||||
public:
|
||||
PCF8574(int address);
|
||||
|
||||
uint8_t read8();
|
||||
uint8_t read(uint8_t pin);
|
||||
uint8_t value();
|
||||
public:
|
||||
PCF8574(uint8_t deviceAddress);
|
||||
|
||||
void write8(uint8_t value);
|
||||
void write(uint8_t pin, uint8_t value);
|
||||
uint8_t read8();
|
||||
uint8_t read(uint8_t pin);
|
||||
uint8_t value();
|
||||
|
||||
void toggle(uint8_t pin);
|
||||
void shiftRight(uint8_t n=1);
|
||||
void shiftLeft(uint8_t n=1);
|
||||
|
||||
int lastError();
|
||||
|
||||
private:
|
||||
int _address;
|
||||
uint8_t _data;
|
||||
int _error;
|
||||
void write8(uint8_t value);
|
||||
void write(uint8_t pin, uint8_t value);
|
||||
|
||||
void toggle(uint8_t pin);
|
||||
void shiftRight(uint8_t n=1);
|
||||
void shiftLeft(uint8_t n=1);
|
||||
|
||||
int lastError();
|
||||
|
||||
private:
|
||||
uint8_t _address;
|
||||
uint8_t _data;
|
||||
int _error;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user