Donal Morrissey / Mbed 2 deprecated MCP4725_Library_Test

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
donalm
Date:
Sun Nov 03 11:22:01 2013 +0000
Parent:
3:e8da8b9d757e
Commit message:
Added feedback test on DAC output.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Oct 27 14:28:55 2013 +0000
+++ b/main.cpp	Sun Nov 03 11:22:01 2013 +0000
@@ -3,17 +3,25 @@
 #include "mcp4725.h"
 
 /**
- * A basic test suite for the MCP4725 DAC Library.
+ * A test suite for the MCP4725 DAC Library. This Test Application requires the following:
+ *  * An MCP4725 connected to the IC2 bus of the mbed LPC1768.
+ *  * The analog out of the MCP1768 connected to Pin15 (analog in) of the mbed LPC1768.
+ *  * An optional LED & resistor connected to analog output for visual feedback of the running tests.
+ *
+ * This application tests the MCP4725 library's setting of the MCP4725's internal registers (inclusing eeprom storage), 
+ * it's analog output and power modes.
+ *
  */
 
-DigitalOut myled(LED1);
+DigitalOut testLed(LED1);
 Serial pc(USBTX, USBRX);
+AnalogIn analogIn(p15);
 
 void test_mcp4725_library(PinName sda, PinName scl, MCP4725::BusFrequency bus_frequency, int device_address_bits, char* testName);
 int  test_init(MCP4725* interface, char* testName);
-int  test_simple_read_write_no_eeprom(MCP4725* interface, enum MCP4725::PowerMode mode, int dac_value);
-int  test_simple_read_write_with_eeprom(MCP4725* interface, enum MCP4725::PowerMode mode, int dac_value);
-int test_eeprom_write_status(MCP4725* interface);
+int  test_simple_read_write_no_eeprom(MCP4725* interface, enum MCP4725::PowerMode mode, float output_voltage);
+int  test_simple_read_write_with_eeprom(MCP4725* interface, enum MCP4725::PowerMode mode, float output_voltage);
+int  test_eeprom_write_status(MCP4725* interface);
 
 int main()
 {
@@ -24,9 +32,9 @@
     test_mcp4725_library(p9, p10, MCP4725::HighSpeed3_4Mhz, 0, "p9, p10, MCP4725::HighSpeed3_4Mhz");
 
     while(1) {
-        myled = 1;
+        testLed = 1;
         wait(0.5);
-        myled = 0;
+        testLed = 0;
         wait(0.5);
     }
 }
@@ -47,17 +55,17 @@
     
     if(test_init(&mcp4725_interface, testName) == 0)
     {
-        test_simple_read_write_no_eeprom(&mcp4725_interface, MCP4725::Normal, 3095);
-        test_simple_read_write_no_eeprom(&mcp4725_interface, MCP4725::PowerDown1k, 2094);
-        test_simple_read_write_no_eeprom(&mcp4725_interface, MCP4725::PowerDown100k, 2093);
-        test_simple_read_write_no_eeprom(&mcp4725_interface, MCP4725::PowerDown500k, 92);
-        test_simple_read_write_no_eeprom(&mcp4725_interface, MCP4725::Normal, 4095);
+        test_simple_read_write_no_eeprom(&mcp4725_interface, MCP4725::Normal, 3.0);
+        test_simple_read_write_no_eeprom(&mcp4725_interface, MCP4725::PowerDown1k, 1.3);
+        test_simple_read_write_no_eeprom(&mcp4725_interface, MCP4725::PowerDown100k, 1.8);
+        test_simple_read_write_no_eeprom(&mcp4725_interface, MCP4725::PowerDown500k, 0.5);
+        test_simple_read_write_no_eeprom(&mcp4725_interface, MCP4725::Normal, 2.5);
         
-        test_simple_read_write_with_eeprom(&mcp4725_interface, MCP4725::Normal, 3095);
-        test_simple_read_write_with_eeprom(&mcp4725_interface, MCP4725::PowerDown1k, 2094);
-        test_simple_read_write_with_eeprom(&mcp4725_interface, MCP4725::PowerDown100k, 2093);
-        test_simple_read_write_with_eeprom(&mcp4725_interface, MCP4725::PowerDown500k, 92);
-        test_simple_read_write_with_eeprom(&mcp4725_interface, MCP4725::Normal, 4095);
+        test_simple_read_write_with_eeprom(&mcp4725_interface, MCP4725::Normal, 3.0);
+        test_simple_read_write_with_eeprom(&mcp4725_interface, MCP4725::PowerDown1k, 1.3);
+        test_simple_read_write_with_eeprom(&mcp4725_interface, MCP4725::PowerDown100k, 1.8);
+        test_simple_read_write_with_eeprom(&mcp4725_interface, MCP4725::PowerDown500k, 0.5);
+        test_simple_read_write_with_eeprom(&mcp4725_interface, MCP4725::Normal, 2.5);
         
         test_eeprom_write_status(&mcp4725_interface);
     }
