#131 replaced TWBR register code with wire.setClock()

+ added begin(dataPin, clockPin) for ESP platforms too
This commit is contained in:
RobTillaart 2019-10-14 13:16:57 +02:00
parent cd673f01c9
commit feeaab9d20
4 changed files with 124 additions and 103 deletions

View File

@ -2,9 +2,9 @@
// FILE: MCP4725.cpp
// AUTHOR: Rob Tillaart
// PURPOSE: Simple MCP4725 DAC (12 bit I2C) library for Arduino
// VERSION: 0.1.8
// VERSION: 0.1.9
// HISTORY: See MCP4725.cpp
// URL:
// URL: https://github.com/RobTillaart/Arduino/
//
// HISTORY:
// 0.1.00 - 2013-11-24 initial version
@ -16,6 +16,7 @@
// 0.1.6 - 2017-04-19 refactor + remove timeout - https://github.com/RobTillaart/Arduino/issues/63
// 0.1.7 - 2017-04-20 refactor the removed timeout (Thanks to Koepel)
// 0.1.8 - 2018-10-24 fix read only var #115 (kudos to perl1234)
// 0.1.9 - 2019-10-14 replace AVR specific TWBR with Wire.setClock() #131
//
// Released to the public domain
//
@ -23,53 +24,70 @@
#include "MCP4725.h"
#if defined(ARDUINO) && ARDUINO >= 100
#define WIRE_WRITE Wire.write
#define WIRE_READ Wire.read
#define WIRE_WRITE Wire.write
#define WIRE_READ Wire.read
#else
#define WIRE_WRITE Wire.send
#define WIRE_READ Wire.receive
#define WIRE_WRITE Wire.send
#define WIRE_READ Wire.receive
#endif
MCP4725::MCP4725(const uint8_t deviceAddress)
{
_deviceAddress = deviceAddress;
_lastValue = 0;
_powerDownMode = 0;
_deviceAddress = deviceAddress;
_lastValue = 0;
_powerDownMode = 0;
}
#if defined(ESP8266) || defined(ESP32)
void MCP4725::begin(const uint8_t dataPin, const uint8_t clockPin)
{
Wire.begin(dataPin, clockPin);
Wire.setClock(100000UL);
#ifdef MCP4725_EXTENDED
_lastValue = readDAC();
#else
_lastValue = 0;
#endif
#ifdef MCP4725_POWERDOWNMODE
_powerDownMode = readPowerDownModeDAC();
#else
_powerDownMode = 0;
#endif
}
#endif
void MCP4725::begin()
{
Wire.begin();
TWBR = 72;
// 0=1000 1=888 2=800 8=500
// 12=400KHz 24=250 32=200 72=100 152=50
// F_CPU/16+(2*TWBR) // TWBR is a uint8_t
Wire.begin();
Wire.setClock(100000UL);
#ifdef MCP4725_EXTENDED
_lastValue = readDAC();
#else
_lastValue = 0;
#endif
#ifdef MCP4725_EXTENDED
_lastValue = readDAC();
#else
_lastValue = 0;
#endif
#ifdef MCP4725_POWERDOWNMODE
_powerDownMode = readPowerDownModeDAC();
#else
_powerDownMode = 0;
#endif
#ifdef MCP4725_POWERDOWNMODE
_powerDownMode = readPowerDownModeDAC();
#else
_powerDownMode = 0;
#endif
}
int MCP4725::setValue(const uint16_t value)
{
if (value == _lastValue) return 0;
if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR;
int rv = writeFastMode(value);
if (rv == 0) _lastValue = value;
return rv;
if (value == _lastValue) return 0;
if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR;
int rv = writeFastMode(value);
if (rv == 0) _lastValue = value;
return rv;
}
uint16_t MCP4725::getValue()
{
return _lastValue;
return _lastValue;
}
#ifdef MCP4725_EXTENDED
@ -78,32 +96,32 @@ uint16_t MCP4725::getValue()
// to the DAC and EEPROM simultaneously or write EEPROM only.
int MCP4725::writeDAC(const uint16_t value, const bool EEPROM)
{
if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR;
while(!RDY());
int rv = writeRegisterMode(value, EEPROM ? MCP4725_DACEEPROM : MCP4725_DAC);
if (rv == 0) _lastValue = value;
return rv;
if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR;
while(!RDY());
int rv = writeRegisterMode(value, EEPROM ? MCP4725_DACEEPROM : MCP4725_DAC);
if (rv == 0) _lastValue = value;
return rv;
}
uint16_t MCP4725::readDAC()
{
uint8_t buffer[3];
readRegister(buffer, 3);
uint16_t value = buffer[1];
value = value << 4;
value = value + (buffer[2] >> 4);
return value;
uint8_t buffer[3];
readRegister(buffer, 3);
uint16_t value = buffer[1];
value = value << 4;
value = value + (buffer[2] >> 4);
return value;
}
uint16_t MCP4725::readEEPROM()
{
while(!RDY());
uint8_t buffer[5];
readRegister(buffer, 5);
uint16_t value = buffer[3] & 0x0F;
value = value << 8;
value = value + buffer[4];
return value;
while(!RDY());
uint8_t buffer[5];
readRegister(buffer, 5);
uint16_t value = buffer[3] & 0x0F;
value = value << 8;
value = value + buffer[4];
return value;
}
#endif
@ -115,26 +133,26 @@ uint16_t MCP4725::readEEPROM()
// (true) DAC & EEPROM,
int MCP4725::writePowerDownMode(const uint8_t PDM, const bool EEPROM)
{
_powerDownMode = (PDM & 0x03); // mask pdm bits only
return writeDAC(_lastValue, EEPROM);
_powerDownMode = (PDM & 0x03); // mask pdm bits only
return writeDAC(_lastValue, EEPROM);
}
uint8_t MCP4725::readPowerDownModeEEPROM()
{
while(!RDY());
uint8_t buffer[4];
readRegister(buffer, 4);
uint8_t value = (buffer[3] >> 5) & 0x03;
return value;
while(!RDY());
uint8_t buffer[4];
readRegister(buffer, 4);
uint8_t value = (buffer[3] >> 5) & 0x03;
return value;
}
uint8_t MCP4725::readPowerDownModeDAC()
{
while(!RDY());
uint8_t buffer[1];
readRegister(buffer, 1);
uint8_t value = (buffer[0] >> 1) & 0x03;
return value;
while(!RDY());
uint8_t buffer[1];
readRegister(buffer, 1);
uint8_t value = (buffer[0] >> 1) & 0x03;
return value;
}
// PAGE 22 - experimental
@ -142,9 +160,9 @@ uint8_t MCP4725::readPowerDownModeDAC()
// need to reflect this in cached value
int MCP4725::powerOnReset()
{
int rv = generalCall(MCP4725_GC_RESET);
_lastValue = readDAC(); // update cache to actual value;
return rv;
int rv = generalCall(MCP4725_GC_RESET);
_lastValue = readDAC(); // update cache to actual value;
return rv;
}
// PAGE 22 - experimental
@ -152,9 +170,9 @@ int MCP4725::powerOnReset()
// need to reflect this in cached value
int MCP4725::powerOnWakeUp()
{
int rv = generalCall(MCP4725_GC_WAKEUP);
_powerDownMode = readPowerDownModeDAC(); // update to actual value;
return rv;
int rv = generalCall(MCP4725_GC_WAKEUP);
_powerDownMode = readPowerDownModeDAC(); // update to actual value;
return rv;
}
#endif
@ -166,13 +184,13 @@ int MCP4725::powerOnWakeUp()
// PAGE 18 DATASHEET
int MCP4725::writeFastMode(const uint16_t value)
{
Wire.beginTransmission(_deviceAddress);
uint8_t h = ((value / 256) & 0x0F); // set C0 = C1 = 0, no PDmode
h = h | (_powerDownMode << 4);
uint8_t l = value & 0xFF;
WIRE_WRITE(h);
WIRE_WRITE(l);
return Wire.endTransmission();
Wire.beginTransmission(_deviceAddress);
uint8_t h = ((value / 256) & 0x0F); // set C0 = C1 = 0, no PDmode
h = h | (_powerDownMode << 4);
uint8_t l = value & 0xFF;
WIRE_WRITE(h);
WIRE_WRITE(l);
return Wire.endTransmission();
}
#ifdef MCP4725_EXTENDED
@ -181,50 +199,50 @@ int MCP4725::writeFastMode(const uint16_t value)
// until RDY all writes to the MCP4725 are ignored!
bool MCP4725::RDY()
{
uint8_t buffer[1];
readRegister(buffer, 1);
return ((buffer[0] & 0x80) > 0);
uint8_t buffer[1];
readRegister(buffer, 1);
return ((buffer[0] & 0x80) > 0);
}
// PAGE 19 DATASHEET
// reg = MCP4725_DAC | MCP4725_EEPROM
int MCP4725::writeRegisterMode(const uint16_t value, uint8_t reg)
{
uint8_t h = (value / 16);
uint8_t l = (value & 0x0F) << 4;
Wire.beginTransmission(_deviceAddress);
reg = reg | (_powerDownMode << 1);
WIRE_WRITE(reg);
WIRE_WRITE(h);
WIRE_WRITE(l);
return Wire.endTransmission();
uint8_t h = (value / 16);
uint8_t l = (value & 0x0F) << 4;
Wire.beginTransmission(_deviceAddress);
reg = reg | (_powerDownMode << 1);
WIRE_WRITE(reg);
WIRE_WRITE(h);
WIRE_WRITE(l);
return Wire.endTransmission();
}
// PAGE 20 DATASHEET
// typical 3 or 5 bytes
uint8_t MCP4725::readRegister(uint8_t* buffer, const uint8_t length)
{
Wire.beginTransmission(_deviceAddress);
int rv = Wire.endTransmission();
if (rv != 0) return 0; // error
Wire.beginTransmission(_deviceAddress);
int rv = Wire.endTransmission();
if (rv != 0) return 0; // error
// readbytes will always be equal or smaller to length
uint8_t readBytes = Wire.requestFrom(_deviceAddress, length);
uint8_t cnt = 0;
while (cnt < readBytes)
{
buffer[cnt++] = WIRE_READ();
}
return readBytes;
// readbytes will always be equal or smaller to length
uint8_t readBytes = Wire.requestFrom(_deviceAddress, length);
uint8_t cnt = 0;
while (cnt < readBytes)
{
buffer[cnt++] = WIRE_READ();
}
return readBytes;
}
#endif
#ifdef MCP4725_POWERDOWNMODE
int MCP4725::generalCall(const uint8_t gc)
{
Wire.beginTransmission(0);
WIRE_WRITE(gc);
return Wire.endTransmission();
Wire.beginTransmission(0);
WIRE_WRITE(gc);
return Wire.endTransmission();
}
#endif

View File

@ -4,9 +4,9 @@
// FILE: MCP4725.h
// AUTHOR: Rob Tillaart
// PURPOSE: Simple MCP4725 DAC (12 bit I2C) library for Arduino
// VERSION: 0.1.8
// VERSION: 0.1.9
// HISTORY: See MCP4725.cpp
// URL:
// URL: https://github.com/RobTillaart/Arduino/
//
// Released to the public domain
//
@ -21,7 +21,7 @@
#include "Wiring.h"
#endif
#define MCP4725_VERSION "0.1.8"
#define MCP4725_VERSION "0.1.9"
// regisiterMode
#define MCP4725_DAC 0x40
@ -53,6 +53,9 @@ class MCP4725
public:
explicit MCP4725(const uint8_t deviceAddress);
#if defined(ESP8266) || defined(ESP32)
void begin(const uint8_t dataPin, const uint8_t clockPin);
#endif
void begin();
// uses writeFastMode
int setValue(const uint16_t value);

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/Arduino.git"
},
"version":"0.1.8",
"version":"0.1.9",
"frameworks": "arduino",
"platforms": "*",
"export": {

View File

@ -1,5 +1,5 @@
name=MCP4725
version=0.1.8
version=0.1.9
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for the MCP4725 DAC.