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
|
// FILE: PCF8574.cpp
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// DATE: 02-febr-2013
|
// DATE: 02-febr-2013
|
||||||
// VERSION: 0.1.02
|
// VERSION: 0.1.04
|
||||||
// PURPOSE: I2C PCF8574 library for Arduino
|
// PURPOSE: I2C PCF8574 library for Arduino
|
||||||
// URL:
|
// URL:
|
||||||
//
|
//
|
||||||
// HISTORY:
|
// 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;
|
// 0.1.02 replaced ints with uint8_t to reduce footprint;
|
||||||
// added default value for shiftLeft() and shiftRight()
|
// added default value for shiftLeft() and shiftRight()
|
||||||
// renamed status() to lastError();
|
// 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!
|
// value() does not always reflect the latest state of the pins!
|
||||||
// 0.1.00 initial version
|
// 0.1.00 initial version
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "PCF8574.h"
|
#include "PCF8574.h"
|
||||||
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
PCF8574::PCF8574(int address)
|
PCF8574::PCF8574(uint8_t deviceAddress)
|
||||||
{
|
{
|
||||||
_address = address;
|
_address = deviceAddress;
|
||||||
Wire.begin();
|
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()
|
uint8_t PCF8574::read8()
|
||||||
{
|
{
|
||||||
Wire.beginTransmission(_address);
|
if (Wire.requestFrom(_address, (uint8_t)1) != 1)
|
||||||
Wire.requestFrom(_address, 1);
|
{
|
||||||
|
_error = 10;
|
||||||
|
return _data; // last value
|
||||||
|
}
|
||||||
#if (ARDUINO < 100)
|
#if (ARDUINO < 100)
|
||||||
_data = Wire.receive();
|
_data = Wire.receive();
|
||||||
#else
|
#else
|
||||||
_data = Wire.read();
|
_data = Wire.read();
|
||||||
#endif
|
#endif
|
||||||
_error = Wire.endTransmission();
|
return _data;
|
||||||
return _data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t PCF8574::value()
|
uint8_t PCF8574::value()
|
||||||
{
|
{
|
||||||
return _data;
|
return _data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PCF8574::write8(uint8_t value)
|
void PCF8574::write8(uint8_t value)
|
||||||
{
|
{
|
||||||
Wire.beginTransmission(_address);
|
_data = value;
|
||||||
_data = value;
|
Wire.beginTransmission(_address);
|
||||||
Wire.write(_data);
|
Wire.write(_data);
|
||||||
_error = Wire.endTransmission();
|
_error = Wire.endTransmission();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pin should be 0..7
|
||||||
uint8_t PCF8574::read(uint8_t pin)
|
uint8_t PCF8574::read(uint8_t pin)
|
||||||
{
|
{
|
||||||
PCF8574::read8();
|
PCF8574::read8();
|
||||||
return (_data & (1<<pin)) > 0;
|
return (_data & (1<<pin)) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pin should be 0..7
|
||||||
void PCF8574::write(uint8_t pin, uint8_t value)
|
void PCF8574::write(uint8_t pin, uint8_t value)
|
||||||
{
|
{
|
||||||
PCF8574::read8();
|
PCF8574::read8();
|
||||||
if (value == LOW)
|
if (value == LOW)
|
||||||
{
|
{
|
||||||
_data &= ~(1<<pin);
|
_data &= ~(1<<pin);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_data |= (1<<pin);
|
_data |= (1<<pin);
|
||||||
}
|
}
|
||||||
PCF8574::write8(_data);
|
PCF8574::write8(_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pin should be 0..7
|
||||||
void PCF8574::toggle(uint8_t pin)
|
void PCF8574::toggle(uint8_t pin)
|
||||||
{
|
{
|
||||||
PCF8574::read8();
|
PCF8574::read8();
|
||||||
_data ^= (1 << pin);
|
_data ^= (1 << pin);
|
||||||
PCF8574::write8(_data);
|
PCF8574::write8(_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// n should be 0..7
|
||||||
void PCF8574::shiftRight(uint8_t n)
|
void PCF8574::shiftRight(uint8_t n)
|
||||||
{
|
{
|
||||||
if (n == 0 || n > 7 ) return;
|
if (n == 0 || n > 7 ) return;
|
||||||
PCF8574::read8();
|
PCF8574::read8();
|
||||||
_data >>= n;
|
_data >>= n;
|
||||||
PCF8574::write8(_data);
|
PCF8574::write8(_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// n should be 0..7
|
||||||
void PCF8574::shiftLeft(uint8_t n)
|
void PCF8574::shiftLeft(uint8_t n)
|
||||||
{
|
{
|
||||||
if (n == 0 || n > 7) return;
|
if (n == 0 || n > 7) return;
|
||||||
PCF8574::read8();
|
PCF8574::read8();
|
||||||
_data <<= n;
|
_data <<= n;
|
||||||
PCF8574::write8(_data);
|
PCF8574::write8(_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
int PCF8574::lastError()
|
int PCF8574::lastError()
|
||||||
{
|
{
|
||||||
int e = _error;
|
int e = _error;
|
||||||
_error = 0;
|
_error = 0;
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
// END OF FILE
|
// END OF FILE
|
||||||
|
@ -2,13 +2,13 @@
|
|||||||
// FILE: PCF8574.H
|
// FILE: PCF8574.H
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// DATE: 02-febr-2013
|
// DATE: 02-febr-2013
|
||||||
// VERSION: 0.1.02
|
// VERSION: 0.1.04
|
||||||
// PURPOSE: I2C PCF8574 library for Arduino
|
// PURPOSE: I2C PCF8574 library for Arduino
|
||||||
// URL:
|
// URL:
|
||||||
//
|
//
|
||||||
// HISTORY:
|
// HISTORY:
|
||||||
// see PCF8574.cpp file
|
// see PCF8574.cpp file
|
||||||
//
|
//
|
||||||
|
|
||||||
#ifndef _PCF8574_H
|
#ifndef _PCF8574_H
|
||||||
#define _PCF8574_H
|
#define _PCF8574_H
|
||||||
@ -19,30 +19,30 @@
|
|||||||
#include "WProgram.h"
|
#include "WProgram.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define PCF8574_LIB_VERSION "0.1.02"
|
#define PCF8574_LIB_VERSION "0.1.04"
|
||||||
|
|
||||||
class PCF8574
|
class PCF8574
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PCF8574(int address);
|
PCF8574(uint8_t deviceAddress);
|
||||||
|
|
||||||
uint8_t read8();
|
|
||||||
uint8_t read(uint8_t pin);
|
|
||||||
uint8_t value();
|
|
||||||
|
|
||||||
void write8(uint8_t value);
|
uint8_t read8();
|
||||||
void write(uint8_t pin, uint8_t value);
|
uint8_t read(uint8_t pin);
|
||||||
|
uint8_t value();
|
||||||
|
|
||||||
void toggle(uint8_t pin);
|
void write8(uint8_t value);
|
||||||
void shiftRight(uint8_t n=1);
|
void write(uint8_t pin, uint8_t value);
|
||||||
void shiftLeft(uint8_t n=1);
|
|
||||||
|
void toggle(uint8_t pin);
|
||||||
int lastError();
|
void shiftRight(uint8_t n=1);
|
||||||
|
void shiftLeft(uint8_t n=1);
|
||||||
private:
|
|
||||||
int _address;
|
int lastError();
|
||||||
uint8_t _data;
|
|
||||||
int _error;
|
private:
|
||||||
|
uint8_t _address;
|
||||||
|
uint8_t _data;
|
||||||
|
int _error;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user