2013-11-24 15:59:40 -05:00
|
|
|
//
|
|
|
|
// FILE: mcp4725_test.ino
|
|
|
|
// AUTHOR: Rob Tillaart
|
2013-12-04 13:42:58 -05:00
|
|
|
// VERSION: 0.1.04
|
2013-11-24 15:59:40 -05:00
|
|
|
// PURPOSE: test mcp4725 lib
|
|
|
|
// DATE: 2013-11-24
|
|
|
|
// URL:
|
|
|
|
//
|
|
|
|
// Released to the public domain
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <Wire.h>
|
|
|
|
#include "MCP4725.h"
|
|
|
|
|
|
|
|
MCP4725 DAC(0x62); // 0x62 or 0x63
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("MCP4725 test program: ");
|
2013-11-24 15:59:40 -05:00
|
|
|
Serial.println(MCP4725_VERSION);
|
|
|
|
|
|
|
|
DAC.begin();
|
|
|
|
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("\nValue:\t");
|
2013-11-24 15:59:40 -05:00
|
|
|
Serial.println(DAC.getValue());
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println();
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2013-12-01 13:33:59 -05:00
|
|
|
for (int i=100; i<500; i+=100)
|
|
|
|
{
|
|
|
|
Serial.print("setValue(");
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.print(")\n");
|
|
|
|
DAC.setValue(i);
|
|
|
|
Serial.print("Value:\t");
|
|
|
|
Serial.println(DAC.getValue());
|
|
|
|
}
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println();
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2013-12-01 13:33:59 -05:00
|
|
|
#ifdef MCP4725_EXTENDED
|
|
|
|
Serial.println("\n\nMCP4725_EXTENDED\n\n");
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2013-12-01 13:33:59 -05:00
|
|
|
for (int i=100; i<500; i+=100)
|
|
|
|
{
|
|
|
|
Serial.print("writeDAC(");
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.print(")\n");
|
|
|
|
DAC.writeDAC(i);
|
|
|
|
Serial.print("DACValue:\t");
|
|
|
|
Serial.println(DAC.readDAC());
|
|
|
|
Serial.print("EEValue:\t");
|
|
|
|
Serial.println(DAC.readEEPROM());
|
|
|
|
}
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println();
|
|
|
|
|
2013-12-01 13:33:59 -05:00
|
|
|
for (int i=100; i<500; i+=100)
|
|
|
|
{
|
|
|
|
Serial.print("writeDAC(");
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.print(", true)\n");
|
|
|
|
DAC.writeDAC(i, true);
|
|
|
|
Serial.print("DACValue:\t");
|
|
|
|
Serial.println(DAC.readDAC());
|
|
|
|
Serial.print("EEValue:\t");
|
|
|
|
Serial.println(DAC.readEEPROM());
|
|
|
|
}
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
Serial.println("writeDAC(200)");
|
2013-12-01 13:33:59 -05:00
|
|
|
DAC.writeDAC(200);
|
|
|
|
Serial.print("DACValue:\t");
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println(DAC.readDAC());
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("EEValue:\t");
|
|
|
|
Serial.println(DAC.readEEPROM());
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-12-01 13:33:59 -05:00
|
|
|
#ifdef MCP4725_POWERDOWNMODE
|
|
|
|
Serial.println("\n\nMCP4725_POWERDOWNMODE\n\n");
|
|
|
|
|
2013-12-01 16:09:02 -05:00
|
|
|
for (int i=0; i<4; i++)
|
|
|
|
{
|
|
|
|
Serial.print("DAC.writePowerDownMode(");
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.println(")");
|
|
|
|
DAC.writePowerDownMode(i);
|
|
|
|
Serial.print("EPR PDM Value:\t");
|
|
|
|
Serial.println(DAC.readPowerDownModeEEPROM());
|
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println("EXPERIMENTAL");
|
|
|
|
Serial.println("DAC.writePowerDownMode(3)");
|
|
|
|
DAC.writePowerDownMode(3);
|
2013-12-04 13:42:58 -05:00
|
|
|
DAC.writeDAC(305);
|
|
|
|
Serial.print("Value:\t");
|
|
|
|
Serial.println(DAC.getValue());
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println("DAC.powerOnReset()");
|
|
|
|
Serial.println("Before");
|
|
|
|
Serial.print("DAC PDM Value:\t");
|
|
|
|
Serial.println(DAC.readPowerDownModeDAC());
|
|
|
|
Serial.print("EPR PDM Value:\t");
|
|
|
|
Serial.println(DAC.readPowerDownModeEEPROM());
|
2013-12-04 13:42:58 -05:00
|
|
|
Serial.print("DACValue:\t");
|
|
|
|
Serial.println(DAC.readDAC());
|
|
|
|
Serial.print("EEValue:\t");
|
|
|
|
Serial.println(DAC.readEEPROM());
|
2013-12-01 16:09:02 -05:00
|
|
|
DAC.powerOnReset();
|
|
|
|
Serial.println("After");
|
|
|
|
Serial.print("DAC PDM Value:\t");
|
|
|
|
Serial.println(DAC.readPowerDownModeDAC());
|
|
|
|
Serial.print("EPR PDM Value:\t");
|
|
|
|
Serial.println(DAC.readPowerDownModeEEPROM());
|
2013-12-04 13:42:58 -05:00
|
|
|
Serial.print("DACValue:\t");
|
|
|
|
Serial.println(DAC.readDAC());
|
|
|
|
Serial.print("EEValue:\t");
|
|
|
|
Serial.println(DAC.readEEPROM());
|
|
|
|
Serial.print("Value:\t");
|
|
|
|
Serial.println(DAC.getValue());
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
|
|
|
|
Serial.println("EXPERIMENTAL");
|
|
|
|
Serial.println("DAC.writePowerDownMode(2)");
|
|
|
|
DAC.writePowerDownMode(2);
|
2013-12-04 13:42:58 -05:00
|
|
|
DAC.writeDAC(405);
|
|
|
|
Serial.print("Value:\t");
|
|
|
|
Serial.println(DAC.getValue());
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println("DAC.powerOnWakeUp()");
|
|
|
|
Serial.println("Before");
|
|
|
|
Serial.print("DAC PDM Value:\t");
|
|
|
|
Serial.println(DAC.readPowerDownModeDAC());
|
|
|
|
Serial.print("EPR PDM Value:\t");
|
|
|
|
Serial.println(DAC.readPowerDownModeEEPROM());
|
2013-12-04 13:42:58 -05:00
|
|
|
Serial.print("DACValue:\t");
|
|
|
|
Serial.println(DAC.readDAC());
|
|
|
|
Serial.print("EEValue:\t");
|
|
|
|
Serial.println(DAC.readEEPROM());
|
2013-12-01 16:09:02 -05:00
|
|
|
DAC.powerOnWakeUp();
|
|
|
|
Serial.println("after");
|
|
|
|
Serial.print("DAC PDM Value:\t");
|
|
|
|
Serial.println(DAC.readPowerDownModeDAC());
|
|
|
|
Serial.print("EPR PDM Value:\t");
|
|
|
|
Serial.println(DAC.readPowerDownModeEEPROM());
|
2013-12-04 13:42:58 -05:00
|
|
|
Serial.print("DACValue:\t");
|
|
|
|
Serial.println(DAC.readDAC());
|
|
|
|
Serial.print("EEValue:\t");
|
|
|
|
Serial.println(DAC.readEEPROM());
|
|
|
|
Serial.print("Value:\t");
|
|
|
|
Serial.println(DAC.getValue());
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println();
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2013-12-01 13:33:59 -05:00
|
|
|
#endif
|
2013-11-24 15:59:40 -05:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println("\n\nPERFORMANCE");
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("TWBR:\t");
|
|
|
|
Serial.println(TWBR);
|
|
|
|
Serial.println();
|
2013-11-24 15:59:40 -05:00
|
|
|
|
|
|
|
uint32_t start = micros();
|
|
|
|
for (int i=0; i< 1000; i++)
|
|
|
|
{
|
|
|
|
volatile int x = DAC.getValue();
|
|
|
|
}
|
|
|
|
uint32_t end = micros();
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.print("1000x DAC.getValue():\t\t");
|
2013-11-24 15:59:40 -05:00
|
|
|
Serial.println(end - start);
|
|
|
|
|
|
|
|
start = micros();
|
|
|
|
for (int i=0; i< 1000; i++)
|
|
|
|
{
|
|
|
|
DAC.setValue(i);
|
|
|
|
}
|
|
|
|
end = micros();
|
2013-11-30 17:41:37 -05:00
|
|
|
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");
|
2013-11-24 15:59:40 -05:00
|
|
|
Serial.println(end - start);
|
|
|
|
|
2013-11-30 17:41:37 -05:00
|
|
|
|
|
|
|
#ifdef MCP4725_EXTENDED
|
|
|
|
|
2013-11-24 15:59:40 -05:00
|
|
|
start = micros();
|
2013-12-01 13:33:59 -05:00
|
|
|
for (int i=0; i< 1000; i++)
|
2013-11-24 15:59:40 -05:00
|
|
|
{
|
2013-12-01 13:33:59 -05:00
|
|
|
volatile int x = DAC.readDAC();
|
2013-11-24 15:59:40 -05:00
|
|
|
}
|
|
|
|
end = micros();
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("1000x DAC.readDAC():\t\t");
|
2013-11-24 15:59:40 -05:00
|
|
|
Serial.println(end - start);
|
|
|
|
|
2013-11-30 17:41:37 -05:00
|
|
|
start = micros();
|
|
|
|
for (int i=0; i< 1000; i++)
|
|
|
|
{
|
2013-12-01 13:33:59 -05:00
|
|
|
volatile int x = DAC.writeDAC(i);
|
2013-11-30 17:41:37 -05:00
|
|
|
}
|
|
|
|
end = micros();
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("1000x DAC.writeDAC(i):\t\t");
|
|
|
|
Serial.println(end - start);
|
|
|
|
|
|
|
|
start = micros();
|
|
|
|
for (int i=0; i< 10; i++)
|
|
|
|
{
|
|
|
|
volatile int x = DAC.writeDAC(i, true);
|
|
|
|
}
|
|
|
|
end = micros();
|
|
|
|
Serial.print("10x DAC.writeDAC(i, true):\t");
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println(end - start);
|
|
|
|
|
|
|
|
start = micros();
|
|
|
|
for (int i=0; i< 1000; i++)
|
|
|
|
{
|
2013-12-01 13:33:59 -05:00
|
|
|
volatile int x = DAC.RDY();
|
2013-11-30 17:41:37 -05:00
|
|
|
}
|
|
|
|
end = micros();
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("1000x DAC.RDY():\t\t");
|
|
|
|
Serial.println(end - start);
|
|
|
|
|
|
|
|
while (!DAC.RDY());
|
|
|
|
DAC.writeDAC(0, true);
|
|
|
|
start = micros();
|
|
|
|
while (!DAC.RDY());
|
|
|
|
end = micros();
|
|
|
|
Serial.print("EEPROM write latency:\t\t");
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println(end - start);
|
|
|
|
|
|
|
|
#endif
|
2013-12-01 13:33:59 -05:00
|
|
|
|
|
|
|
#ifdef MCP4725_POWERDOWNMODE
|
|
|
|
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println("\nEXPERIMENTAL");
|
|
|
|
|
|
|
|
start = micros();
|
|
|
|
for (int i=0; i< 10; i++)
|
|
|
|
{
|
|
|
|
volatile int x = DAC.readPowerDownModeDAC();
|
|
|
|
}
|
|
|
|
end = micros();
|
|
|
|
Serial.print("10x DAC.readPowerDownModeDAC():\t\t");
|
|
|
|
Serial.println(end - start);
|
|
|
|
|
|
|
|
start = micros();
|
|
|
|
for (int i=0; i< 10; i++)
|
|
|
|
{
|
|
|
|
volatile int x = DAC.readPowerDownModeEEPROM();
|
|
|
|
}
|
|
|
|
end = micros();
|
|
|
|
Serial.print("10x DAC.readPowerDownModeEEPROM():\t");
|
|
|
|
Serial.println(end - start);
|
|
|
|
|
|
|
|
start = micros();
|
|
|
|
for (int i=0; i< 10; i++)
|
|
|
|
{
|
|
|
|
volatile int x = DAC.writePowerDownMode(i & 0x03);
|
|
|
|
}
|
|
|
|
end = micros();
|
|
|
|
Serial.print("10x DAC.writePowerDownMode(i):\t\t");
|
|
|
|
Serial.println(end - start);
|
2013-12-01 13:33:59 -05:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
Serial.print("\nDone... (start triangle mode)");
|
2013-11-24 15:59:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
2013-11-30 16:21:07 -05:00
|
|
|
for (int i=0; i< 4096; i++)
|
|
|
|
{
|
|
|
|
DAC.setValue(i);
|
|
|
|
delay(10);
|
|
|
|
}
|
|
|
|
for (int i=0; i< 4096; i++)
|
|
|
|
{
|
|
|
|
DAC.setValue(4096 - i);
|
|
|
|
delay(10);
|
|
|
|
}
|
2013-11-24 15:59:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-30 16:21:07 -05:00
|
|
|
|
2013-11-30 17:41:37 -05:00
|
|
|
|