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

275 lines
6.0 KiB
C++
Raw Normal View History

2017-12-19 05:44:03 -05:00
//
// FILE: DAC8554.cpp
// AUTHOR: Rob Tillaart
2020-11-27 05:10:47 -05:00
// PURPOSE: Arduino library for DAC8554 SPI Digital Analog Convertor
2021-12-15 08:57:49 -05:00
// VERSION: 0.2.4
2020-11-27 05:10:47 -05:00
// URL: https://github.com/RobTillaart/DAC8554
//
2022-10-31 11:49:34 -04:00
// HISTORY: see changelog.md
2021-08-30 08:09:24 -04:00
2021-01-29 06:31:58 -05:00
#include "DAC8554.h"
2017-12-19 05:44:03 -05:00
2021-01-29 06:31:58 -05:00
#define DAC8554_BUFFER_WRITE 0x00
#define DAC8554_SINGLE_WRITE 0x10
#define DAC8554_ALL_WRITE 0x20
#define DAC8554_BROADCAST 0x30
2017-12-19 05:44:03 -05:00
2021-01-29 06:31:58 -05:00
DAC8554::DAC8554(uint8_t slaveSelect, uint8_t address)
2017-12-19 05:44:03 -05:00
{
_hwSPI = true;
2021-08-30 08:09:24 -04:00
_select = slaveSelect;
2017-12-19 05:44:03 -05:00
_address = (address & 0x03) << 6;
}
2021-01-29 06:31:58 -05:00
2017-12-19 05:44:03 -05:00
DAC8554::DAC8554(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect, uint8_t address)
{
2021-08-30 08:09:24 -04:00
_hwSPI = false;
_dataOut = spiData;
_clock = spiClock;
_select = slaveSelect;
2017-12-19 05:44:03 -05:00
_address = (address & 0x03) << 6;
}
2021-01-29 06:31:58 -05:00
2022-10-31 11:49:34 -04:00
// initializes the SPI
// and sets internal state
2017-12-19 05:44:03 -05:00
void DAC8554::begin()
{
2021-08-30 08:09:24 -04:00
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE1);
2021-01-29 06:31:58 -05:00
2017-12-19 05:44:03 -05:00
if(_hwSPI)
{
2021-08-30 08:09:24 -04:00
#if defined(ESP32)
2022-10-31 11:49:34 -04:00
if (_useHSPI) // HSPI
2021-08-30 08:09:24 -04:00
{
mySPI = new SPIClass(HSPI);
mySPI->end();
2022-10-31 11:49:34 -04:00
mySPI->begin(14, 12, 13, _select); // CLK=14 MISO=12 MOSI=13
2021-08-30 08:09:24 -04:00
}
2022-10-31 11:49:34 -04:00
else // VSPI
2021-08-30 08:09:24 -04:00
{
mySPI = new SPIClass(VSPI);
mySPI->end();
2022-10-31 11:49:34 -04:00
mySPI->begin(18, 19, 23, _select); // CLK=18 MISO=19 MOSI=23
2021-08-30 08:09:24 -04:00
}
2022-10-31 11:49:34 -04:00
#else // generic hardware SPI
2021-08-30 08:09:24 -04:00
mySPI = &SPI;
mySPI->end();
mySPI->begin();
#endif
2017-12-19 05:44:03 -05:00
delay(1);
}
2022-10-31 11:49:34 -04:00
else // software SPI
2017-12-19 05:44:03 -05:00
{
2021-08-30 08:09:24 -04:00
pinMode(_dataOut, OUTPUT);
pinMode(_clock, OUTPUT);
digitalWrite(_dataOut, LOW);
digitalWrite(_clock, LOW);
2017-12-19 05:44:03 -05:00
}
2021-01-29 06:31:58 -05:00
for (uint8_t i = 0; i < 4; i++)
{
_register[i] = 0;
_value[i] = 0;
}
2017-12-19 05:44:03 -05:00
}
2021-01-29 06:31:58 -05:00
2021-08-30 08:09:24 -04:00
#if defined(ESP32)
void DAC8554::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);
2022-10-31 11:49:34 -04:00
mySPI->end(); // disable SPI
2021-08-30 08:09:24 -04:00
mySPI->begin(clk, miso, mosi, select);
}
#endif
2017-12-19 05:44:03 -05:00
//////////////////////////////////////////////////////////////////////
//
2022-10-31 11:49:34 -04:00
// SETVALUE
2017-12-19 05:44:03 -05:00
//
2022-10-31 11:49:34 -04:00
// channel = 0, 1, 2, 3
// value = 0..65535
2021-01-29 06:31:58 -05:00
void DAC8554::bufferValue(uint8_t channel, uint16_t value)
2017-12-19 05:44:03 -05:00
{
uint8_t configRegister = _address;
configRegister |= DAC8554_BUFFER_WRITE;
2021-01-29 06:31:58 -05:00
configRegister |= (channel << 1);
2017-12-19 05:44:03 -05:00
writeDevice(configRegister, value);
}
2021-01-29 06:31:58 -05:00
void DAC8554::setValue(uint8_t channel, uint16_t value)
2017-12-19 05:44:03 -05:00
{
2021-01-29 06:31:58 -05:00
_value[channel] = value;
2017-12-19 05:44:03 -05:00
uint8_t configRegister = _address;
configRegister |= DAC8554_ALL_WRITE;
2021-01-29 06:31:58 -05:00
configRegister |= (channel << 1);
2017-12-19 05:44:03 -05:00
writeDevice(configRegister, value);
}
2021-01-29 06:31:58 -05:00
2022-10-31 11:49:34 -04:00
// channel = 0, 1, 2, 3 depending on type
// returns 0..65535
2021-01-29 06:31:58 -05:00
uint16_t DAC8554::getValue(uint8_t channel)
2017-12-19 05:44:03 -05:00
{
2021-01-29 06:31:58 -05:00
return _value[channel];
}
void DAC8554::setSingleValue(uint8_t channel, uint16_t value)
{
_value[channel] = value;
2017-12-19 05:44:03 -05:00
uint8_t configRegister = _address;
configRegister |= DAC8554_SINGLE_WRITE;
2021-01-29 06:31:58 -05:00
configRegister |= (channel << 1);
2017-12-19 05:44:03 -05:00
writeDevice(configRegister, value);
}
2021-01-29 06:31:58 -05:00
2017-12-19 05:44:03 -05:00
//////////////////////////////////////////////////////////////////////
//
2022-10-31 11:49:34 -04:00
// POWERDOWN
2017-12-19 05:44:03 -05:00
//
2022-10-31 11:49:34 -04:00
// channel = 0, 1, 2, 3
// powerDownMode =
// DAC8554_POWERDOWN_NORMAL 0x00
// DAC8554_POWERDOWN_1K 0x40
// DAC8554_POWERDOWN_100K 0x80
// DAC8554_POWERDOWN_HIGH_IMP 0xC0
2021-12-15 08:57:49 -05:00
//
2021-01-29 06:31:58 -05:00
void DAC8554::bufferPowerDown(uint8_t channel, uint8_t powerDownMode)
2017-12-19 05:44:03 -05:00
{
2021-01-29 06:31:58 -05:00
_register[channel] = powerDownMode;
2017-12-19 05:44:03 -05:00
uint8_t configRegister = _address;
configRegister |= DAC8554_BUFFER_WRITE;
2021-01-29 06:31:58 -05:00
configRegister |= (channel << 1);
2017-12-19 05:44:03 -05:00
uint16_t value = (powerDownMode & 0xC0) << 8;
writeDevice(configRegister, value);
}
2021-01-29 06:31:58 -05:00
void DAC8554::setPowerDown(uint8_t channel, uint8_t powerDownMode)
2017-12-19 05:44:03 -05:00
{
2021-01-29 06:31:58 -05:00
_register[channel] = powerDownMode;
2017-12-19 05:44:03 -05:00
uint8_t configRegister = _address;
configRegister |= DAC8554_ALL_WRITE;
2021-01-29 06:31:58 -05:00
configRegister |= (channel << 1);
2017-12-19 05:44:03 -05:00
uint16_t value = (powerDownMode & 0xC0) << 8;
writeDevice(configRegister, value);
}
2021-01-29 06:31:58 -05:00
void DAC8554::setSinglePowerDown(uint8_t channel, uint8_t powerDownMode)
2017-12-19 05:44:03 -05:00
{
2021-01-29 06:31:58 -05:00
_register[channel] = powerDownMode;
2017-12-19 05:44:03 -05:00
uint8_t configRegister = _address;
configRegister |= DAC8554_SINGLE_WRITE;
2021-01-29 06:31:58 -05:00
configRegister |= (channel << 1);
2017-12-19 05:44:03 -05:00
uint16_t value = (powerDownMode & 0xC0) << 8;
writeDevice(configRegister, value);
}
2021-01-29 06:31:58 -05:00
uint8_t DAC8554::getPowerDownMode(uint8_t channel)
{
2022-10-31 11:49:34 -04:00
return _register[channel]; // slightly different than DAC8552 rrrrrr
2021-01-29 06:31:58 -05:00
}
2017-12-19 05:44:03 -05:00
//////////////////////////////////////////////////////////////////////
//
2022-10-31 11:49:34 -04:00
// BROADCAST (addresses all 8554 on "SPI-bus")
2017-12-19 05:44:03 -05:00
//
void DAC8554::broadcastBuffer()
{
uint8_t configRegister = DAC8554_BROADCAST;
uint16_t value = 0;
writeDevice(configRegister, value);
}
2021-01-29 06:31:58 -05:00
2017-12-19 05:44:03 -05:00
void DAC8554::broadcastValue(uint16_t value)
{
uint8_t configRegister = DAC8554_BROADCAST;
configRegister |= 0x04; // TODO magic number
writeDevice(configRegister, value);
}
2021-01-29 06:31:58 -05:00
2017-12-19 05:44:03 -05:00
void DAC8554::broadcastPowerDown(uint8_t powerDownMode)
{
uint8_t configRegister = DAC8554_BROADCAST;
configRegister |= 0x05; // TODO magic number
uint16_t value = (powerDownMode & 0xC0) << 8;
writeDevice(configRegister, value);
}
2021-08-30 08:09:24 -04:00
void DAC8554::setSPIspeed(uint32_t speed)
{
_SPIspeed = speed;
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE1);
};
//////////////////////////////////////////////////////////////////
//
2022-10-31 11:49:34 -04:00
// PRIVATE
2021-08-30 08:09:24 -04:00
//
2017-12-19 05:44:03 -05:00
void DAC8554::writeDevice(uint8_t configRegister, uint16_t value)
{
2021-08-30 08:09:24 -04:00
digitalWrite(_select, LOW);
2017-12-19 05:44:03 -05:00
if (_hwSPI)
{
2021-08-30 08:09:24 -04:00
mySPI->beginTransaction(_spi_settings);
mySPI->transfer(configRegister);
mySPI->transfer(value >> 8);
mySPI->transfer(value & 0xFF);
mySPI->endTransaction();;
2017-12-19 05:44:03 -05:00
}
else // Software SPI
{
swSPI_transfer(configRegister);
swSPI_transfer(value >> 8);
swSPI_transfer(value & 0xFF);
}
2021-08-30 08:09:24 -04:00
digitalWrite(_select, HIGH);
2017-12-19 05:44:03 -05:00
}
2021-01-29 06:31:58 -05:00
2022-10-31 11:49:34 -04:00
// simple one mode version
2017-12-19 05:44:03 -05:00
void DAC8554::swSPI_transfer(uint8_t value)
{
2021-08-30 08:09:24 -04:00
uint8_t clk = _clock;
uint8_t dao = _dataOut;
2017-12-19 05:44:03 -05:00
for (uint8_t mask = 0x80; mask; mask >>= 1)
{
2021-08-30 08:09:24 -04:00
digitalWrite(dao,(value & mask));
digitalWrite(clk, HIGH);
digitalWrite(clk, LOW);
2017-12-19 05:44:03 -05:00
}
}
2021-12-15 08:57:49 -05:00
2020-11-27 05:10:47 -05:00
// -- END OF FILE --
2021-12-15 08:57:49 -05:00