2013-11-24 15:59:40 -05:00
|
|
|
#ifndef MCP4725_H
|
|
|
|
#define MCP4725_H
|
|
|
|
//
|
|
|
|
// FILE: MCP4725.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2017-04-19 06:31:31 -04:00
|
|
|
// PURPOSE: Simple MCP4725 DAC (12 bit I2C) library for Arduino
|
2019-10-14 07:16:57 -04:00
|
|
|
// VERSION: 0.1.9
|
2013-11-24 15:59:40 -05:00
|
|
|
// HISTORY: See MCP4725.cpp
|
2019-10-14 07:16:57 -04:00
|
|
|
// URL: https://github.com/RobTillaart/Arduino/
|
2013-11-24 15:59:40 -05:00
|
|
|
//
|
|
|
|
// Released to the public domain
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <Wire.h>
|
|
|
|
|
|
|
|
#if defined(ARDUINO) && ARDUINO >= 100
|
|
|
|
#include "Arduino.h"
|
|
|
|
#else
|
|
|
|
#include "WProgram.h"
|
|
|
|
#include "Wstring.h"
|
|
|
|
#include "Wiring.h"
|
|
|
|
#endif
|
|
|
|
|
2019-10-14 07:16:57 -04:00
|
|
|
#define MCP4725_VERSION "0.1.9"
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2013-11-30 17:41:37 -05:00
|
|
|
// regisiterMode
|
|
|
|
#define MCP4725_DAC 0x40
|
2013-12-01 13:33:59 -05:00
|
|
|
#define MCP4725_DACEEPROM 0x60
|
2013-11-30 17:41:37 -05:00
|
|
|
|
|
|
|
// constants
|
2013-11-24 15:59:40 -05:00
|
|
|
#define MCP4725_MAXVALUE 4095
|
2013-11-30 17:41:37 -05:00
|
|
|
|
|
|
|
// errors
|
2013-11-24 15:59:40 -05:00
|
|
|
#define MCP4725_VALUE_ERROR -999
|
2013-11-30 17:41:37 -05:00
|
|
|
#define MCP4725_REG_ERROR -998
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2013-12-01 13:33:59 -05:00
|
|
|
// page 22
|
2013-12-04 13:42:58 -05:00
|
|
|
#define MCP4725_GC_RESET 0x06
|
|
|
|
#define MCP4725_GC_WAKEUP 0x09
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2013-12-01 16:09:02 -05:00
|
|
|
// powerDown Mode - TODO ENUM?
|
2013-12-01 13:33:59 -05:00
|
|
|
#define MCP4725_PDMODE_NORMAL 0x00
|
2013-12-01 16:09:02 -05:00
|
|
|
#define MCP4725_PDMODE_1K 0x01
|
|
|
|
#define MCP4725_PDMODE_100K 0x02
|
|
|
|
#define MCP4725_PDMODE_500K 0x03
|
2013-12-01 13:33:59 -05:00
|
|
|
|
|
|
|
// conditional to minimize footprint.
|
2013-12-04 13:42:58 -05:00
|
|
|
//#define MCP4725_EXTENDED
|
|
|
|
//#define MCP4725_POWERDOWNMODE
|
2013-11-24 15:59:40 -05:00
|
|
|
|
|
|
|
class MCP4725
|
|
|
|
{
|
|
|
|
public:
|
2017-04-19 06:31:31 -04:00
|
|
|
explicit MCP4725(const uint8_t deviceAddress);
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2019-10-14 07:16:57 -04:00
|
|
|
#if defined(ESP8266) || defined(ESP32)
|
|
|
|
void begin(const uint8_t dataPin, const uint8_t clockPin);
|
|
|
|
#endif
|
2013-11-24 15:59:40 -05:00
|
|
|
void begin();
|
2013-11-30 17:41:37 -05:00
|
|
|
// uses writeFastMode
|
2015-03-06 14:19:01 -05:00
|
|
|
int setValue(const uint16_t value);
|
2013-11-30 17:41:37 -05:00
|
|
|
// returns last value set - cached - much faster than readDAC();
|
2013-12-01 16:09:02 -05:00
|
|
|
uint16_t getValue();
|
|
|
|
|
2013-11-30 17:41:37 -05:00
|
|
|
#ifdef MCP4725_EXTENDED
|
2015-03-06 14:19:01 -05:00
|
|
|
int writeDAC(const uint16_t value, const bool EEPROM = false);
|
2013-12-01 13:33:59 -05:00
|
|
|
bool RDY();
|
2013-11-30 17:41:37 -05:00
|
|
|
uint16_t readDAC();
|
2013-12-01 13:33:59 -05:00
|
|
|
uint16_t readEEPROM();
|
|
|
|
#endif
|
2013-12-01 16:09:02 -05:00
|
|
|
|
2013-12-01 13:33:59 -05:00
|
|
|
#ifdef MCP4725_POWERDOWNMODE
|
2013-12-01 16:09:02 -05:00
|
|
|
// experimental
|
2015-03-06 14:19:01 -05:00
|
|
|
int writePowerDownMode(const uint8_t PDM, const bool EEPROM = false);
|
2013-12-01 16:09:02 -05:00
|
|
|
uint8_t readPowerDownModeEEPROM();
|
|
|
|
uint8_t readPowerDownModeDAC();
|
2013-12-01 13:33:59 -05:00
|
|
|
int powerOnReset();
|
|
|
|
int powerOnWakeUp();
|
2013-11-30 17:41:37 -05:00
|
|
|
#endif
|
2013-11-24 15:59:40 -05:00
|
|
|
|
|
|
|
private:
|
2013-11-30 17:41:37 -05:00
|
|
|
uint8_t _deviceAddress;
|
2013-12-01 13:33:59 -05:00
|
|
|
uint16_t _lastValue;
|
2013-12-01 16:09:02 -05:00
|
|
|
uint8_t _powerDownMode; // DATASHEET P15?
|
2015-03-06 14:19:01 -05:00
|
|
|
int writeFastMode(const uint16_t value);
|
2013-12-01 13:33:59 -05:00
|
|
|
|
2013-12-01 16:09:02 -05:00
|
|
|
#ifdef MCP4725_EXTENDED
|
2018-10-18 02:48:42 -04:00
|
|
|
int writeRegisterMode(const uint16_t value, uint8_t reg);
|
2015-03-06 14:19:01 -05:00
|
|
|
uint8_t readRegister(uint8_t* buffer, const uint8_t length);
|
2013-12-01 13:33:59 -05:00
|
|
|
#endif
|
2013-12-01 16:09:02 -05:00
|
|
|
|
2013-12-01 13:33:59 -05:00
|
|
|
#ifdef MCP4725_POWERDOWNMODE
|
2015-03-06 14:19:01 -05:00
|
|
|
int generalCall(const uint8_t gc);
|
2013-11-30 17:41:37 -05:00
|
|
|
#endif
|
2013-12-01 16:09:02 -05:00
|
|
|
|
2013-11-24 15:59:40 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
// END OF FILE
|