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

160 lines
3.0 KiB
C++
Raw Normal View History

2017-12-19 01:47:37 -05:00
//
// FILE: DAC8551.cpp
// AUTHOR: Rob Tillaart
2020-11-27 05:10:47 -05:00
// PURPOSE: Arduino library for DAC8551 SPI Digital Analog Convertor
2021-06-02 02:31:14 -04:00
// VERSION: 0.2.3
2020-11-27 05:10:47 -05:00
// URL: https://github.com/RobTillaart/DAC8551
//
2021-01-29 06:31:58 -05:00
// HISTORY
// 0.1.0: 2017-12-18 initial version
// 0.1.1: 2017-12-19 fix begin() bug
// 0.1.2 2020-04-06 minor refactor, readme.md
// 0.1.3 2020-06-07 fix library.json
// 0.2.0 2020-12-18 add slaveSelect to hardware SPI
// 0.2.1 2020-12-18 add arduino-ci + unit tests
2021-02-06 09:52:51 -05:00
// 0.2.2 2021-02-04 add DAC8550 DAC8501 DAC8501 derived class + minor refactor
2021-06-02 02:31:14 -04:00
// 0.2.3 2021-06-02 compile ESP32
2021-01-29 06:31:58 -05:00
#include "DAC8551.h"
2017-12-19 01:47:37 -05:00
2021-01-29 06:31:58 -05:00
DAC8551::DAC8551(uint8_t slaveSelect)
2017-12-19 01:47:37 -05:00
{
_hwSPI = true;
2021-01-29 06:31:58 -05:00
_slaveSelect = slaveSelect;
2017-12-19 01:47:37 -05:00
}
2021-02-06 09:52:51 -05:00
2017-12-19 01:47:37 -05:00
DAC8551::DAC8551(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
{
2021-02-06 09:52:51 -05:00
_hwSPI = false;
2017-12-19 01:47:37 -05:00
_spiData = spiData;
_spiClock = spiClock;
_slaveSelect = slaveSelect;
}
2021-02-06 09:52:51 -05:00
2017-12-19 01:47:37 -05:00
// initializes the SPI
// and sets internal state
void DAC8551::begin()
{
2021-01-29 06:31:58 -05:00
pinMode(_slaveSelect, OUTPUT);
digitalWrite(_slaveSelect, HIGH);
2017-12-19 01:47:37 -05:00
if(_hwSPI)
{
SPI.begin();
delay(1);
}
2017-12-19 05:36:49 -05:00
else
{
pinMode(_spiData, OUTPUT);
pinMode(_spiClock, OUTPUT);
2020-11-27 05:10:47 -05:00
digitalWrite(_spiData, LOW);
digitalWrite(_spiClock, LOW);
2017-12-19 05:36:49 -05:00
}
2017-12-19 01:47:37 -05:00
_register = 0;
_value = 0;
}
// value = 0..65535
void DAC8551::setValue(uint16_t value)
{
_value = value;
updateDevice();
}
2021-02-06 09:52:51 -05:00
2017-12-19 01:47:37 -05:00
// returns 0..65535
uint16_t DAC8551::getValue()
{
return _value;
}
2021-02-06 09:52:51 -05:00
2017-12-19 01:47:37 -05:00
void DAC8551::setPowerDown(uint8_t powerDownMode)
{
2021-06-02 02:31:14 -04:00
_register = (powerDownMode & 0x03);
2017-12-19 01:47:37 -05:00
updateDevice();
}
2021-02-06 09:52:51 -05:00
2017-12-19 01:47:37 -05:00
uint8_t DAC8551::getPowerDownMode()
{
return _register & 0x03;
}
2021-02-06 09:52:51 -05:00
2017-12-19 01:47:37 -05:00
void DAC8551::updateDevice()
{
uint8_t configRegister = _register;
2021-02-06 09:52:51 -05:00
digitalWrite(_slaveSelect, LOW);
2017-12-19 01:47:37 -05:00
if (_hwSPI)
{
SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE1));
SPI.transfer(configRegister);
SPI.transfer(_value >> 8);
SPI.transfer(_value & 0xFF);
SPI.endTransaction();
}
else // Software SPI
{
swSPI_transfer(configRegister);
swSPI_transfer(_value >> 8);
swSPI_transfer(_value & 0xFF);
}
2021-02-06 09:52:51 -05:00
digitalWrite(_slaveSelect, LOW);
2017-12-19 01:47:37 -05:00
}
2021-02-06 09:52:51 -05:00
2017-12-19 01:47:37 -05:00
// simple one mode version
void DAC8551::swSPI_transfer(uint8_t value)
{
for (uint8_t mask = 0x80; mask; mask >>= 1)
{
digitalWrite(_spiData,(value & mask) != 0);
digitalWrite(_spiClock, HIGH);
digitalWrite(_spiClock, LOW);
}
}
2021-02-06 09:52:51 -05:00
/////////////////////////////////////////////////////////
//
// derive 8501, 8531 and 8550 from 8551
//
DAC8501::DAC8501(uint8_t slaveSelect) : DAC8551(slaveSelect)
{
}
DAC8501::DAC8501(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
: DAC8551(spiData, spiClock, slaveSelect)
{
}
DAC8531::DAC8531(uint8_t slaveSelect) : DAC8551(slaveSelect)
{
}
DAC8531::DAC8531(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
: DAC8551(spiData, spiClock, slaveSelect)
{
}
DAC8550::DAC8550(uint8_t slaveSelect) : DAC8551(slaveSelect)
{
}
DAC8550::DAC8550(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
: DAC8551(spiData, spiClock, slaveSelect)
{
}
2020-11-27 05:10:47 -05:00
// -- END OF FILE --