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

313 lines
7.1 KiB
C
Raw Normal View History

2021-05-26 05:38:28 -04:00
#pragma once
//
// FILE: MCP_DAC.h
// AUTHOR: Rob Tillaart
2022-10-19 05:48:25 -04:00
// VERSION: 0.1.8
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
//
#include "Arduino.h"
#include "SPI.h"
2022-10-19 05:48:25 -04:00
#define MCP_DAC_LIB_VERSION (F("0.1.8"))
2021-07-31 11:44:31 -04:00
2021-05-26 05:38:28 -04:00
///////////////////////////////////////////////////////////////
//
// BASE CLASS
//
class MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
#if defined(ARDUINO_ARCH_RP2040)
MCP_DAC(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *mySPI = &SPI);
#else
MCP_DAC(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *mySPI = &SPI);
#endif
2021-08-01 09:52:01 -04:00
2021-05-26 05:38:28 -04:00
// if only select is given ==> HW SPI
void begin(uint8_t select);
// 0 or 1
uint8_t channels() { return _channels; };
// 255 (8 bit) or 1023 (10 bit) or 4095 (12 bit)
uint16_t maxValue() { return _maxValue; };
// gain = 1 or 2
2021-11-08 10:16:58 -05:00
bool setGain(uint8_t gain = 1);
2021-05-26 05:38:28 -04:00
uint8_t getGain() { return _gain; };
bool analogWrite(uint16_t value, uint8_t channel = 0);
uint16_t lastValue(uint8_t channel = 0) { return _value[channel]; };
void fastWriteA(uint16_t value);
void fastWriteB(uint16_t value);
2021-08-01 09:52:01 -04:00
bool increment(uint8_t channel = 0);
bool decrement(uint8_t channel = 0);
2021-05-26 05:38:28 -04:00
// convenience wrappers
2021-11-08 10:16:58 -05:00
// percentage = 0..100.0%
void setPercentage(float percentage, uint8_t channel = 0);
float getPercentage(uint8_t channel = 0);
2021-05-26 05:38:28 -04:00
// trigger LDAC = LatchDAC pin - if not set it does nothing
void setLatchPin( uint8_t latchPin);
void triggerLatch();
// shutDown - Page 21 ==> write will wake up.
void shutDown();
bool isActive() { return _active; };
// speed in Hz
2021-07-31 11:44:31 -04:00
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed() { return _SPIspeed; };
2021-05-26 05:38:28 -04:00
//
// MCP49xxx series only
//
// see page 20 ==> not functional for MCP48xx series.
2021-11-08 10:16:58 -05:00
void setBufferedMode(bool mode = false) { _buffered = mode; };
bool getBufferedMode() { return _buffered; };
2021-05-26 05:38:28 -04:00
// debugging
void reset();
bool usesHWSPI() { return _hwSPI; };
2022-10-02 11:19:23 -04:00
2022-10-19 05:48:25 -04:00
#if defined(ESP32) // ESP32 specific
2022-10-02 11:19:23 -04:00
2021-07-31 11:44:31 -04:00
void selectHSPI() { _useHSPI = true; };
void selectVSPI() { _useHSPI = false; };
bool usesHSPI() { return _useHSPI; };
bool usesVSPI() { return !_useHSPI; };
2021-08-01 09:52:01 -04:00
2022-10-02 11:19:23 -04:00
// to overrule the ESP32s default hardware pins
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
2022-10-19 05:48:25 -04:00
#elif defined(ARDUINO_ARCH_RP2040) // RP2040 specific
2022-10-02 11:19:23 -04:00
// to overrule the RP2040s default hardware pins
2021-07-31 11:44:31 -04:00
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
2022-10-02 11:19:23 -04:00
2022-10-19 05:48:25 -04:00
#endif
2021-05-26 05:38:28 -04:00
2022-10-02 11:19:23 -04:00
2021-05-26 05:38:28 -04:00
protected:
2022-10-02 11:19:23 -04:00
uint8_t _dataOut; // Data out Pin (MOSI)
uint8_t _clock; // Clock Pin (SCK)
uint8_t _select; // Chip Select Pin (CS)
uint8_t _latchPin = 255; // Latch-DAC Pin (LDAC)
bool _hwSPI; // Hardware SPI (true) or Software SPI (false)
uint32_t _SPIspeed = 16000000; // SPI-Bus Frequency
uint8_t _channels; // Number of DAC-Channels of a given Chip
uint16_t _maxValue; // Maximum value of a given Chip
uint16_t _value[2]; // Current value (cache for performance)
uint8_t _gain; // Programmable Gain Amplifier variable
bool _buffered = false; // Buffer for the Reference Voltage of the MCP49XX Series Chips
bool _active = true; // Indicates shutDown mode.
2021-05-26 05:38:28 -04:00
void transfer(uint16_t data);
uint8_t swSPI_transfer(uint8_t d);
2021-07-31 11:44:31 -04:00
2022-10-19 05:48:25 -04:00
#if defined(ARDUINO_ARCH_RP2040)
2022-10-02 11:19:23 -04:00
SPIClassRP2040 * mySPI;
2022-10-19 05:48:25 -04:00
#else
2022-10-02 11:19:23 -04:00
2021-07-31 11:44:31 -04:00
SPIClass * mySPI;
2022-10-02 11:19:23 -04:00
2022-10-19 05:48:25 -04:00
#endif
2022-10-02 11:19:23 -04:00
2021-07-31 11:44:31 -04:00
SPISettings _spi_settings;
2022-10-19 05:48:25 -04:00
#if defined(ESP32)
2022-10-02 11:19:23 -04:00
bool _useHSPI = true;
2022-10-19 05:48:25 -04:00
#endif
};
#if defined(ARDUINO_ARCH_RP2040)
///////////////////////////////////////////////////////////////
//
// MCP4800 Series
//
class MCP4801 : public MCP_DAC
{
public:
MCP4801(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *inSPI = &SPI);
};
class MCP4802 : public MCP_DAC
{
public:
MCP4802(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *inSPI = &SPI);
};
class MCP4811 : public MCP_DAC
{
public:
MCP4811(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *inSPI = &SPI);
};
class MCP4812 : public MCP_DAC
{
public:
MCP4812(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *inSPI = &SPI);
};
class MCP4821 : public MCP_DAC
{
public:
MCP4821(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *inSPI = &SPI);
};
class MCP4822 : public MCP_DAC
{
public:
MCP4822(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *inSPI = &SPI);
};
///////////////////////////////////////////////////////////////
//
// MCP4900 Series
//
class MCP4901 : public MCP_DAC
{
public:
MCP4901(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *inSPI = &SPI);
};
class MCP4902 : public MCP_DAC
{
public:
MCP4902(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *inSPI = &SPI);
};
class MCP4911 : public MCP_DAC
{
public:
MCP4911(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *inSPI = &SPI);
};
class MCP4912 : public MCP_DAC
{
public:
MCP4912(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *inSPI = &SPI);
};
2022-10-02 11:19:23 -04:00
2022-10-19 05:48:25 -04:00
class MCP4921 : public MCP_DAC
{
public:
MCP4921(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *inSPI = &SPI);
};
2022-10-02 11:19:23 -04:00
2022-10-19 05:48:25 -04:00
class MCP4922 : public MCP_DAC
{
public:
MCP4922(uint8_t dataOut = 255, uint8_t clock = 255, SPIClassRP2040 *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
2022-10-19 05:48:25 -04:00
#else
2021-05-26 05:38:28 -04:00
///////////////////////////////////////////////////////////////
//
2022-10-02 11:19:23 -04:00
// MCP4800 Series
2021-05-26 05:38:28 -04:00
//
2022-10-19 05:48:25 -04:00
2021-05-26 05:38:28 -04:00
class MCP4801 : public MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
MCP4801(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
class MCP4802 : public MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
MCP4802(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
class MCP4811 : public MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
MCP4811(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
class MCP4812 : public MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
MCP4812(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
class MCP4821 : public MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
MCP4821(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
class MCP4822 : public MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
MCP4822(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
///////////////////////////////////////////////////////////////
//
2022-10-02 11:19:23 -04:00
// MCP4900 Series
2021-05-26 05:38:28 -04:00
//
2022-10-19 05:48:25 -04:00
2021-05-26 05:38:28 -04:00
class MCP4901 : public MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
MCP4901(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
class MCP4902 : public MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
MCP4902(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
class MCP4911 : public MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
MCP4911(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
class MCP4912 : public MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
MCP4912(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
class MCP4921 : public MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
MCP4921(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
class MCP4922 : public MCP_DAC
{
public:
2022-10-19 05:48:25 -04:00
MCP4922(uint8_t dataOut = 255, uint8_t clock = 255, SPIClass *inSPI = &SPI);
2021-05-26 05:38:28 -04:00
};
2022-10-19 05:48:25 -04:00
#endif
2021-05-26 05:38:28 -04:00
2022-10-19 05:48:25 -04:00
// -- END OF FILE --
2021-05-26 05:38:28 -04:00