+ added readDAC(), writeDAC(val), registerWriteMode()
+ made part conditional - MCP4725_EXTENDED - to minimize footprint
+ upgraded test sketch
+ some layout
+ prep EEPROM & PowerDownMode
This commit is contained in:
Rob Tillaart 2013-11-30 23:41:37 +01:00
parent 144e1d7e02
commit f252aa514e
3 changed files with 167 additions and 20 deletions

View File

@ -2,12 +2,13 @@
// FILE: MCP4725.cpp
// AUTHOR: Rob Tillaart
// PURPOSE: Simple MCP4725 DAC library for Arduino
// VERSION: 1.0.00
// VERSION: 1.0.01
// HISTORY: See MCP4725.cpp
// URL:
//
// HISTORY:
// 0.1.00 - 2013-11-24 initial version
// 0.1.01 - 2013-11-30 added readDAC() & writeDAC (registerwrite)
//
// Released to the public domain
//
@ -24,7 +25,6 @@ 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
@ -38,13 +38,15 @@ int MCP4725::setValue(uint16_t value)
if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR;
int rv = writeFastMode(value);
if (rv == 0)
{
_lastValue = value;
}
if (rv == 0) _lastValue = value;
return rv;
}
uint16_t MCP4725::getValue()
{
return _lastValue;
}
int MCP4725::smooth2Value(uint16_t value, uint8_t steps)
{
// speed optimization
@ -69,11 +71,23 @@ int MCP4725::smooth2Value(uint16_t value, uint8_t steps)
return rv;
}
uint16_t MCP4725::getValue()
int MCP4725::writeDAC(uint16_t value)
{
return _lastValue;
if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR;
int rv = writeRegisterMode(value, MCP4725_DAC);
if (rv == 0) _lastValue = value;
return rv;
}
uint16_t MCP4725::readDAC()
{
uint8_t buffer[3];
// TODO set error value?
readRegister(buffer, 3);
uint16_t value = (((uint16_t)buffer[1]) << 4) + (buffer[2] >> 4);
return value;
}
////////////////////////////////////////////////////////////////////
@ -97,4 +111,58 @@ int MCP4725::writeFastMode(uint16_t value)
return Wire.endTransmission();
}
//PAGE 19 DATASHEET
// reg = MCP4725_DAC | MCP4725_EEPROM
int MCP4725::writeRegisterMode(uint16_t value, uint8_t reg)
{
uint8_t h;
uint8_t l;
if (reg == MCP4725_DAC)
{
h = (value / 16);
l = (value & 0x0F) << 4;
}
else if (reg == MCP4725_EEPROM)
{
h = (value / 256);
l = (value & 0xFF);
}
else return MCP4725_REG_ERROR;
Wire.beginTransmission(_deviceAddress);
reg = reg | _PowerDownMode;
#if defined(ARDUINO) && ARDUINO >= 100
Wire.write(reg);
Wire.write(h);
Wire.write(l);
#else
Wire.send(reg);
Wire.send(h);
Wire.send(l);
#endif
return Wire.endTransmission();
}
// PAGE 20 DATASHEET
// typical 3 or 5 bytes
uint8_t MCP4725::readRegister(uint8_t* buffer, uint8_t length)
{
Wire.beginTransmission(_deviceAddress);
int rv = Wire.endTransmission();
if (rv != 0) return 0; // error
Wire.requestFrom(_deviceAddress, length);
uint8_t cnt = 0;
uint32_t before = millis();
while ((cnt < length) && ((millis() - before) < MCP4725_TIMEOUT))
{
#if defined(ARDUINO) && ARDUINO >= 100
if (Wire.available()) buffer[cnt++] = Wire.read();
#else
if (Wire.available()) buffer[cnt++] = Wire.receive();
#endif
}
return cnt;
}
// END OF FILE

View File

