203 lines
3.7 KiB
C++
Raw Normal View History

2017-12-19 07:47:37 +01:00
//
// FILE: DAC8551.cpp
// AUTHOR: Rob Tillaart
2020-11-27 11:10:47 +01:00
// PURPOSE: Arduino library for DAC8551 SPI Digital Analog Convertor
2023-09-23 17:02:25 +02:00
// VERSION: 0.2.7
2020-11-27 11:10:47 +01:00
// URL: https://github.com/RobTillaart/DAC8551
2021-01-29 12:31:58 +01:00
#include "DAC8551.h"
2017-12-19 07:47:37 +01:00
2021-01-29 12:31:58 +01:00
DAC8551::DAC8551(uint8_t slaveSelect)
2017-12-19 07:47:37 +01:00
{
2021-08-29 20:58:12 +02:00
_hwSPI = true;
_select = slaveSelect;
2017-12-19 07:47:37 +01:00
}
2021-02-06 15:52:51 +01:00
2017-12-19 07:47:37 +01:00
DAC8551::DAC8551(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
{
2021-08-29 20:58:12 +02:00
_hwSPI = false;
_dataOut = spiData;
_clock = spiClock;
_select = slaveSelect;
2017-12-19 07:47:37 +01:00
}
2021-02-06 15:52:51 +01:00
2022-10-31 16:41:19 +01:00
// initializes the SPI
// and sets internal state
2017-12-19 07:47:37 +01:00
void DAC8551::begin()
{
2021-08-29 20:58:12 +02:00
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE1);
2021-01-29 12:31:58 +01:00
2017-12-19 07:47:37 +01:00
if(_hwSPI)
{
2021-08-29 20:58:12 +02:00
#if defined(ESP32)
2022-10-31 16:41:19 +01:00
if (_useHSPI) // HSPI
2021-08-29 20:58:12 +02:00
{
mySPI = new SPIClass(HSPI);
mySPI->end();
2022-10-31 16:41:19 +01:00
mySPI->begin(14, 12, 13, _select); // CLK=14 MISO=12 MOSI=13
2021-08-29 20:58:12 +02:00
}
2022-10-31 16:41:19 +01:00
else // VSPI
2021-08-29 20:58:12 +02:00
{
mySPI = new SPIClass(VSPI);
mySPI->end();
2022-10-31 16:41:19 +01:00
mySPI->begin(18, 19, 23, _select); // CLK=18 MISO=19 MOSI=23
2021-08-29 20:58:12 +02:00
}
2022-10-31 16:41:19 +01:00
#else // generic hardware SPI
2021-08-29 20:58:12 +02:00
mySPI = &SPI;
mySPI->end();
mySPI->begin();
#endif
2017-12-19 07:47:37 +01:00
delay(1);
}
2022-10-31 16:41:19 +01:00
else // software SPI
2017-12-19 11:36:49 +01:00
{
2021-08-29 20:58:12 +02:00
pinMode(_dataOut, OUTPUT);
pinMode(_clock, OUTPUT);
digitalWrite(_dataOut, LOW);
digitalWrite(_clock, LOW);
2017-12-19 11:36:49 +01:00
}
2017-12-19 07:47:37 +01:00
_register = 0;
_value = 0;
}
2021-08-29 20:58:12 +02:00
#if defined(ESP32)
void DAC8551::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);
2023-09-23 17:02:25 +02:00
mySPI->end(); // disable SPI
2021-08-29 20:58:12 +02:00
mySPI->begin(clk, miso, mosi, select);
}
#endif
2022-10-31 16:41:19 +01:00
// value = 0..65535
2017-12-19 07:47:37 +01:00
void DAC8551::setValue(uint16_t value)
{
_value = value;
updateDevice();
}
2021-02-06 15:52:51 +01:00
2022-10-31 16:41:19 +01:00
// returns 0..65535
2017-12-19 07:47:37 +01:00
uint16_t DAC8551::getValue()
{
return _value;
}
2021-02-06 15:52:51 +01:00
2017-12-19 07:47:37 +01:00
void DAC8551::setPowerDown(uint8_t powerDownMode)
{
2021-06-02 08:31:14 +02:00
_register = (powerDownMode & 0x03);
2017-12-19 07:47:37 +01:00
updateDevice();
}
2021-02-06 15:52:51 +01:00
2017-12-19 07:47:37 +01:00
uint8_t DAC8551::getPowerDownMode()
{
return _register & 0x03;
}
2021-02-06 15:52:51 +01:00
2021-08-29 20:58:12 +02:00
void DAC8551::setSPIspeed(uint32_t speed)
{
_SPIspeed = speed;
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE1);
};
//////////////////////////////////////////////////////////////////
//
2022-10-31 16:41:19 +01:00
// PRIVATE
2021-08-29 20:58:12 +02:00
//
2017-12-19 07:47:37 +01:00
void DAC8551::updateDevice()
{
uint8_t configRegister = _register;
2021-08-29 20:58:12 +02:00
digitalWrite(_select, LOW);
2017-12-19 07:47:37 +01:00
if (_hwSPI)
{
2021-08-29 20:58:12 +02:00
mySPI->beginTransaction(_spi_settings);
mySPI->transfer(configRegister);
mySPI->transfer(_value >> 8);
mySPI->transfer(_value & 0xFF);
mySPI->endTransaction();
2017-12-19 07:47:37 +01:00
}
2023-09-23 17:02:25 +02:00
else // Software SPI
2017-12-19 07:47:37 +01:00
{
swSPI_transfer(configRegister);
swSPI_transfer(_value >> 8);
swSPI_transfer(_value & 0xFF);
}
2021-08-29 20:58:12 +02:00
digitalWrite(_select, HIGH);
2017-12-19 07:47:37 +01:00
}
2021-02-06 15:52:51 +01:00
2022-10-31 16:41:19 +01:00
// simple one mode version
2017-12-19 07:47:37 +01:00
void DAC8551::swSPI_transfer(uint8_t value)
{
2021-08-29 20:58:12 +02:00
uint8_t clk = _clock;
uint8_t dao = _dataOut;
2017-12-19 07:47:37 +01:00
for (uint8_t mask = 0x80; mask; mask >>= 1)
{
2021-08-29 20:58:12 +02:00
digitalWrite(dao,(value & mask));
digitalWrite(clk, HIGH);
digitalWrite(clk, LOW);
2017-12-19 07:47:37 +01:00
}
}
2021-02-06 15:52:51 +01:00
/////////////////////////////////////////////////////////
//
2022-10-31 16:41:19 +01:00
// derive 8501, 8531 and 8550 from 8551
2023-09-23 17:02:25 +02:00
//
2021-02-06 15:52:51 +01:00
DAC8501::DAC8501(uint8_t slaveSelect) : DAC8551(slaveSelect)
{
}
2021-12-15 12:34:24 +01:00
2021-02-06 15:52:51 +01:00
DAC8501::DAC8501(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
: DAC8551(spiData, spiClock, slaveSelect)
{
}
DAC8531::DAC8531(uint8_t slaveSelect) : DAC8551(slaveSelect)
{
}
2021-12-15 12:34:24 +01:00
2021-02-06 15:52:51 +01:00
DAC8531::DAC8531(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
: DAC8551(spiData, spiClock, slaveSelect)
{
}
DAC8550::DAC8550(uint8_t slaveSelect) : DAC8551(slaveSelect)
{
}
2021-12-15 12:34:24 +01:00
2021-02-06 15:52:51 +01:00
DAC8550::DAC8550(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
: DAC8551(spiData, spiClock, slaveSelect)
{
}
2021-12-15 12:34:24 +01:00
2023-09-23 17:02:25 +02:00
// -- END OF FILE --
2021-12-15 12:34:24 +01:00