2020-11-27 11:20:37 +01:00
|
|
|
#pragma once
|
2013-11-24 21:59:40 +01:00
|
|
|
//
|
|
|
|
// FILE: MCP4725.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-01-16 17:33:16 +01:00
|
|
|
// PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725
|
2023-09-19 20:21:06 +02:00
|
|
|
// VERSION: 0.3.8
|
2020-11-27 11:20:37 +01:00
|
|
|
// URL: https://github.com/RobTillaart/MCP4725
|
2013-11-24 21:59:40 +01:00
|
|
|
//
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2020-11-27 11:20:37 +01:00
|
|
|
#include "Wire.h"
|
2013-11-24 21:59:40 +01:00
|
|
|
#include "Arduino.h"
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2023-09-19 20:21:06 +02:00
|
|
|
#define MCP4725_VERSION (F("0.3.8"))
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2013-11-30 23:41:37 +01:00
|
|
|
|
2022-10-17 10:40:05 +02:00
|
|
|
// CONSTANTS
|
2013-11-24 21:59:40 +01:00
|
|
|
#define MCP4725_MAXVALUE 4095
|
2013-11-30 23:41:37 +01:00
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2022-10-17 10:40:05 +02:00
|
|
|
// ERRORS
|
2021-01-29 12:31:58 +01:00
|
|
|
#define MCP4725_OK 0
|
2013-11-24 21:59:40 +01:00
|
|
|
#define MCP4725_VALUE_ERROR -999
|
2013-11-30 23:41:37 +01:00
|
|
|
#define MCP4725_REG_ERROR -998
|
2021-06-07 08:28:17 +02:00
|
|
|
#define MCP4725_NOT_CONNECTED -997
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2013-11-24 21:59:40 +01:00
|
|
|
|
2022-10-17 10:40:05 +02:00
|
|
|
// powerDown Mode - TODO ENUM?
|
2013-12-01 19:33:59 +01:00
|
|
|
#define MCP4725_PDMODE_NORMAL 0x00
|
2013-12-01 22:09:02 +01:00
|
|
|
#define MCP4725_PDMODE_1K 0x01
|
|
|
|
#define MCP4725_PDMODE_100K 0x02
|
|
|
|
#define MCP4725_PDMODE_500K 0x03
|
2013-12-01 19:33:59 +01:00
|
|
|
|
2023-01-16 17:33:16 +01:00
|
|
|
#define MCP4725_MIDPOINT 2048
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2013-11-24 21:59:40 +01:00
|
|
|
class MCP4725
|
|
|
|
{
|
|
|
|
public:
|
2021-01-29 12:31:58 +01:00
|
|
|
explicit MCP4725(const uint8_t deviceAddress, TwoWire *wire = &Wire);
|
2013-11-24 21:59:40 +01:00
|
|
|
|
2019-10-14 13:16:57 +02:00
|
|
|
#if defined(ESP8266) || defined(ESP32)
|
2023-08-26 11:31:04 +02:00
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
bool begin(const uint8_t dataPin, const uint8_t clockPin);
|
2023-08-26 11:31:04 +02:00
|
|
|
|
2019-10-14 13:16:57 +02:00
|
|
|
#endif
|
2022-10-17 10:40:05 +02:00
|
|
|
|
|
|
|
#if defined (ARDUINO_ARCH_RP2040)
|
|
|
|
|
2023-01-16 17:33:16 +01:00
|
|
|
bool begin(int sda, int scl);
|
2022-10-17 10:40:05 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
bool begin();
|
|
|
|
bool isConnected();
|
|
|
|
|
|
|
|
|
2022-10-17 10:40:05 +02:00
|
|
|
// uses writeFastMode
|
2021-01-29 12:31:58 +01:00
|
|
|
int setValue(const uint16_t value = 0);
|
2022-10-17 10:40:05 +02:00
|
|
|
// returns last value set - cached - much faster than readDAC();
|
2021-01-29 12:31:58 +01:00
|
|
|
uint16_t getValue();
|
|
|
|
|
|
|
|
|
2023-08-26 11:31:04 +02:00
|
|
|
// 0..100.0% - input checked.
|
|
|
|
// will set the closest integer value in range 0..4095
|
2021-12-21 16:28:24 +01:00
|
|
|
int setPercentage(float percentage = 0);
|
2023-08-26 11:31:04 +02:00
|
|
|
// due to rounding the returned value can differ slightly.
|
2023-07-19 10:04:16 +02:00
|
|
|
float getPercentage();
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2023-09-19 20:21:06 +02:00
|
|
|
// typical and default value = 5.0 V
|
|
|
|
void setMaxVoltage(float v = 5.0);
|
|
|
|
float getMaxVoltage();
|
|
|
|
// will set 0..5V to the closest integer value in range 0..4095
|
|
|
|
int setVoltage(float v);
|
|
|
|
// returns (approximation of) the set voltage. Assumes linear mapping.
|
|
|
|
float getVoltage();
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2023-08-26 11:31:04 +02:00
|
|
|
// unfortunately it is not possible to write a different value
|
|
|
|
// to the DAC and EEPROM simultaneously or write EEPROM only.
|
2021-01-29 12:31:58 +01:00
|
|
|
int writeDAC(const uint16_t value, const bool EEPROM = false);
|
2023-08-26 11:31:04 +02:00
|
|
|
// ready checks if the last write to EEPROM has been written.
|
|
|
|
// until ready all writes to the MCP4725 are ignored!
|
2021-01-29 12:31:58 +01:00
|
|
|
bool ready();
|
|
|
|
uint16_t readDAC();
|
|
|
|
uint16_t readEEPROM();
|
2023-08-26 11:31:04 +02:00
|
|
|
uint32_t getLastWriteEEPROM(); // returns timestamp
|
2021-01-29 12:31:58 +01:00
|
|
|
|
|
|
|
|
2022-10-17 10:40:05 +02:00
|
|
|
// experimental
|
2021-01-29 12:31:58 +01:00
|
|
|
int writePowerDownMode(const uint8_t PDM, const bool EEPROM = false);
|
|
|
|
uint8_t readPowerDownModeEEPROM();
|
|
|
|
uint8_t readPowerDownModeDAC();
|
|
|
|
int powerOnReset();
|
|
|
|
int powerOnWakeUp();
|
|
|
|
|
2013-11-24 21:59:40 +01:00
|
|
|
|
|
|
|
private:
|
2021-01-29 12:31:58 +01:00
|
|
|
uint8_t _deviceAddress;
|
|
|
|
uint16_t _lastValue;
|
2022-10-17 10:40:05 +02:00
|
|
|
uint8_t _powerDownMode; // DATASHEET P15?
|
2021-01-29 12:31:58 +01:00
|
|
|
int _writeFastMode(const uint16_t value);
|
|
|
|
uint32_t _lastWriteEEPROM;
|
2013-12-01 19:33:59 +01:00
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
int _writeRegisterMode(const uint16_t value, uint8_t reg);
|
|
|
|
uint8_t _readRegister(uint8_t* buffer, const uint8_t length);
|
2013-12-01 22:09:02 +01:00
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
int _generalCall(const uint8_t gc);
|
2013-12-01 22:09:02 +01:00
|
|
|
|
2023-09-19 20:21:06 +02:00
|
|
|
float _maxVoltage;
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
TwoWire* _wire;
|
2013-11-24 21:59:40 +01:00
|
|
|
};
|
|
|
|
|
2021-12-21 16:28:24 +01:00
|
|
|
|
2023-01-16 17:33:16 +01:00
|
|
|
// -- END OF FILE --
|
2021-12-21 16:28:24 +01:00
|
|
|
|