2013-11-24 15:59:40 -05:00
|
|
|
//
|
|
|
|
// FILE: mcp4725_test.ino
|
|
|
|
// AUTHOR: Rob Tillaart
|
2020-11-27 05:20:37 -05:00
|
|
|
// VERSION: 0.1.5
|
2013-11-24 15:59:40 -05:00
|
|
|
// PURPOSE: test mcp4725 lib
|
|
|
|
// DATE: 2013-11-24
|
2020-11-27 05:20:37 -05:00
|
|
|
// URL: https://github.com/RobTillaart/MCP4725
|
2013-11-24 15:59:40 -05:00
|
|
|
//
|
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
#include "Wire.h"
|
2013-11-24 15:59:40 -05:00
|
|
|
#include "MCP4725.h"
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP4725 MCP(0x62); // 0x62 or 0x63
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
volatile int x;
|
|
|
|
uint32_t start, stop;
|
|
|
|
|
|
|
|
void setup()
|
2013-11-24 15:59:40 -05:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP.begin();
|
2020-11-27 05:20:37 -05:00
|
|
|
test1();
|
|
|
|
test2();
|
|
|
|
test3();
|
|
|
|
test4();
|
|
|
|
test5();
|
|
|
|
test6();
|
|
|
|
}
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
void test1()
|
|
|
|
{
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("\nValue:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.getValue());
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println();
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 100; i < 500; i += 100)
|
2013-12-01 13:33:59 -05:00
|
|
|
{
|
|
|
|
Serial.print("setValue(");
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.print(")\n");
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP.setValue(i);
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("Value:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.getValue());
|
2013-12-01 13:33:59 -05:00
|
|
|
}
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println();
|
2020-11-27 05:20:37 -05:00
|
|
|
}
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
void test2()
|
|
|
|
{
|
|
|
|
Serial.println("\n\nMCP4725_II\n\n");
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 100; i < 500; i += 100)
|
2013-12-01 13:33:59 -05:00
|
|
|
{
|
|
|
|
Serial.print("writeDAC(");
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.print(")\n");
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP.writeDAC(i);
|
|
|
|
Serial.print("MCPValue:\t");
|
|
|
|
Serial.println(MCP.readDAC());
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("EEValue:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.readEEPROM());
|
2013-12-01 13:33:59 -05:00
|
|
|
}
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println();
|
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 100; i < 500; i += 100)
|
2013-12-01 13:33:59 -05:00
|
|
|
{
|
|
|
|
Serial.print("writeDAC(");
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.print(", true)\n");
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP.writeDAC(i, true);
|
|
|
|
Serial.print("MCPValue:\t");
|
|
|
|
Serial.println(MCP.readDAC());
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("EEValue:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.readEEPROM());
|
2013-12-01 13:33:59 -05:00
|
|
|
}
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
Serial.println("writeDAC(200)");
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP.writeDAC(200);
|
|
|
|
Serial.print("MCPValue:\t");
|
|
|
|
Serial.println(MCP.readDAC());
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("EEValue:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.readEEPROM());
|
2013-11-30 17:41:37 -05:00
|
|
|
Serial.println();
|
2020-11-27 05:20:37 -05:00
|
|
|
}
|
2013-11-30 17:41:37 -05:00
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
void test3()
|
|
|
|
{
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.println("\n\nMCP4725_POWERDOWNMODE\n\n");
|
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 0; i < 4; i++)
|
2013-12-01 16:09:02 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("MCP.writePowerDownMode(");
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.print(i);
|
|
|
|
Serial.println(")");
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP.writePowerDownMode(i);
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.print("EPR PDM Value:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.readPowerDownModeEEPROM());
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
Serial.println("\n\nEXPERIMENTAL");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println("MCP.writePowerDownMode(3)");
|
|
|
|
MCP.writePowerDownMode(3);
|
|
|
|
MCP.writeDAC(305);
|
2013-12-04 13:42:58 -05:00
|
|
|
Serial.print("Value:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.getValue());
|
|
|
|
Serial.println("MCP.powerOnReset()");
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println("Before");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("MCP PDM Value:\t");
|
|
|
|
Serial.println(MCP.readPowerDownModeDAC());
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.print("EPR PDM Value:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.readPowerDownModeEEPROM());
|
|
|
|
Serial.print("MCPValue:\t");
|
|
|
|
Serial.println(MCP.readDAC());
|
2013-12-04 13:42:58 -05:00
|
|
|
Serial.print("EEValue:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.readEEPROM());
|
|
|
|
MCP.powerOnReset();
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println("After");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("MCP PDM Value:\t");
|
|
|
|
Serial.println(MCP.readPowerDownModeDAC());
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.print("EPR PDM Value:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.readPowerDownModeEEPROM());
|
|
|
|
Serial.print("MCPValue:\t");
|
|
|
|
Serial.println(MCP.readDAC());
|
2013-12-04 13:42:58 -05:00
|
|
|
Serial.print("EEValue:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.readEEPROM());
|
2013-12-04 13:42:58 -05:00
|
|
|
Serial.print("Value:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.getValue());
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println();
|
2020-11-27 05:20:37 -05:00
|
|
|
}
|
2013-12-01 16:09:02 -05:00
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
void test4()
|
|
|
|
{
|
|
|
|
Serial.println("\n\nEXPERIMENTAL");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println("MCP.writePowerDownMode(2)");
|
|
|
|
MCP.writePowerDownMode(2);
|
|
|
|
MCP.writeDAC(405);
|
2013-12-04 13:42:58 -05:00
|
|
|
Serial.print("Value:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.getValue());
|
|
|
|
Serial.println("MCP.powerOnWakeUp()");
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println("Before");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("MCP PDM Value:\t");
|
|
|
|
Serial.println(MCP.readPowerDownModeDAC());
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.print("EPR PDM Value:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.readPowerDownModeEEPROM());
|
|
|
|
Serial.print("MCPValue:\t");
|
|
|
|
Serial.println(MCP.readDAC());
|
2013-12-04 13:42:58 -05:00
|
|
|
Serial.print("EEValue:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.readEEPROM());
|
2020-11-27 05:20:37 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP.powerOnWakeUp();
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println("after");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("MCP PDM Value:\t");
|
|
|
|
Serial.println(MCP.readPowerDownModeDAC());
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.print("EPR PDM Value:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.readPowerDownModeEEPROM());
|
|
|
|
Serial.print("MCPValue:\t");
|
|
|
|
Serial.println(MCP.readDAC());
|
2013-12-04 13:42:58 -05:00
|
|
|
Serial.print("EEValue:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.readEEPROM());
|
2013-12-04 13:42:58 -05:00
|
|
|
Serial.print("Value:\t");
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.println(MCP.getValue());
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println();
|
2020-11-27 05:20:37 -05:00
|
|
|
}
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
void test5()
|
|
|
|
{
|
2013-12-01 16:09:02 -05:00
|
|
|
Serial.println("\n\nPERFORMANCE");
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.println();
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
start = micros();
|
|
|
|
for (int i = 0; i < 1000; i++)
|
2013-11-24 15:59:40 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
x = MCP.getValue();
|
2013-11-24 15:59:40 -05:00
|
|
|
}
|
2020-11-27 05:20:37 -05:00
|
|
|
stop = micros();
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("1000x MCP.getValue():\t\t");
|
2020-11-27 05:20:37 -05:00
|
|
|
Serial.println(stop - start);
|
2013-11-24 15:59:40 -05:00
|
|
|
|
|
|
|
start = micros();
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 0; i < 1000; i++)
|
2013-11-24 15:59:40 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP.setValue(i);
|
2013-11-24 15:59:40 -05:00
|
|
|
}
|
2020-11-27 05:20:37 -05:00
|
|
|
stop = micros();
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("1000x MCP.setValue(i):\t\t");
|
2020-11-27 05:20:37 -05:00
|
|
|
Serial.println(stop - start);
|
2013-11-30 17:41:37 -05:00
|
|
|
|
|
|
|
start = micros();
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 0; i < 1000; i++)
|
2013-11-30 17:41:37 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP.setValue(1000);
|
2013-11-30 17:41:37 -05:00
|
|
|
}
|
2020-11-27 05:20:37 -05:00
|
|
|
stop = micros();
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("1000x MCP.setValue(1000):\t");
|
2020-11-27 05:20:37 -05:00
|
|
|
Serial.println(stop - start);
|
2013-11-30 17:41:37 -05:00
|
|
|
|
2013-11-24 15:59:40 -05:00
|
|
|
start = micros();
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 0; i < 1000; i++)
|
2013-11-24 15:59:40 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
x = MCP.readDAC();
|
2013-11-24 15:59:40 -05:00
|
|
|
}
|
2020-11-27 05:20:37 -05:00
|
|
|
stop = micros();
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("1000x MCP.readDAC():\t\t");
|
2020-11-27 05:20:37 -05:00
|
|
|
Serial.println(stop - start);
|
2013-11-24 15:59:40 -05:00
|
|
|
|
2013-11-30 17:41:37 -05:00
|
|
|
start = micros();
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 0; i < 1000; i++)
|
2013-11-30 17:41:37 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
x = MCP.writeDAC(i);
|
2013-11-30 17:41:37 -05:00
|
|
|
}
|
2020-11-27 05:20:37 -05:00
|
|
|
stop = micros();
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("1000x MCP.writeDAC(i):\t\t");
|
2020-11-27 05:20:37 -05:00
|
|
|
Serial.println(stop - start);
|
2013-12-01 13:33:59 -05:00
|
|
|
|
|
|
|
start = micros();
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 0; i < 10; i++)
|
2013-12-01 13:33:59 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
x = MCP.writeDAC(i, true);
|
2013-12-01 13:33:59 -05:00
|
|
|
}
|
2020-11-27 05:20:37 -05:00
|
|
|
stop = micros();
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("10x MCP.writeDAC(i, true):\t");
|
2020-11-27 05:20:37 -05:00
|
|
|
Serial.println(stop - start);
|
2013-11-30 17:41:37 -05:00
|
|
|
|
|
|
|
start = micros();
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 0; i < 1000; i++)
|
2013-11-30 17:41:37 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
x = MCP.ready();
|
2013-11-30 17:41:37 -05:00
|
|
|
}
|
2020-11-27 05:20:37 -05:00
|
|
|
stop = micros();
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("1000x MCP.ready():\t\t");
|
2020-11-27 05:20:37 -05:00
|
|
|
Serial.println(stop - start);
|
2013-12-01 13:33:59 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
while (!MCP.ready());
|
|
|
|
MCP.writeDAC(0, true);
|
2013-12-01 13:33:59 -05:00
|
|
|
start = micros();
|
2021-01-29 06:31:58 -05:00
|
|
|
while (!MCP.ready());
|
2020-11-27 05:20:37 -05:00
|
|
|
stop = micros();
|
2013-12-01 13:33:59 -05:00
|
|
|
Serial.print("EEPROM write latency:\t\t");
|
2020-11-27 05:20:37 -05:00
|
|
|
Serial.println(stop - start);
|
|
|
|
}
|
2013-12-01 13:33:59 -05:00
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
void test6()
|
|
|
|
{
|
|
|
|
Serial.println("\n\nEXPERIMENTAL II");
|
2013-12-01 16:09:02 -05:00
|
|
|
|
|
|
|
start = micros();
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 0; i < 10; i++)
|
2013-12-01 16:09:02 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
x = MCP.readPowerDownModeDAC();
|
2013-12-01 16:09:02 -05:00
|
|
|
}
|
2020-11-27 05:20:37 -05:00
|
|
|
stop = micros();
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("10x MCP.readPowerDownModeDAC():\t\t");
|
2020-11-27 05:20:37 -05:00
|
|
|
Serial.println(stop - start);
|
2013-12-01 16:09:02 -05:00
|
|
|
|
|
|
|
start = micros();
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 0; i < 10; i++)
|
2013-12-01 16:09:02 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
x = MCP.readPowerDownModeEEPROM();
|
2013-12-01 16:09:02 -05:00
|
|
|
}
|
2020-11-27 05:20:37 -05:00
|
|
|
stop = micros();
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("10x MCP.readPowerDownModeEEPROM():\t");
|
2020-11-27 05:20:37 -05:00
|
|
|
Serial.println(stop - start);
|
2013-12-01 16:09:02 -05:00
|
|
|
|
|
|
|
start = micros();
|
2020-11-27 05:20:37 -05:00
|
|
|
for (int i = 0; i < 10; i++)
|
2013-12-01 16:09:02 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
x = MCP.writePowerDownMode(i & 0x03);
|
2013-12-01 16:09:02 -05:00
|
|
|
}
|
2020-11-27 05:20:37 -05:00
|
|
|
stop = micros();
|
2021-01-29 06:31:58 -05:00
|
|
|
Serial.print("10x MCP.writePowerDownMode(i):\t\t");
|
2020-11-27 05:20:37 -05:00
|
|
|
Serial.println(stop - start);
|
2013-12-01 13:33:59 -05:00
|
|
|
|
|
|
|
Serial.print("\nDone... (start triangle mode)");
|
2013-11-24 15:59:40 -05:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
void loop()
|
2013-11-24 15:59:40 -05:00
|
|
|
{
|
2020-11-27 05:20:37 -05:00
|
|
|
for (uint16_t i = 0; i < 4096; i++)
|
2013-11-30 16:21:07 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP.setValue(i);
|
2013-11-30 16:21:07 -05:00
|
|
|
delay(10);
|
|
|
|
}
|
2020-11-27 05:20:37 -05:00
|
|
|
for (uint16_t i = 0; i < 4096; i++)
|
2013-11-30 16:21:07 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP.setValue(4096 - i);
|
2013-11-30 16:21:07 -05:00
|
|
|
delay(10);
|
|
|
|
}
|
2013-11-24 15:59:40 -05:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
// -- END OF FILE --
|