@ -4,7 +4,7 @@
// FILE: MCP4725.h
// AUTHOR: Rob Tillaart
// PURPOSE: Simple MCP4725 DAC library for Arduino
// VERSION: 1.0.00
// VERSION: 1.0.01
// HISTORY: See MCP4725.cpp
// URL:
//
@ -21,14 +21,22 @@
#include "Wiring.h"
#endif
#define MCP4725_VERSION "1.0.00"
#define MCP4725_MAXVALUE 4095
#define MCP4725_VALUE_ERROR -999
#define MCP4725_VERSION "1.0.01"
// regisiterMode
#define MCP4725_DAC 0x40
#define MCP4725_EEPROM 0x20
// #define MCP4725_EXTENDED
// constants
#define MCP4725_MAXVALUE 4095
#define MCP4725_TIMEOUT 1000
// errors
#define MCP4725_VALUE_ERROR -999
#define MCP4725_REG_ERROR -998
#define MCP4725_EXTENDED
class MCP4725
{
@ -36,14 +44,31 @@ public:
MCP4725(uint8_t deviceAddress);
void begin();
// uses writeFastMode
int setValue(uint16_t value);
// returns last value set - cached - much faster than readDAC();
uint16_t getValue();
#ifdef MCP4725_EXTENDED
int smooth2Value(uint16_t value, uint8_t steps);
uint16_t getValue();
int writeDAC(uint16_t value);
uint16_t readDAC();
#endif
private:
uint8_t _deviceAddress;
uint8_t _deviceAddress;
int writeFastMode(uint16_t value);
uint16_t _lastValue;
#ifdef MCP4725_EXTENDED
int writeRegisterMode(uint16_t value, uint8_t reg);
uint8_t readRegister(uint8_t* buffer, uint8_t length);
uint8_t _PowerDownMode; // DATASHEET P15?
#endif
};
#endif

View File

@ -1,7 +1,7 @@
//
// FILE: mcp4725_test.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// VERSION: 0.1.01
// PURPOSE: test mcp4725 lib
// DATE: 2013-11-24
// URL:
@ -25,26 +25,48 @@ void setup()
Serial.print("Value:\t");
Serial.println(DAC.getValue());
Serial.println();
Serial.println("setValue(100)");
Serial.println(DAC.setValue(100));
Serial.print("Value:\t");
Serial.println(DAC.getValue());
Serial.println();
Serial.println("setValue(200)");
Serial.println(DAC.setValue(200));
Serial.print("Value:\t");
Serial.println(DAC.getValue());
Serial.println();
Serial.println("smooth2Value(100, 10)");
Serial.println(DAC.smooth2Value(100, 10));
Serial.print("Value:\t");
Serial.println(DAC.getValue());
Serial.println();
#ifdef MCP4725_EXTENDED
Serial.println("smooth2Value(200, 10)");
Serial.println(DAC.smooth2Value(200, 10));
Serial.print("Value:\t");
Serial.println(DAC.getValue());
Serial.println();
Serial.println("writeDAC(100)");
Serial.println(DAC.writeDAC(100));
Serial.print("Value:\t");
Serial.println(DAC.readDAC());
Serial.println();
Serial.println("writeDAC(200)");
Serial.println(DAC.writeDAC(200));
Serial.print("Value:\t");
Serial.println(DAC.readDAC());
Serial.println();
#endif
//////////////////////////////////////////////////
@ -56,7 +78,7 @@ void setup()
volatile int x = DAC.getValue();
}
uint32_t end = micros();
Serial.print("1000x DAC.getValue():\t");
Serial.print("1000x DAC.getValue():\t\t");
Serial.println(end - start);
start = micros();
@ -65,18 +87,49 @@ void setup()
DAC.setValue(i);
}
end = micros();
Serial.print("1000x DAC.setValue(i):\t");
Serial.print("1000x DAC.setValue(i):\t\t");
Serial.println(end - start);
start = micros();
for (int i=0; i< 1000; i++)
{
DAC.setValue(1000);
}
end = micros();
Serial.print("1000x DAC.setValue(1000):\t");
Serial.println(end - start);
#ifdef MCP4725_EXTENDED
start = micros();
for (int i=0; i< 100; i++)
{
DAC.smooth2Value(i*10, 10);
}
end = micros();
Serial.print("100x DAC.smooth2Value(i*10, 10):\t");
Serial.print("100x DAC.smooth2Value(i*10, 10):\t");
Serial.println(end - start);
start = micros();
for (int i=0; i< 1000; i++)
{
volatile int x = DAC.readDAC();
}
end = micros();
Serial.print("1000x DAC.readDAC():\t\t");
Serial.println(end - start);
start = micros();
for (int i=0; i< 1000; i++)
{
volatile int x = DAC.writeDAC(i);
}
end = micros();
Serial.print("1000x DAC.writeDAC(i):\t\t");
Serial.println(end - start);
#endif
}
void loop()
@ -95,3 +148,4 @@ void loop()