GY-63_MS5611/libraries/AD524X/AD524X.cpp

272 lines
5.1 KiB
C++
Raw Normal View History

//
// FILE: AD524X.cpp
// AUTHOR: Rob Tillaart
2023-12-05 09:50:43 -05:00
// VERSION: 0.5.0
// PURPOSE: I2C digital potentiometer AD5241 AD5242
// DATE: 2013-10-12
2020-11-27 05:10:47 -05:00
// URL: https://github.com/RobTillaart/AD524X
2021-10-16 15:39:45 -04:00
#include "AD524X.h"
2023-01-11 10:35:57 -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
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;
2023-02-26 11:03:39 -05:00
// power on reset => mid position
_lastValue[0] = _lastValue[1] = AD524X_MIDPOINT;
2021-01-29 06:31:58 -05:00
_O1 = _O2 = 0;
_pmCount = 2;
}
bool AD524X::begin()
{
if (! isConnected()) return false;
reset();
return true;
}
bool AD524X::isConnected()
{
_wire->beginTransmission(_address);
return ( _wire->endTransmission() == 0);
}
uint8_t AD524X::reset()
{
2023-01-11 10:35:57 -05:00
write(0, AD524X_MIDPOINT, LOW, LOW);
return write(1, AD524X_MIDPOINT);
}
2021-01-29 06:31:58 -05:00
uint8_t AD524X::zeroAll()
{
2021-01-29 06:31:58 -05:00
write(0, 0, LOW, LOW);
return write(1, 0);
}
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)
{
2021-12-10 08:52:27 -05:00
if (rdac >= _pmCount) return AD524X_ERROR;
2021-12-10 08:52:27 -05:00
uint8_t cmd = (rdac == 0) ? AD524X_RDAC0 : AD524X_RDAC1;
2023-02-26 11:03:39 -05:00
// apply the output lines
2021-01-29 06:31:58 -05:00
cmd = cmd | _O1 | _O2;
_lastValue[rdac] = value;
return send(cmd, value);
}
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)
{
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
_O1 = (O1 == LOW) ? 0 : AD524X_O1_HIGH;
_O2 = (O2 == LOW) ? 0 : AD524X_O2_HIGH;
2023-02-26 11:03:39 -05:00
uint8_t cmd = (rdac == 0) ? AD524X_RDAC0 : AD524X_RDAC1;
// apply the output lines
2021-01-29 06:31:58 -05:00
cmd = cmd | _O1 | _O2;
_lastValue[rdac] = value;
return send(cmd, value);
}
2021-10-16 15:39:45 -04:00
2015-03-06 08:40:36 -05:00
uint8_t AD524X::setO1(const uint8_t value)
{
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]);
}
2021-10-16 15:39:45 -04:00
2015-03-06 08:40:36 -05:00
uint8_t AD524X::setO2(const uint8_t value)
{
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]);
}
2021-10-16 15:39:45 -04:00
uint8_t AD524X::getO1()
{
2021-01-29 06:31:58 -05:00
return (_O1 > 0);
}
2021-10-16 15:39:45 -04:00
uint8_t AD524X::getO2()
{
2021-01-29 06:31:58 -05:00
return (_O2 > 0);
}
2021-10-16 15:39:45 -04:00
2015-03-06 08:40:36 -05:00
uint8_t AD524X::read(const uint8_t rdac)
{
2021-01-29 06:31:58 -05:00
return _lastValue[rdac];
}
2021-10-16 15:39:45 -04:00
uint8_t AD524X::readBackRegister()
{
2023-02-03 02:41:34 -05:00
_wire->beginTransmission(_address);
_wire->endTransmission();
_wire->requestFrom(_address, (uint8_t)1);
return _wire->read();
}
2021-10-16 15:39:45 -04:00
2015-03-06 08:40:36 -05:00
uint8_t AD524X::midScaleReset(const uint8_t rdac)
{
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;
2023-01-11 10:35:57 -05:00
_lastValue[rdac] = AD524X_MIDPOINT;
2021-01-29 06:31:58 -05:00
return send(cmd, _lastValue[rdac]);
}
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);
}
2023-01-11 10:35:57 -05:00
uint8_t AD524X::pmCount()
{
return _pmCount;
2023-02-26 11:03:39 -05:00
}
2023-01-11 10:35:57 -05:00
//////////////////////////////////////////////////////////
//
2023-01-11 10:35:57 -05:00
// PROTECTED
//
2015-03-06 08:40:36 -05:00
uint8_t AD524X::send(const uint8_t cmd, const uint8_t value)
{
2023-02-03 02:41:34 -05:00
_wire->beginTransmission(_address);
_wire->write(cmd);
_wire->write(value);
return _wire->endTransmission();
}
2021-01-29 06:31:58 -05:00
2021-10-16 15:39:45 -04:00
//////////////////////////////////////////////////////////////
//
2023-12-05 09:50:43 -05:00
// DERIVED CLASSES AD5241 AD5242
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;
2023-02-26 11:03:39 -05:00
}
uint8_t AD5241::write(const uint8_t value)
{
// apply the output lines
uint8_t cmd = AD524X_RDAC0 | _O1 | _O2;
_lastValue[0] = value;
return send(cmd, value);
}
uint8_t AD5241::write(const uint8_t value, const uint8_t O1, const uint8_t O2)
{
_O1 = (O1 == LOW) ? 0 : AD524X_O1_HIGH;
_O2 = (O2 == LOW) ? 0 : AD524X_O2_HIGH;
// apply the output lines
uint8_t cmd = AD524X_RDAC0 | _O1 | _O2;
_lastValue[0] = value;
return send(cmd, value);
}
uint8_t AD5241::write(const uint8_t rdac, const uint8_t value)
{
return AD524X::write(rdac, value);
}
uint8_t AD5241::write(const uint8_t rdac, const uint8_t value, const uint8_t O1, const uint8_t O2)
{
return AD524X::write(rdac, value, O1, O2);
}
2021-01-29 06:31:58 -05:00
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;
2023-02-26 11:03:39 -05:00
}
2021-01-29 06:31:58 -05:00
2022-08-13 13:31:52 -04:00
2023-12-05 09:50:43 -05:00
//////////////////////////////////////////////////////////////
//
// DERIVED CLASSES AD5280 AD5282
//
AD5280::AD5280(const uint8_t address, TwoWire *wire) : AD524X(address, wire)
{
_pmCount = 1;
}
uint8_t AD5280::write(const uint8_t value)
{
// apply the output lines
uint8_t cmd = AD524X_RDAC0 | _O1 | _O2;
_lastValue[0] = value;
return send(cmd, value);
}
uint8_t AD5280::write(const uint8_t value, const uint8_t O1, const uint8_t O2)
{
_O1 = (O1 == LOW) ? 0 : AD524X_O1_HIGH;
_O2 = (O2 == LOW) ? 0 : AD524X_O2_HIGH;
// apply the output lines
uint8_t cmd = AD524X_RDAC0 | _O1 | _O2;
_lastValue[0] = value;
return send(cmd, value);
}
uint8_t AD5280::write(const uint8_t rdac, const uint8_t value)
{
return AD524X::write(rdac, value);
}
uint8_t AD5280::write(const uint8_t rdac, const uint8_t value, const uint8_t O1, const uint8_t O2)
{
return AD524X::write(rdac, value, O1, O2);
}
AD5282::AD5282(const uint8_t address, TwoWire *wire) : AD524X(address, wire)
{
_pmCount = 2;
}
2022-10-25 11:57:23 -04:00
// -- END OF FILE --
2022-08-13 13:31:52 -04:00