Programmable Load V1

Dependencies:   USBDevice mbed

Fork of THzBiasControl08 by malcolm lear

Revision:
11:ceabd476b6cd
Parent:
10:6924c58f3a91
--- a/main.cpp	Wed Feb 22 14:53:00 2017 +0000
+++ b/main.cpp	Thu Mar 08 17:13:58 2018 +0000
@@ -1,92 +1,55 @@
 #include "mbed.h"
 #include "USBSerial.h"
 
-SPI spi(p5, p6, p7);      // DAC mosi, miso, sclk
-DigitalOut cs(p8);        // active low DAC enable
-DigitalOut outen(p21);    // active low pulse output enable
-DigitalOut outxpol(p23);  // pulse output x polarity
-DigitalOut outypol(p22);  // pulse output y polarity
+AnalogIn   ain(p20);      // read load voltage
+AnalogOut  aout(p18);     // set load current
 USBSerial serial;         // USB serial communication
 
-int v = 0;                // voltage 0 to 1023 = 0 to 102.3 V
+float io = 0;             // load current 0 to 1023
+int vi;                   // load voltage
 int t, i, e, len;
 char CmdIn[128];
-char CmdP0[] = "P=0";     // polarity 0
-char CmdP1[] = "P=1";     // polarity 1
-char CmdP2[] = "P=2";     // polarity 2
-char CmdP3[] = "P=3";     // polarity 3
-char CmdO0[] = "O=0";     // output off
-char CmdO1[] = "O=1";     // output on
-char CmdVn[] = "V=";      // voltage
+char CmdIL[] = "IL=";     // set load current
+char CmdVL[] = "VL";      // return load voltage
 char CmdID[] = "ID";      // Identification
 
-void Voltage(int d) {
-    d = (d<<4)&0x3fff;    // value is bit 13 to 4
-    cs = 0;
-    spi.write(d);
-    cs = 1;
+void SetCurrent(float d) {
+    d = d - (d / 10) - 10; // Bodge it !!
+    d = d / 1024;         //
+    aout = d;
 }
 
 int main() {
-    outen = 1;            // output off
-    outxpol = 0;
-    outypol = 0;
-    cs = 1;               // spi not selected
-    spi.format(16,2);
-    spi.frequency(1000000);
-    Voltage(v);
+    SetCurrent(io);
     while(1)
     {
         e = 1;            // set error
         serial.scanf("%s", CmdIn);
-        if (strcmp(CmdIn, CmdP0) == 0) {
-            outxpol = 1;
-            outypol = 1;
-            e = 0;
-        }
-        if (strcmp(CmdIn, CmdP1) == 0) {
-            outxpol = 1;
-            outypol = 0;
-            e = 0;
-        }
-        if (strcmp(CmdIn, CmdP2) == 0) {
-            outxpol = 0;
-            outypol = 0;
+        if (strcmp(CmdIn, CmdVL) == 0) {
+            vi = ain * 1024;
+            serial.printf("%d\n", vi);           
             e = 0;
         }
-        if (strcmp(CmdIn, CmdP3) == 0) {
-            outxpol = 0;
-            outypol = 1;
-            e = 0;
-        }
-        if (strcmp(CmdIn, CmdO0) == 0) {
-            outen = 1;
-            e = 0;
-        }
-        if (strcmp(CmdIn, CmdO1) == 0) {
-            outen = 0;
-            e = 0;
-        }
-        if (strncmp(CmdIn, CmdVn, 2) == 0) {
-            v = 0;
+        if (strncmp(CmdIn, CmdIL, 3) == 0) {
+            io = 0;
             len = strlen(CmdIn);
-            for(i=2; i<len; i++){
+            for(i=3; i<len; i++){
                 t = CmdIn[i] - '0';
                 if ((t >= 0) && (t <= 9)) { 
-                    v = v * 10 + t; 
+                    io = io * 10 + t; 
                 }
                 else {
-                    v = -1;   // on input error make v out of range
+                    io = -1;   // on input error make v out of range
                     break;
                 }
             }
-            if ((v >= 0) && (v <= 1023)) {
-                Voltage(v);
+            if ((io >= 0) && (io <= 1023)) {
+                SetCurrent(io);
                 e = 0;  
             }
         }
         if (strcmp(CmdIn, CmdID) == 0) {
-            serial.printf( "Bias Voltage Generator V1.0\n");
+            serial.printf( "Load Monitor V1.0\n");
             e = 0;
         }
         if (e == 1) {