2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: AD520X.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// DATE: 2020-07-24
|
2021-12-10 08:12:54 -05:00
|
|
|
// VERSION: 0.2.1
|
2021-01-29 06:31:58 -05:00
|
|
|
// PURPOSE: Arduino library for AD5204 and AD5206 digital potentiometers (+ older AD8400, AD8402, AD8403)
|
|
|
|
// URL: https://github.com/RobTillaart/AD520X
|
|
|
|
//
|
|
|
|
// HISTORY:
|
2021-08-22 04:47:15 -04:00
|
|
|
// 0.0.1 2020-07-24 initial version
|
|
|
|
// 0.0.2 2020-07-25 support for AD8400 series in documentation.
|
|
|
|
// 0.1.0 2020-07-26 refactor, fix #2 select pin for HW SPI; add shutdown.
|
|
|
|
// 0.1.1 2020-12-08 Arduino-CI + unit test + isPowerOn()
|
|
|
|
// 0.1.2 2021-08-19 VSPI / HSPI support for ESP32 only
|
|
|
|
// add setGPIOpins for ESP32 only
|
|
|
|
// add SetSPIspeed (generic)
|
2021-10-16 14:58:54 -04:00
|
|
|
// 0.2.0 2021-10-16 update build-CI
|
|
|
|
// add get- and setPercentage()
|
2021-12-10 08:12:54 -05:00
|
|
|
// 0.2.1 2021-12-10 update library.json, licence
|
|
|
|
// default value for setAll()
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
#include "AD520X.h"
|
|
|
|
|
|
|
|
|
|
|
|
AD520X::AD520X(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
|
|
|
{
|
|
|
|
_pmCount = 6;
|
|
|
|
_select = select;
|
2021-08-22 04:47:15 -04:00
|
|
|
_dataOut = dataOut;
|
2021-01-29 06:31:58 -05:00
|
|
|
_clock = clock;
|
|
|
|
_reset = reset;
|
|
|
|
_shutdown = shutdown;
|
|
|
|
_hwSPI = (dataOut == 255) && (clock == 255);
|
|
|
|
}
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-10-16 14:58:54 -04:00
|
|
|
// initializes the pins and starts SPI in case of hardware SPI
|
2021-01-29 06:31:58 -05:00
|
|
|
void AD520X::begin(uint8_t value)
|
|
|
|
{
|
|
|
|
pinMode(_select, OUTPUT);
|
|
|
|
digitalWrite(_select, HIGH);
|
|
|
|
pinMode(_reset, OUTPUT);
|
|
|
|
digitalWrite(_reset, LOW);
|
|
|
|
pinMode(_shutdown, OUTPUT);
|
|
|
|
digitalWrite(_shutdown, LOW);
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE1);
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
if(_hwSPI)
|
|
|
|
{
|
2021-08-22 04:47:15 -04:00
|
|
|
#if defined(ESP32)
|
2021-10-16 14:58:54 -04:00
|
|
|
if (_useHSPI) // HSPI
|
2021-08-22 04:47:15 -04:00
|
|
|
{
|
|
|
|
mySPI = new SPIClass(HSPI);
|
|
|
|
mySPI->end();
|
2021-10-16 14:58:54 -04:00
|
|
|
mySPI->begin(14, 12, 13, _select); // CLK=14 MISO=12 MOSI=13
|
2021-08-22 04:47:15 -04:00
|
|
|
}
|
2021-10-16 14:58:54 -04:00
|
|
|
else // VSPI
|
2021-08-22 04:47:15 -04:00
|
|
|
{
|
|
|
|
mySPI = new SPIClass(VSPI);
|
|
|
|
mySPI->end();
|
2021-10-16 14:58:54 -04:00
|
|
|
mySPI->begin(18, 19, 23, _select); // CLK=18 MISO=19 MOSI=23
|
2021-08-22 04:47:15 -04:00
|
|
|
}
|
2021-10-16 14:58:54 -04:00
|
|
|
#else // generic hardware SPI
|
2021-08-22 04:47:15 -04:00
|
|
|
mySPI = &SPI;
|
|
|
|
mySPI->end();
|
|
|
|
mySPI->begin();
|
|
|
|
#endif
|
2021-01-29 06:31:58 -05:00
|
|
|
delay(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-22 04:47:15 -04:00
|
|
|
pinMode(_dataOut, OUTPUT);
|
|
|
|
pinMode(_clock, OUTPUT);
|
|
|
|
digitalWrite(_dataOut, LOW);
|
|
|
|
digitalWrite(_clock, LOW);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setAll(value);
|
|
|
|
}
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
|
|
|
#if defined(ESP32)
|
|
|
|
void AD520X::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)
|
|
|
|
{
|
|
|
|
_clock = clk;
|
|
|
|
_dataOut = mosi;
|
|
|
|
_select = select;
|
|
|
|
pinMode(_select, OUTPUT);
|
|
|
|
digitalWrite(_select, HIGH);
|
|
|
|
|
2021-10-16 14:58:54 -04:00
|
|
|
mySPI->end(); // disable SPI and restart
|
2021-08-22 04:47:15 -04:00
|
|
|
mySPI->begin(clk, miso, mosi, select);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2021-10-16 14:58:54 -04:00
|
|
|
bool AD520X::setValue(uint8_t pm, uint8_t value)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-10-16 14:58:54 -04:00
|
|
|
if (pm >= _pmCount) return false;
|
2021-01-29 06:31:58 -05:00
|
|
|
_value[pm] = value;
|
|
|
|
updateDevice(pm);
|
2021-10-16 14:58:54 -04:00
|
|
|
return true;
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void AD520X::setAll(uint8_t value)
|
|
|
|
{
|
|
|
|
for (uint8_t pm = 0; pm < _pmCount; pm++)
|
|
|
|
{
|
|
|
|
setValue(pm, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t AD520X::getValue(uint8_t pm)
|
|
|
|
{
|
|
|
|
if (pm >= _pmCount) return 0;
|
|
|
|
return _value[pm];
|
|
|
|
}
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-10-16 14:58:54 -04:00
|
|
|
bool AD520X::setPercentage(uint8_t pm, float percentage)
|
|
|
|
{
|
|
|
|
if ((percentage < 0) || (percentage > 100.0)) return false;
|
|
|
|
return setValue(pm, round(percentage * (255.0 / 100.0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float AD520X::getPercentage(uint8_t pm)
|
|
|
|
{
|
|
|
|
if (pm >= _pmCount) return 0;
|
|
|
|
uint8_t v = _value[pm];
|
|
|
|
if (v == 0) return 0.0;
|
|
|
|
return (100.0 / 255.0) * v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void AD520X::reset(uint8_t value)
|
|
|
|
{
|
|
|
|
digitalWrite(_reset, HIGH);
|
|
|
|
digitalWrite(_reset, LOW);
|
|
|
|
setAll(value);
|
|
|
|
}
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
|
|
|
void AD520X::setSPIspeed(uint32_t speed)
|
|
|
|
{
|
|
|
|
_SPIspeed = speed;
|
|
|
|
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE1);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// PROTECTED
|
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
void AD520X::updateDevice(uint8_t pm)
|
|
|
|
{
|
2021-08-22 04:47:15 -04:00
|
|
|
digitalWrite(_select, LOW);
|
2021-01-29 06:31:58 -05:00
|
|
|
if (_hwSPI)
|
|
|
|
{
|
2021-08-22 04:47:15 -04:00
|
|
|
mySPI->beginTransaction(_spi_settings);
|
|
|
|
mySPI->transfer(pm);
|
|
|
|
mySPI->transfer(_value[pm]);
|
|
|
|
mySPI->endTransaction();
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
2021-10-16 14:58:54 -04:00
|
|
|
else // Software SPI
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
swSPI_transfer(pm);
|
|
|
|
swSPI_transfer(_value[pm]);
|
|
|
|
}
|
2021-08-22 04:47:15 -04:00
|
|
|
digitalWrite(_select, HIGH);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-10-16 14:58:54 -04:00
|
|
|
// simple one mode version
|
2021-08-22 04:47:15 -04:00
|
|
|
void AD520X::swSPI_transfer(uint8_t val)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-08-22 04:47:15 -04:00
|
|
|
uint8_t clk = _clock;
|
|
|
|
uint8_t dao = _dataOut;
|
2021-01-29 06:31:58 -05:00
|
|
|
for (uint8_t mask = 0x80; mask; mask >>= 1)
|
|
|
|
{
|
2021-08-22 04:47:15 -04:00
|
|
|
digitalWrite(dao,(val & mask));
|
|
|
|
digitalWrite(clk, HIGH);
|
|
|
|
digitalWrite(clk, LOW);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2021-08-22 04:47:15 -04:00
|
|
|
//
|
2021-10-16 14:58:54 -04:00
|
|
|
// DERIVED CLASSES
|
2021-08-22 04:47:15 -04:00
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
AD5206::AD5206(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
|
|
|
: AD520X(select, reset, shutdown, dataOut, clock)
|
|
|
|
{
|
|
|
|
_pmCount = 6;
|
|
|
|
}
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
AD5204::AD5204(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
|
|
|
: AD520X(select, reset, shutdown, dataOut, clock)
|
|
|
|
{
|
|
|
|
_pmCount = 4;
|
|
|
|
}
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
AD8403::AD8403(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
|
|
|
: AD520X(select, reset, shutdown, dataOut, clock)
|
|
|
|
{
|
|
|
|
_pmCount = 4;
|
|
|
|
}
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
AD8402::AD8402(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
|
|
|
: AD520X(select, reset, shutdown, dataOut, clock)
|
|
|
|
{
|
|
|
|
_pmCount = 2;
|
|
|
|
}
|
|
|
|
|
2021-08-22 04:47:15 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
AD8400::AD8400(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
|
|
|
: AD520X(select, reset, shutdown, dataOut, clock)
|
|
|
|
{
|
|
|
|
_pmCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- END OF FILE --
|