2013-10-15 14:18:00 -04:00
|
|
|
//
|
|
|
|
// FILE: AD524X.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-10-25 11:57:23 -04:00
|
|
|
// VERSION: 0.3.5
|
2013-10-15 15:42:24 -04:00
|
|
|
// PURPOSE: I2C digital potentiometer AD5241 AD5242
|
2013-10-15 14:18:00 -04:00
|
|
|
// DATE: 2013-10-12
|
2020-11-27 05:10:47 -05:00
|
|
|
// URL: https://github.com/RobTillaart/AD524X
|
2013-10-15 14:18:00 -04:00
|
|
|
//
|
2022-10-25 11:57:23 -04:00
|
|
|
// HISTORY see changelog.md
|
2021-10-16 15:39:45 -04:00
|
|
|
|
2013-10-15 14:18:00 -04:00
|
|
|
|
|
|
|
#include "AD524X.h"
|
|
|
|
|
2021-12-10 08:52:27 -05:00
|
|
|
#define AD524X_RDAC0 0x00
|
|
|
|
#define AD524X_RDAC1 0x80
|
|
|
|
#define AD524X_RESET 0x40
|
|
|
|
#define AD524X_SHUTDOWN 0x20
|
|
|
|
#define AD524X_O1_HIGH 0x10
|
|
|
|
#define AD524X_O2_HIGH 0x08
|
2013-10-15 14:18:00 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
AD524X::AD524X(const uint8_t address, TwoWire *wire)
|
|
|
|
{
|
2022-10-25 11:57:23 -04:00
|
|
|
// address: 0x01011xx = 0x2C - 0x2F
|
2021-01-29 06:31:58 -05:00
|
|
|
_address = address;
|
|
|
|
_wire = wire;
|
2022-10-25 11:57:23 -04:00
|
|
|
_lastValue[0] = _lastValue[1] = 127; // power on reset => mid position
|
2021-01-29 06:31:58 -05:00
|
|
|
_O1 = _O2 = 0;
|
|
|
|
_pmCount = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if defined (ESP8266) || defined(ESP32)
|
|
|
|
bool AD524X::begin(uint8_t dataPin, uint8_t clockPin)
|
|
|
|
{
|
|
|
|
_wire = &Wire;
|
|
|
|
if ((dataPin < 255) && (clockPin < 255))
|
|
|
|
{
|
|
|
|
_wire->begin(dataPin, clockPin);
|
|
|
|
} else {
|
|
|
|
_wire->begin();
|
|
|
|
}
|
|
|
|
if (! isConnected()) return false;
|
|
|
|
reset();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
bool AD524X::begin()
|
|
|
|
{
|
|
|
|
_wire->begin();
|
|
|
|
if (! isConnected()) return false;
|
|
|
|
reset();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool AD524X::isConnected()
|
|
|
|
{
|
|
|
|
_wire->beginTransmission(_address);
|
|
|
|
return ( _wire->endTransmission() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t AD524X::reset()
|
2013-10-15 14:18:00 -04:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
write(0, 127, LOW, LOW);
|
|
|
|
return write(1, 127);
|
2013-10-15 14:18:00 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2013-10-15 15:42:24 -04:00
|
|
|
uint8_t AD524X::zeroAll()
|
2013-10-15 14:18:00 -04:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
write(0, 0, LOW, LOW);
|
|
|
|
return write(1, 0);
|
2013-10-15 15:42:24 -04:00
|
|
|
}
|
|
|
|
|
2021-10-16 15:39:45 -04:00
|
|
|
|
2015-03-06 08:40:36 -05:00
|
|
|
uint8_t AD524X::write(const uint8_t rdac, const uint8_t value)
|
2013-10-15 15:42:24 -04:00
|
|
|
{
|
2021-12-10 08:52:27 -05:00
|
|
|
if (rdac >= _pmCount) return AD524X_ERROR;
|
2013-10-15 15:42:24 -04:00
|
|
|
|
2021-12-10 08:52:27 -05:00
|
|
|
uint8_t cmd = (rdac == 0) ? AD524X_RDAC0 : AD524X_RDAC1;
|
2021-01-29 06:31:58 -05:00
|
|
|
// apply the output lines
|
|
|
|
cmd = cmd | _O1 | _O2;
|
|
|
|
_lastValue[rdac] = value;
|
|
|
|
return send(cmd, value);
|
2013-10-15 14:18:00 -04:00
|
|
|
}
|
|
|
|
|
2021-10-16 15:39:45 -04:00
|
|
|
|
2015-03-06 08:40:36 -05:00
|
|
|
uint8_t AD524X::write(const uint8_t rdac, const uint8_t value, const uint8_t O1, const uint8_t O2)
|
2013-10-15 14:18:00 -04:00
|
|
|
{
|
2021-12-10 08:52:27 -05:00
|
|
|
if (rdac >= _pmCount) return AD524X_ERROR;
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-10 08:52:27 -05:00
|
|
|
uint8_t cmd = (rdac == 0) ? AD524X_RDAC0 : AD524X_RDAC1;
|
|
|
|
_O1 = (O1 == LOW) ? 0 : AD524X_O1_HIGH;
|
|
|
|
_O2 = (O2 == LOW) ? 0 : AD524X_O2_HIGH;
|
2021-01-29 06:31:58 -05:00
|
|
|
// apply the output lines
|
|
|
|
cmd = cmd | _O1 | _O2;
|
|
|
|
_lastValue[rdac] = value;
|
|
|
|
return send(cmd, value);
|
2013-10-15 14:18:00 -04:00
|
|
|
}
|
|
|
|
|
2021-10-16 15:39:45 -04:00
|
|
|
|
2015-03-06 08:40:36 -05:00
|
|
|
uint8_t AD524X::setO1(const uint8_t value)
|
2013-10-15 14:18:00 -04:00
|
|
|
{
|
2021-12-10 08:52:27 -05:00
|
|
|
_O1 = (value == LOW) ? 0 : AD524X_O1_HIGH;
|
|
|
|
uint8_t cmd = AD524X_RDAC0 | _O1 | _O2;
|
2021-01-29 06:31:58 -05:00
|
|
|
return send(cmd, _lastValue[0]);
|
2013-10-15 14:18:00 -04:00
|
|
|
}
|
|
|
|
|
2021-10-16 15:39:45 -04:00
|
|
|
|
2015-03-06 08:40:36 -05:00
|
|
|
uint8_t AD524X::setO2(const uint8_t value)
|
2013-10-15 14:18:00 -04:00
|
|
|
{
|
2021-12-10 08:52:27 -05:00
|
|
|
_O2 = (value == LOW) ? 0: AD524X_O2_HIGH;
|
|
|
|
uint8_t cmd = AD524X_RDAC0 | _O1 | _O2;
|
2021-01-29 06:31:58 -05:00
|
|
|
return send(cmd, _lastValue[0]);
|
2013-10-15 14:18:00 -04:00
|
|
|
}
|
|
|
|
|
2021-10-16 15:39:45 -04:00
|
|
|
|
2013-10-15 15:42:24 -04:00
|
|
|
uint8_t AD524X::getO1()
|
2013-10-15 14:18:00 -04:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
return (_O1 > 0);
|
2013-10-15 14:18:00 -04:00
|
|
|
}
|
|
|
|
|
2021-10-16 15:39:45 -04:00
|
|
|
|
2013-10-15 15:42:24 -04:00
|
|
|
uint8_t AD524X::getO2()
|
2013-10-15 14:18:00 -04:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
return (_O2 > 0);
|
2013-10-15 15:42:24 -04:00
|
|
|
}
|
|
|
|
|
2021-10-16 15:39:45 -04:00
|
|
|
|
2015-03-06 08:40:36 -05:00
|
|
|
uint8_t AD524X::read(const uint8_t rdac)
|
2013-10-15 15:42:24 -04:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
return _lastValue[rdac];
|
2013-10-15 15:42:24 -04:00
|
|
|
}
|
|
|
|
|
2021-10-16 15:39:45 -04:00
|
|
|
|
2016-04-10 14:23:28 -04:00
|
|
|
uint8_t AD524X::readBackRegister()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
Wire.beginTransmission(_address);
|
|
|
|
Wire.endTransmission();
|
|
|
|
Wire.requestFrom(_address, (uint8_t)1);
|
|
|
|
return Wire.read();
|
2016-04-10 14:23:28 -04:00
|
|
|
}
|
|
|
|
|
2021-10-16 15:39:45 -04:00
|
|
|
|
2015-03-06 08:40:36 -05:00
|
|
|
uint8_t AD524X::midScaleReset(const uint8_t rdac)
|
2013-10-15 15:42:24 -04:00
|
|
|
{
|
2021-12-10 08:52:27 -05:00
|
|
|
if (rdac >= _pmCount) return AD524X_ERROR;
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-10 08:52:27 -05:00
|
|
|
uint8_t cmd = AD524X_RESET;
|
|
|
|
if (rdac == 1) cmd |= AD524X_RDAC1;
|
2021-01-29 06:31:58 -05:00
|
|
|
cmd = cmd | _O1 | _O2;
|
|
|
|
_lastValue[rdac] = 127;
|
|
|
|
return send(cmd, _lastValue[rdac]);
|
|
|
|
}
|
2013-10-15 15:42:24 -04:00
|
|
|
|
2021-10-16 15:39:45 -04:00
|
|
|
|
2022-10-25 11:57:23 -04:00
|
|
|
// read datasheet P.15
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t AD524X::shutDown()
|
|
|
|
{
|
2022-10-25 11:57:23 -04:00
|
|
|
uint8_t cmd = AD524X_SHUTDOWN; // TODO TEST & VERIFY
|
2021-01-29 06:31:58 -05:00
|
|
|
return send(cmd, 0);
|
2013-10-15 14:18:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////
|
|
|
|
//
|
2022-10-25 11:57:23 -04:00
|
|
|
// PRIVATE
|
2013-10-15 14:18:00 -04:00
|
|
|
//
|
2015-03-06 08:40:36 -05:00
|
|
|
uint8_t AD524X::send(const uint8_t cmd, const uint8_t value)
|
2013-10-15 14:18:00 -04:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
Wire.beginTransmission(_address);
|
|
|
|
Wire.write(cmd);
|
|
|
|
Wire.write(value);
|
|
|
|
return Wire.endTransmission();
|
2013-10-15 14:18:00 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-10-16 15:39:45 -04:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// DERIVED CLASSES
|
2022-08-13 13:31:52 -04:00
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
AD5241::AD5241(const uint8_t address, TwoWire *wire) : AD524X(address, wire)
|
|
|
|
{
|
|
|
|
_pmCount = 1;
|
|
|
|
};
|
|
|
|
|
2021-10-16 15:39:45 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
AD5242::AD5242(const uint8_t address, TwoWire *wire) : AD524X(address, wire)
|
|
|
|
{
|
|
|
|
_pmCount = 2;
|
|
|
|
};
|
|
|
|
|
2022-08-13 13:31:52 -04:00
|
|
|
|
2022-10-25 11:57:23 -04:00
|
|
|
// -- END OF FILE --
|
2022-08-13 13:31:52 -04:00
|
|
|
|