@@ -87,7 +95,7 @@
 }
 
 /* Run a simple read/write test that does not write to eeprom. */
-int test_simple_read_write_no_eeprom(MCP4725* interface, enum MCP4725::PowerMode mode, int dac_value)
+int test_simple_read_write_no_eeprom(MCP4725* interface, enum MCP4725::PowerMode mode, float output_voltage)
 {
     enum MCP4725::PowerMode read_mode;
     enum MCP4725::PowerMode read_mode_eeprom;//, write_mode_eeprom;
@@ -95,6 +103,8 @@
     int  read_dac_value_eeprom;//, write_dac_value_eeprom;
     bool read_eeprom_write_in_progress;
     
+    int dac_value = (int) (0xFFF * (output_voltage/3.3) );
+    
     pc.printf("\tRead/write test without write to eeprom: ");
     
     interface->write(mode, dac_value, false);
@@ -106,20 +116,44 @@
     }
     else
     {
-        pc.printf("Passed\r\n");
-        return 0;
+        // We need to wait a short period for the output to stabalise.
+        wait(0.2);
+        
+        /* We check at mV resolution. 
+         * Resolution of 12bit DAC is 0.8mV
+         * Resolution of 16bit ADC is 0.05mV
+         */
+        int read_value_mv = (analogIn.read() * 3.3) * 100;
+        int set_value_mv = ((int)(output_voltage*100));
+        
+        if( (mode == MCP4725::Normal) && (read_value_mv !=  set_value_mv) )
+        {
+            pc.printf("Failed: set_value=%dmV, read_value=%dmV\r\n", read_value_mv, set_value_mv);
+            return 1;
+        }
+        else if ( (mode != MCP4725::Normal) && (read_value_mv != 0) )
+        {
+            pc.printf("Failed: read_value=%dmV, but DAC in PowerDown!\r\n", read_value_mv);
+            return 1;
+        }
+        else
+        {
+            pc.printf("Passed\r\n");
+            return 0;
+        }
     }
 }
 
 
 /* Run a simple read/write test that does write to eeprom. */
-int test_simple_read_write_with_eeprom(MCP4725* interface, enum MCP4725::PowerMode mode, int dac_value)
+int test_simple_read_write_with_eeprom(MCP4725* interface, enum MCP4725::PowerMode mode, float output_voltage)
 {
     enum MCP4725::PowerMode read_mode;
     enum MCP4725::PowerMode read_mode_eeprom;//, write_mode_eeprom;
     int  read_dac_value;
     int  read_dac_value_eeprom;//, write_dac_value_eeprom;
     bool read_eeprom_write_in_progress;
+    int dac_value = (int) (0xFFF * (output_voltage/3.3) );
     
     pc.printf("\tRead/write test with write to eeprom: ");