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

438 lines
7.3 KiB
C++
Raw Normal View History

2021-05-26 05:38:28 -04:00
//
// FILE: MCP_DAC.cpp
// AUTHOR: Rob Tillaart
2024-06-05 13:01:03 -04:00
// VERSION: 0.5.1
2021-05-26 05:38:28 -04:00
// DATE: 2021-02-03
// PURPOSE: Arduino library for MCP_DAC
// URL: https://github.com/RobTillaart/MCP_DAC
2021-07-31 11:44:31 -04:00
2021-05-26 05:38:28 -04:00
#include "MCP_DAC.h"
2023-03-24 06:08:34 -04:00
2023-11-26 10:56:37 -05:00
// HW SPI
MCP_DAC::MCP_DAC(__SPI_CLASS__ *inSPI)
2021-05-26 05:38:28 -04:00
{
2023-11-26 10:56:37 -05:00
mySPI = inSPI;
_dataOut = 255;
_clock = 255;
_select = 0;
_hwSPI = true;
_channels = 1;
_maxValue = 255;
reset();
}
// SW SPI
MCP_DAC::MCP_DAC(uint8_t dataOut, uint8_t clock)
{
mySPI = NULL;
2021-05-26 05:38:28 -04:00
_dataOut = dataOut;
_clock = clock;
2021-07-31 11:44:31 -04:00
_select = 0;
2023-11-26 10:56:37 -05:00
_hwSPI = false;
2021-05-26 05:38:28 -04:00
_channels = 1;
_maxValue = 255;
reset();
}
2023-03-24 06:08:34 -04:00
2021-05-26 05:38:28 -04:00
void MCP_DAC::reset()
{
_gain = 1;
_value[0] = 0;
_value[1] = 0;
_buffered = false;
_active = true;
}
void MCP_DAC::begin(uint8_t select)
{
_select = select;
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);
2021-07-31 11:44:31 -04:00
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
2021-05-26 05:38:28 -04:00
if (_hwSPI)
{
2024-01-20 08:53:57 -05:00
// mySPI->end();
// mySPI->begin();
2021-07-31 11:44:31 -04:00
}
2023-11-26 10:56:37 -05:00
else // software SPI
2021-07-31 11:44:31 -04:00
{
pinMode(_dataOut, OUTPUT);
pinMode(_clock, OUTPUT);
digitalWrite(_dataOut, LOW);
digitalWrite(_clock, LOW);
2021-05-26 05:38:28 -04:00
}
}
2023-03-24 06:08:34 -04:00
uint8_t MCP_DAC::channels()
{
return _channels;
}
uint16_t MCP_DAC::maxValue()
{
return _maxValue;
}
2021-05-26 05:38:28 -04:00
bool MCP_DAC::setGain(uint8_t gain)
{
2021-11-08 10:16:58 -05:00
if ((0 == gain) || (gain > 2)) return false;
2021-05-26 05:38:28 -04:00
_gain = gain;
return true;
}
2023-03-24 06:08:34 -04:00
uint8_t MCP_DAC::getGain()
{
return _gain;
}
2023-12-19 10:59:32 -05:00
bool MCP_DAC::write(uint16_t value, uint8_t channel)
2021-05-26 05:38:28 -04:00
{
if (channel >= _channels) return false;
2022-10-19 05:48:25 -04:00
// CONSTRAIN VALUE
2021-05-26 05:38:28 -04:00
uint16_t _val = value;
if (_val > _maxValue) _val = _maxValue;
_value[channel] = value;
2022-10-19 05:48:25 -04:00
// PREPARING THE DATA TRANSFER
2021-05-26 05:38:28 -04:00
uint16_t data = 0x1000;
if (channel == 1) data |= 0x8000;
if (_buffered) data |= 0x4000;
if (_gain == 1) data |= 0x2000;
if (_maxValue == 4095) data |= _val;
else if (_maxValue == 1023) data |= (_val << 2);
else data |= (_val << 4);
transfer(data);
return true;
}
2023-03-24 06:08:34 -04:00
uint16_t MCP_DAC::lastValue(uint8_t channel)
{
return _value[channel];
}
2021-05-26 05:38:28 -04:00
void MCP_DAC::fastWriteA(uint16_t value)
{
transfer(0x3000 | value);
}
void MCP_DAC::fastWriteB(uint16_t value)
{
transfer(0xB000 | value);
}
2021-08-01 09:52:01 -04:00
bool MCP_DAC::increment(uint8_t channel)
{
if (channel >= _channels) return false;
if (_value[channel] == _maxValue) return false;
2023-12-19 10:59:32 -05:00
return write(_value[channel] + 1, channel);
2021-08-01 09:52:01 -04:00
}
bool MCP_DAC::decrement(uint8_t channel)
{
if (channel >= _channels) return false;
if (_value[channel] == 0) return false;
2023-12-19 10:59:32 -05:00
return write(_value[channel] - 1, channel);
2021-08-01 09:52:01 -04:00
}
2021-05-26 05:38:28 -04:00
void MCP_DAC::setPercentage(float perc, uint8_t channel)
{
if (perc < 0) perc = 0;
if (perc > 100) perc = 100;
2023-12-19 10:59:32 -05:00
write((0.01 * perc * _maxValue), channel);
2021-05-26 05:38:28 -04:00
}
float MCP_DAC::getPercentage(uint8_t channel)
{
return (_value[channel] * 100.0) / _maxValue;
}
void MCP_DAC::setLatchPin(uint8_t latchPin)
{
_latchPin = latchPin;
pinMode(_latchPin, OUTPUT);
2023-03-24 06:08:34 -04:00
digitalWrite(_latchPin, HIGH);
2021-05-26 05:38:28 -04:00
}
void MCP_DAC::triggerLatch()
{
2021-07-31 11:44:31 -04:00
if (_latchPin != 255)
{
digitalWrite(_latchPin, LOW);
2023-03-24 06:08:34 -04:00
// delay needed == 100 ns - Page 7
// on "slow" devices the next delay can be commented
delayMicroseconds(1);
digitalWrite(_latchPin, HIGH);
2021-07-31 11:44:31 -04:00
}
2021-05-26 05:38:28 -04:00
}
void MCP_DAC::shutDown()
{
_active = false;
2023-03-24 06:08:34 -04:00
transfer(0x0000); // a write will reset the values.
}
bool MCP_DAC::isActive()
{
return _active;
2021-05-26 05:38:28 -04:00
}
2021-07-31 11:44:31 -04:00
void MCP_DAC::setSPIspeed(uint32_t speed)
{
_SPIspeed = speed;
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
};
2023-03-24 06:08:34 -04:00
uint32_t MCP_DAC::getSPIspeed()
{
return _SPIspeed;
}
void MCP_DAC::setBufferedMode(bool mode)
{
_buffered = mode;
}
bool MCP_DAC::getBufferedMode()
{
return _buffered;
}
bool MCP_DAC::usesHWSPI()
{
return _hwSPI;
}
2021-07-31 11:44:31 -04:00
//////////////////////////////////////////////////////////////////
2021-11-08 10:16:58 -05:00
//
2022-10-19 05:48:25 -04:00
// PROTECTED
2021-11-08 10:16:58 -05:00
//
2021-05-26 05:38:28 -04:00
void MCP_DAC::transfer(uint16_t data)
{
2022-10-19 05:48:25 -04:00
// DATA TRANSFER
2021-05-26 05:38:28 -04:00
digitalWrite(_select, LOW);
if (_hwSPI)
{
2021-07-31 11:44:31 -04:00
mySPI->beginTransaction(_spi_settings);
mySPI->transfer((uint8_t)(data >> 8));
mySPI->transfer((uint8_t)(data & 0xFF));
mySPI->endTransaction();
2021-05-26 05:38:28 -04:00
}
2022-10-19 05:48:25 -04:00
else // Software SPI
2021-05-26 05:38:28 -04:00
{
swSPI_transfer((uint8_t)(data >> 8));
swSPI_transfer((uint8_t)(data & 0xFF));
}
digitalWrite(_select, HIGH);
}
2022-10-19 05:48:25 -04:00
// MSBFIRST
2021-07-31 11:44:31 -04:00
uint8_t MCP_DAC::swSPI_transfer(uint8_t val)
2021-05-26 05:38:28 -04:00
{
2021-07-31 11:44:31 -04:00
uint8_t clk = _clock;
uint8_t dao = _dataOut;
2021-05-26 05:38:28 -04:00
for (uint8_t mask = 0x80; mask; mask >>= 1)
{
2021-07-31 11:44:31 -04:00
digitalWrite(dao, (val & mask));
digitalWrite(clk, HIGH);
digitalWrite(clk, LOW);
2021-05-26 05:38:28 -04:00
}
return 0;
}
2023-03-24 06:08:34 -04:00
2023-11-26 10:56:37 -05:00
/////////////////////////////////////////////////////////////////////////////
//
// DERIVED CLASSES
//
2023-03-24 06:08:34 -04:00
2022-10-19 05:48:25 -04:00
/////////////////////////////////////////////////////////////////////////////
//
2023-03-24 06:08:34 -04:00
// MCP4800 series
2022-10-19 05:48:25 -04:00
//
2023-11-26 10:56:37 -05:00
MCP4801::MCP4801(__SPI_CLASS__ *inSPI) : MCP_DAC(inSPI)
2022-10-19 05:48:25 -04:00
{
_channels = 1;
_maxValue = 255;
};
2023-11-26 10:56:37 -05:00
MCP4801::MCP4801(uint8_t dataOut, uint8_t clock) : MCP_DAC(dataOut, clock)
2022-10-19 05:48:25 -04:00
{
2023-11-26 10:56:37 -05:00
_channels = 1;
2022-10-19 05:48:25 -04:00
_maxValue = 255;
};
2023-11-26 10:56:37 -05:00
MCP4802::MCP4802(__SPI_CLASS__ *inSPI) : MCP_DAC(inSPI)
2022-10-19 05:48:25 -04:00
{
2023-11-26 10:56:37 -05:00
_channels = 2;
_maxValue = 255;
2022-10-19 05:48:25 -04:00
};
2023-11-26 10:56:37 -05:00
MCP4802::MCP4802(uint8_t dataOut, uint8_t clock) : MCP_DAC(dataOut, clock)
2022-10-19 05:48:25 -04:00
{
_channels = 2;
2023-11-26 10:56:37 -05:00
_maxValue = 255;
2022-10-19 05:48:25 -04:00
};
2023-11-26 10:56:37 -05:00
MCP4811::MCP4811(__SPI_CLASS__ *inSPI) : MCP_DAC(inSPI)
2022-10-19 05:48:25 -04:00
{
_channels = 1;
2023-11-26 10:56:37 -05:00
_maxValue = 1023;
2022-10-19 05:48:25 -04:00
};
2023-11-26 10:56:37 -05:00
MCP4811::MCP4811(uint8_t dataOut, uint8_t clock) : MCP_DAC(dataOut, clock)
2022-10-19 05:48:25 -04:00
{
_channels = 1;
2023-11-26 10:56:37 -05:00
_maxValue = 1023;
2022-10-19 05:48:25 -04:00
};
2023-11-26 10:56:37 -05:00
MCP4812::MCP4812(__SPI_CLASS__ *inSPI) : MCP_DAC(inSPI)
2022-10-19 05:48:25 -04:00
{
_channels = 2;
_maxValue = 1023;
};
2023-11-26 10:56:37 -05:00
MCP4812::MCP4812(uint8_t dataOut, uint8_t clock) : MCP_DAC(dataOut, clock)
2022-10-19 05:48:25 -04:00
{
_channels = 2;
_maxValue = 1023;
};
2023-11-26 10:56:37 -05:00
MCP4821::MCP4821(__SPI_CLASS__ *inSPI) : MCP_DAC(inSPI)
2022-10-19 05:48:25 -04:00
{
_channels = 1;
_maxValue = 4095;
};
2023-11-26 10:56:37 -05:00
MCP4821::MCP4821(uint8_t dataOut, uint8_t clock) : MCP_DAC(dataOut, clock)
{
_channels = 1;
_maxValue = 4095;
};
2022-10-19 05:48:25 -04:00
2023-11-26 10:56:37 -05:00
MCP4822::MCP4822(__SPI_CLASS__ *inSPI) : MCP_DAC(inSPI)
2022-10-19 05:48:25 -04:00
{
_channels = 2;
_maxValue = 4095;
};
2023-11-26 10:56:37 -05:00
MCP4822::MCP4822(uint8_t dataOut, uint8_t clock) : MCP_DAC(dataOut, clock)
{
_channels = 2;
_maxValue = 4095;
};
2021-05-26 05:38:28 -04:00
2023-03-24 06:08:34 -04:00
2021-05-26 05:38:28 -04:00
/////////////////////////////////////////////////////////////////////////////
//
2023-11-26 10:56:37 -05:00
// MCP4900 series
2021-12-21 10:10:14 -05:00
//
2023-11-26 10:56:37 -05:00
MCP4901::MCP4901(__SPI_CLASS__ *inSPI) : MCP_DAC(inSPI)
2021-05-26 05:38:28 -04:00
{
_channels = 1;
_maxValue = 255;
};
2023-11-26 10:56:37 -05:00
MCP4901::MCP4901(uint8_t dataOut, uint8_t clock) : MCP_DAC(dataOut, clock)
2021-05-26 05:38:28 -04:00
{
2023-11-26 10:56:37 -05:00
_channels = 1;
2021-05-26 05:38:28 -04:00
_maxValue = 255;
};
2023-11-26 10:56:37 -05:00
MCP4902::MCP4902(__SPI_CLASS__ *inSPI) : MCP_DAC(inSPI)
2021-05-26 05:38:28 -04:00
{
2023-11-26 10:56:37 -05:00
_channels = 2;
_maxValue = 255;
2021-05-26 05:38:28 -04:00
};
2023-11-26 10:56:37 -05:00
MCP4902::MCP4902(uint8_t dataOut, uint8_t clock) : MCP_DAC(dataOut, clock)
2021-05-26 05:38:28 -04:00
{
_channels = 2;
2023-11-26 10:56:37 -05:00
_maxValue = 255;
2021-05-26 05:38:28 -04:00
};
2023-11-26 10:56:37 -05:00
MCP4911::MCP4911(__SPI_CLASS__ *inSPI) : MCP_DAC(inSPI)
2021-05-26 05:38:28 -04:00
{
_channels = 1;
2023-11-26 10:56:37 -05:00
_maxValue = 1023;
2021-05-26 05:38:28 -04:00
};
2023-11-26 10:56:37 -05:00
MCP4911::MCP4911(uint8_t dataOut, uint8_t clock) : MCP_DAC(dataOut, clock)
2021-05-26 05:38:28 -04:00
{
_channels = 1;
2023-11-26 10:56:37 -05:00
_maxValue = 1023;
2021-05-26 05:38:28 -04:00
};
2023-11-26 10:56:37 -05:00
MCP4912::MCP4912(__SPI_CLASS__ *inSPI) : MCP_DAC(inSPI)
2021-05-26 05:38:28 -04:00
{
_channels = 2;
_maxValue = 1023;
};
2023-11-26 10:56:37 -05:00
MCP4912::MCP4912(uint8_t dataOut, uint8_t clock) : MCP_DAC(dataOut, clock)
2021-05-26 05:38:28 -04:00
{
_channels = 2;
_maxValue = 1023;
};
2023-11-26 10:56:37 -05:00
MCP4921::MCP4921(__SPI_CLASS__ *inSPI) : MCP_DAC(inSPI)
{
_channels = 1;
_maxValue = 4095;
};
2021-12-21 10:10:14 -05:00
2023-11-26 10:56:37 -05:00
MCP4921::MCP4921(uint8_t dataOut, uint8_t clock) : MCP_DAC(dataOut, clock)
2021-05-26 05:38:28 -04:00
{
_channels = 1;
_maxValue = 4095;
};
2023-11-26 10:56:37 -05:00
MCP4922::MCP4922(__SPI_CLASS__ *inSPI) : MCP_DAC(inSPI)
{
_channels = 2;
_maxValue = 4095;
};
2021-12-21 10:10:14 -05:00
2023-11-26 10:56:37 -05:00
MCP4922::MCP4922(uint8_t dataOut, uint8_t clock) : MCP_DAC(dataOut, clock)
2021-05-26 05:38:28 -04:00
{
_channels = 2;
_maxValue = 4095;
};
2023-03-24 06:08:34 -04:00
// -- END OF FILE --
2021-05-26 05:38:28 -04:00