2

Dependencies:   mbed DevInterfaces MCP4728 MCP4728setaddr I2Cinterfaces

Revision:
9:2d70aa45f16e
Parent:
8:abdd660056fb
diff -r abdd660056fb -r 2d70aa45f16e main.cpp
--- a/main.cpp	Wed Feb 20 14:53:58 2019 +0000
+++ b/main.cpp	Fri Mar 22 12:48:52 2019 +0000
@@ -42,10 +42,6 @@
 I2CInterface* i2cdev= mbedi2cp;
 const float Vdd = 4.97;
 
-DigitalOut LDAC(p5);
-DigitalOut CntPin(p6);
-
-
 
 Serial pc(USBTX, USBRX);
 
@@ -81,7 +77,7 @@
 
 DigitalOut m4x5CLK(p20);
 
-DigitalOut mRefShutters(p30);
+DigitalOut mRefShutters(p5);
 
 bool Shut1 = 0;
 bool Shut2 = 0;
@@ -95,10 +91,10 @@
 {
     int ret = 0;
     char z;
-    float voltage = 0;
+    int voltCount = 0;
     string instr;
 
-    printf("\n\rEnter Voltage \n\r");
+    //printf("\n\rEnter Voltage \n\r");
     while (ret == 0)
     {
         
@@ -107,28 +103,29 @@
         
         if( z == 13 )
             {
-                voltage = atof( instr.c_str() );
+                voltCount = atoi( instr.c_str() );
                 
-                if(  voltage > Vdd  || voltage < 0)
+                if(  voltCount > 4095  || voltCount < 0)
                 {
-                    pc.printf("\n\r Voltage Entered Higher than Vdd Setting Voltage to Maximum\n\r");
+                    //////pc.printf("\n\r Voltage Entered Higher than Vdd Setting Voltage to Maximum\n\r");
+                    dac->setDACvalue(4095,chan);
                 }
                 else
                 {
-                        if ( dac->setVoltage(voltage,chan) )
+                        if ( dac->setDACvalue(voltCount,chan) )
                         { 
-                            printf("failed to set dac value %f for channel %d\n\r",voltage,chan);
+                            //printf("failed to set dac value %f for channel %d\n\r",voltCount,chan);
                         }
-                        //pc.printf("Voltage set to %3fV\n\r", voltage);
+                        ////////pc.printf("Voltage set to %3fV\n\r", voltage);
                         if( dac->update() )
                         {
-                            printf("\n\rfailed to readback channel info \n\r");
+                            //printf("\n\rfailed to readback channel info \n\r");
                         }
                         else 
                         {
-                                (void)dac->getVoltage(voltage,chan);//no need to test done with updat 
-                                printf(" CH%d %f[V]",chan,voltage);
-                                printf("\n\r");
+                                (void)dac->getDACvalue(voltCount,chan);//no need to test done with updat 
+                                //printf(" CH%d %d Counts",chan,voltCount);
+                                //printf("\n\r");
                     
                         }
                             
@@ -139,7 +136,7 @@
             }
         else
             {
-                pc.printf("%c",z);
+                ////pc.printf("%c",z);
                 instr.push_back(z);
             }
     }
@@ -159,7 +156,7 @@
 
   addr = 0;
    MCP4728  dac( i2cdev ,addr, Vdd);  // assuming the address  of the MCP4728 is set to 0 factory default 
-   printf("\n\raddr %d MCP4728 :%s\n\r",addr,dac.getversioninfo());
+   //printf("\n\raddr %d MCP4728 :%s\n\r",addr,dac.getversioninfo());
    wait(1);
 
    int cnt=0;
@@ -168,14 +165,14 @@
 
 
 
-pc.printf("Agiltron Serial Control Program\n\r");
-pc.printf("'[' 4x4 Swtiching \n\r");
-pc.printf("']' 4x5 Swtiching\n\r");
-pc.printf("1, 2, 3, 4 Toggle Single Shutters\n\r");
-pc.printf("'o' Opens all Shutters\n\r");
-pc.printf("'r' Closes all Shutters \n\r");
-pc.printf("',' 4 Channel DAC Control \n\r");
-pc.printf("'.' Single Channel DAC Control \n\r");
+//pc.printf("Agiltron Serial Control Program\n\r");
+//pc.printf("'[' 4x4 Swtiching \n\r");
+//pc.printf("']' 4x5 Swtiching\n\r");
+//pc.printf("1, 2, 3, 4 Toggle Single Shutters\n\r");
+//pc.printf("'o' Opens all Shutters\n\r");
+//pc.printf("'r' Closes all Shutters \n\r");
+//pc.printf("',' 4 Channel DAC Control \n\r");
+//pc.printf("'.' Single Channel DAC Control \n\r");
 
 //Set up dac for external voltage
 dac.setRefExtern();
@@ -198,6 +195,7 @@
     
     m4x4TTL1 = 1;
     float voltage = 0;
+    int voltCount = 0;
     string instr;
 
 while(1) 
@@ -208,34 +206,68 @@
         {
             Shut1 = !Shut1;
             SH.writeShutters(Shut1,Shut2,Shut3,Shut4);
+            if(Shut1)
+            {
+                ////pc.printf("Shutter 1 Closed\n\r");
+            }
+            else
+            {
+                ////pc.printf("Shutter 1 Opened\n\r");
+            }
         }
         
         else if( c == '2') //Toggle Shutter 2
         {
             Shut2 = !Shut2;
             SH.writeShutters(Shut1,Shut2,Shut3,Shut4);
+            if(Shut2)
+            {
+                ////pc.printf("Shutter 2 Closed\n\r");
+            }
+            else
+            {
+                ////pc.printf("Shutter 2 Opened\n\r");
+            }
         }
         
         else if( c == '3') //Toggle Shutter 3
         {
             Shut3 = !Shut3;
             SH.writeShutters(Shut1,Shut2,Shut3,Shut4);
+            if(Shut3)
+            {
+                ////pc.printf("Shutter 3 Closed\n\r");
+            }
+            else
+            {
+                ////pc.printf("Shutter 3 Opened\n\r");
+            }
         }
         
         else if( c == '4') //Toggle Shutter 4
         {
             Shut4 = !Shut4;
             SH.writeShutters(Shut1,Shut2,Shut3,Shut4);
+            if(Shut4)
+            {
+                ////pc.printf("Shutter 4 Closed\n\r");
+            }
+            else
+            {
+                ////pc.printf("Shutter 4 Opened\n\r");
+            }
         }
         
         else if( c == 'u')
         {
             mRefShutters = 0;
+            ////pc.printf("Referenced Shutters Opened\n\r");
         }
         
         else if( c == 'y')
         {
             mRefShutters = 1;
+            ////pc.printf("Reference Shutters Closed\n\r");
         }
         
         else if(c == 'o')  //Open All Shutters
@@ -245,6 +277,7 @@
             Shut3 = 1;
             Shut4 = 1;
             SH.writeShutters(1,1,1,1);
+            ////pc.printf("All Shutters Opened\n\r");
         }
         
         else if(c == 'r')  //Close All Shutters
@@ -254,13 +287,14 @@
             Shut3 = 0;
             Shut4 = 0;
             SH.writeShutters(0,0,0,0);
+            ////pc.printf("All Shutters Closed\n\r");
         }
         
         else if( c == 91 ) //Left square bracket [ 4x4 Control
         {
             //Get 5 1's or 0's for binary number
             int count = 0;
-            pc.printf("4x4 Control: Enter 5 digit binary number\n\r");
+            ////pc.printf("4x4 Control: Enter 5 digit binary number\n\r");
             while ( count < 5 )
             {
                 c = pc.getc();
@@ -288,7 +322,7 @@
         {
             //Get 5 1's or 0's for binary number
             int count = 0;
-            pc.printf("4x5 Control: Enter 5 digit binary number\n\r");
+            ////pc.printf("4x5 Control: Enter 5 digit binary number\n\r");
             while ( count < 5 )
             {
                 c = pc.getc();
@@ -310,9 +344,38 @@
             pc.printf("Optical Path set to : %d%d%d%d%d\n\r", m4x5TTL1.read(), m4x5TTL2.read(), m4x5TTL3.read(), m4x5TTL4.read(), m4x5TTL5.read() ); //Confirm the correct output is set
         }
         
+        
+        else if( c == 'd')
+        {
+            int CH1cnt;
+            int CH2cnt;
+            int CH3cnt;
+            int CH4cnt;
+            //Print State of DAC
+            if( dac.update() == 0)
+            {
+                (void)dac.getDACvalue(CH1cnt,0);
+                (void)dac.getDACvalue(CH2cnt,1);
+                (void)dac.getDACvalue(CH3cnt,2);
+                (void)dac.getDACvalue(CH4cnt,3);
+                pc.printf("CH1:%4d CH2:%4d CH3:%4d CH4:%4d\n\r",CH1cnt,CH2cnt,CH3cnt,CH4cnt);
+            }
+            
+            else
+            {
+                pc.printf("Error: No readback from DAC\n\r");
+            }
+        }
+        
+        else if( c == 'a')
+        {
+            //Print State of Shutters
+            pc.printf("SH1:%d SH2:%d SH3:%d SH4:%d SHRef:%d\n\r",Shut1,Shut2,Shut3,Shut4,mRefShutters.read());
+        }    
+        
         else if( c == ',' ) //4 Channel DAC Control
         {
-            pc.printf("4 Channel DAC Control Enter voltage and Press Enter\n\r");
+            ////pc.printf("4 Channel DAC Control Enter voltage and Press Enter\n\r");
             int ret = 0;
             int chan = 0;
             while( ret ==0 && chan < 4)
@@ -320,19 +383,21 @@
                 char z = pc.getc();
                 if( z == 13 )
                 {
-                    voltage = atof( instr.c_str() );
+                    voltCount = atoi( instr.c_str() );
                     
-                    if( voltage == 0 || voltage > 5.0 )
+                    if( voltCount < 0 || voltCount > 4095 )
                     {
-                        pc.printf("ERROR\n\r");
+                        //////pc.printf("ERROR\n\r");
                     }
                     else
                     {
-                            if ( dac.setVoltage(voltage,chan) )
+                            if ( dac.setDACvalue(voltCount,chan) )
                             { 
-                                printf("failed to set dac value %d for channel %d\n\r",cnt,chan);
+                                //printf("failed to set dac value %d for channel %d\n\r",voltCount,chan);
                             }
-                            //pc.printf("Voltage set to %3fV\n\r", voltage);
+                            //////////pc.printf("Voltage set to %3fV\n\r", voltage);
+                            chan++;
+                            /*
                             if( dac.update() )
                             {
                                 printf("\n\rfailed to readback channel info \n\r");
@@ -344,6 +409,7 @@
                                     printf("\n\r");
                                     chan++;
                             }
+                            */
                                 
                     }
                     if( chan == 4)
@@ -355,22 +421,36 @@
     
                 }
                 
-                else if( z == ']')
+                else if( z == ']')//Sets all channels to full voltage
                 {
                     for ( int cc =0 ; cc <4 ; cc++) 
                     { 
-                        if ( dac.setDACvalue(4095,cc) )
+                        if ( dac.setDACvalue(3333,cc) )
                         {
-                            printf("failed to set dac value %d for channel %d\n\r",cnt,cc);
+                            //printf("failed to set dac value %d for channel %d\n\r",cnt,cc);
                         }
                     }
-                        printf("All Channels Set to 5V /n/r");
+                        //printf("All Channels Set to 5V \n\r");
+                        ret = 1;
+                }
+                
+                
+                else if( z == '[')//Sets all channels to 0 voltage
+                {
+                    for ( int cc =0 ; cc <4 ; cc++) 
+                    { 
+                        if ( dac.setDACvalue(1000,cc) )
+                        {
+                           // printf("failed to set dac value %d for channel %d\n\r",cnt,cc);
+                        }
+                    }
+                        //printf("All Channels Set to 0V \n\r");
                         ret = 1;
                 }
                 
                 else
                 {
-                    pc.printf("%c",z);
+                    //////pc.printf("%c",z);
                     instr.push_back(z);
                 }
     }
@@ -379,17 +459,17 @@
 
         else if( c == '.' ) //Single Channel DAC Control
         {
-            pc.printf("Single Channel DAC Control Press Channel Number\n\r");
+            ////pc.printf("Single Channel DAC Control Press Channel Number\n\r");
             char z = pc.getc();
     
-            if( z == '1' || z== '2' || z == '3' || z == '4' )
+            if( z == '1' || z== '2' || z == '3' || z == '0' )
             {
-                pc.printf("Channel %c Selected",z);
+                //////////pc.printf("Channel %c Selected",z);
                 DACSetChannel( atoi(&z) , &dac);
             }   
             else
             {
-                pc.printf("ERROR");
+                ////pc.printf("ERROR");
             }
         }