The project is a fast lock in amplifier (LIA) which can update its output at rate of 1000 measurements/s. It performs digital dual mixing and filtering to obtain a DC value proportional to the AC input signal.

Dependencies:   N5110 mbed

Revision:
1:bf693859586c
Parent:
0:4e20939af8bb
Child:
2:c9b24787d5e1
diff -r 4e20939af8bb -r bf693859586c main.cpp
--- a/main.cpp	Mon Aug 21 11:22:14 2017 +0000
+++ b/main.cpp	Mon Aug 21 11:43:03 2017 +0000
@@ -9,18 +9,19 @@
     while (ref_freq < 1e2) {
         sleep();
     }
-    //ref_freq = 5e3;
     /// make sure frequency is read before we go to the program
     /// cancel event-triggered rise interrupt, not to interfere with program
     dref.rise(NULL);
     pc.printf("Ref_Freq is:%.2f kHz\n\r",ref_freq*0.001);
-    //constant
+    /// constant 6 for correct sampling time 
+    /// compensates for delay caused by computations
     sample_freq = 6*samples16*ref_freq;
     sample_time = 1/sample_freq;
 
     initDAC();
     delay_freq = ref_freq*amp_points;
     amplitude_delay = 1/delay_freq;
+    /// find the offset of the signal
     offset_ticker.attach(&offset_isr,0.001);
 
     while (offset == 0) {
@@ -31,18 +32,20 @@
         sleep();
     }
     offset_ticker.detach();
-
+    /// once the offset is calculated detach the offset ticker
+    /// attach the output ticker to update every 1 ms
     output_ticker.attach(&output_isr,0.00099);
     
     while (true) {
         // gpo = !gpo;
-        digitalMix(offset);
-        while (g_output_flag == 0) {sleep();} 
-        
+        digitalMix(offset); /// perform digital mixing 
+        while (g_output_flag == 0) {sleep();} /// sleep until flag is set
+        /// update output
         if (g_output_flag == 1) {
             g_output_flag = 0;
             //aout = max(samples16);
-            aout = 2*max(samples16);
+            aout = 2*max(samples16); 
+            /// DC output by taking the maximum value of the mixed signal (R)
         }
     }
 }
@@ -53,7 +56,7 @@
 
     for (int i = 0; i < points; i++) {
         if (amp < R[i])
-            amp = R[i];
+            amp = R[i]; /// find max of R
         //wait(amplitude_delay);
     }
     return amp;
@@ -68,7 +71,7 @@
         avg = avg + signal;
          wait((float)(5e-5));
     }
-    avg = avg/filter_points;
+    avg = avg/filter_points; /// find offset of input signal
     return avg;
 }
 
@@ -129,14 +132,14 @@
         pc.printf("Divider1== %u div2=%u \r\n",div1,div2);
         pc.printf("MCGOUTCLK= %u,  busClk = %u \r\n",SystemCoreClock*div1,busClk);
         /// MCGOUTCLK 120 MHz, Bus Clock = 120 MHz
-        ADC0->SC3 &= ~ADC_SC3_AVGE_MASK;//disable averages
-        ADC0->CFG1 &= ~ADC_CFG1_ADLPC_MASK;//high-power mode
-        ADC0->CFG1 &= ~0x0063 ; //clears ADICLK and ADIV
-        ADC0->CFG1 |= ADC_CFG1_ADIV(2); //divide clock 0=/1, 1=/2, 2=/4, 3=/8
+        ADC1->SC3 &= ~ADC_SC3_AVGE_MASK;//disable averages
+        ADC1->CFG1 &= ~ADC_CFG1_ADLPC_MASK;//high-power mode
+        ADC1->CFG1 &= ~0x0063 ; //clears ADICLK and ADIV
+        ADC1->CFG1 |= ADC_CFG1_ADIV(2); //divide clock 0=/1, 1=/2, 2=/4, 3=/8
         //ADC0->SC3 |= 0x0007;//enable 32 averages
 
-        if (((ADC0->CFG1)& 0x03) == 0) adcClk = busClk/(0x01<<(((ADC0->CFG1)&0x60)>>5));
-        if (((ADC0->SC3)& 0x04) != 0) adcClk = adcClk/(0x01<<(((ADC0->SC3)&0x03)+2));
+        if (((ADC1->CFG1)& 0x03) == 0) adcClk = busClk/(0x01<<(((ADC1->CFG1)&0x60)>>5));
+        if (((ADC1->SC3)& 0x04) != 0) adcClk = adcClk/(0x01<<(((ADC1->SC3)&0x03)+2));
         pc.printf("adcCLK= %u  \r\n",adcClk);
         /// ADC Clock: 60 MHz
     }