DCS_TEAM / Mbed 2 deprecated Chemical_Sensor_DMA

Dependencies:   mbed

Dependents:   DCS_FINAL_CODE

Fork of Chemical_Sensor_DMA by Jared Baxter

Files at this revision

API Documentation at this revision

Comitter:
baxterja
Date:
Fri Nov 06 19:28:49 2015 +0000
Parent:
4:9fd291254686
Child:
6:63de50ac29be
Commit message:
Dewayne, Use this one

Changed in this revision

SignalProcessing.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/SignalProcessing.cpp	Fri Nov 06 03:33:09 2015 +0000
+++ b/SignalProcessing.cpp	Fri Nov 06 19:28:49 2015 +0000
@@ -254,7 +254,7 @@
 
 
 
-
+/*
 #define FIR_100_LENGTH 64
 float FIR100_Sample1_i[FIR_100_LENGTH];
 float FIR100_Sample1_q[FIR_100_LENGTH];
@@ -418,12 +418,12 @@
     
     }
 }
+*/
 
 
 
 
-
-
+#define NUMSAMPLESAVERAGE 100
 #define FIR_1K_LENGTH 32
 float FIR1K_Sample1_i[FIR_1K_LENGTH];
 float FIR1K_Sample1_q[FIR_1K_LENGTH];
@@ -469,6 +469,13 @@
 #define DecimationFactor_10K 10
 void filter1K(float FIR1K_Sample1_i_input, float FIR1K_Sample1_q_input, float FIR1K_Sample2_i_input, float FIR1K_Sample2_q_input)
 {
+    static float Final_Average1_i=0;
+    static float Final_Average1_q=0;//when decimating sum up all 10 samples at a time have that be your output value
+    static float Final_Average2_i=0;
+    static float Final_Average2_q=0;
+    
+    
+    
     static uint8_t decimationCounter = 0;//used to keep track of how many samples you have currently decimated
     static float FIR1K_Sample1_i_DecimatedSum=0;
     static float FIR1K_Sample1_q_DecimatedSum=0;//when decimating sum up all 10 samples at a time have that be your output value
@@ -518,10 +525,26 @@
             FIR1K_Sample2_i_Output += FIR1K_Sample2_i[fir_index] * lp_1K_coeff[fir_counter];
             FIR1K_Sample2_q_Output += FIR1K_Sample2_q[fir_index] * lp_1K_coeff[fir_counter];
         }
+        Final_Average1_i+=FIR1K_Sample1_i_Output;
+        Final_Average1_q+=FIR1K_Sample1_q_Output;//when decimating sum up all 10 samples at a time have that be your output value
+        Final_Average2_i+=FIR1K_Sample2_i_Output;
+        Final_Average2_q+=FIR1K_Sample2_q_Output;
+        finalAverageCounter++;
+        if (finalAverageCounter>=NUMSAMPLESAVERAGE)
+        {
+            finalAverageCounter=0;
+            float mag1 = sqrt(Final_Average1_i*Final_Average1_i+Final_Average1_q*Final_Average1_q);
+            float mag2 = sqrt(Final_Average2_i*Final_Average2_i+Final_Average2_q*Final_Average2_q);
+            printf("V1: %f\tV2: %f\tRatio: %f\n\r",mag1,mag2,mag2/mag1);
+            Final_Average1_i=0;
+            Final_Average1_q=0;//when decimating sum up all 10 samples at a time have that be your output value
+            Final_Average2_i=0;
+            Final_Average2_q=0;
+        }
         //float mag1 = sqrt(FIR1K_Sample1_i_Output*FIR1K_Sample1_i_Output+FIR1K_Sample1_q_Output*FIR1K_Sample1_q_Output);
         //float mag2 = sqrt(FIR1K_Sample2_i_Output*FIR1K_Sample2_i_Output+FIR1K_Sample2_q_Output*FIR1K_Sample2_q_Output);
         //printf("V1: %f\tV2: %f\n\r",mag1,mag2);  
-        filter100(FIR1K_Sample1_i_Output, FIR1K_Sample1_q_Output, FIR1K_Sample2_i_Output, FIR1K_Sample2_q_Output);
+        //filter100(FIR1K_Sample1_i_Output, FIR1K_Sample1_q_Output, FIR1K_Sample2_i_Output, FIR1K_Sample2_q_Output);
     }
 }
 
--- a/main.cpp	Fri Nov 06 03:33:09 2015 +0000
+++ b/main.cpp	Fri Nov 06 19:28:49 2015 +0000
@@ -9,29 +9,10 @@
 DigitalOut led_green(LED_GREEN);
 DigitalOut led_blue(LED_BLUE);
 
-
-void output_data();
-
 Timer t1;
 using namespace std;
  
  
-// uint16_t out_val_pre[];
-// 
-// 
-// uint16_t phase_counter2 = 0;
-// #define pre_compute_length 2000
-// 
-// void ISR_repeat() {
-//  WaveOut.write_u16(out_val_pre[phase_counter2]);  //creates a wave that bounces between 0 & 3.3 V
-// 
-//  phase_counter2++;
-//  if (phase_counter2 >= pre_compute_length) phase_counter2 = 0;
-//} //ISR_repeat
-//
-//Ticker timer0; 
-// 
- 
  
  
 #define PDB_DACINTC0_TOE 0x01 // 0x01 -> PDB DAC interal trigger enabled
@@ -43,90 +24,20 @@
 {
     SIM_SCGC2 |= SIM_SCGC2_DAC0_MASK;  // turn on clock to the DAC
     SIM_SCGC6 |= SIM_SCGC6_DAC0_MASK;  // turn on clock to the DAC
-    //DAC0_C0 = 0;
     DAC0_C0 |= DAC_C0_DACEN_MASK ;   // enable the DAC; must do before any of the following
-    //DAC0_C0 |= DAC_C0_REFSEL(0)
-    //DAC0_C0 &= 0x9f;
-    //DAC0_C0 |= DAC_C0_DACRFS_MASK; // 3.3V VDDA is DACREF_2  
-    //DAC0_C2 =9;
-    //DAC0_C2 |= 0x9;
-    //DAC0_C2 |= DAC_C2_DACBFUP(9);
-    DAC0_C2 =9;
-    //DAC0_C2 |= DAC_C2_DACBFUP(9); // resets to 15 but setting it anyway...
-    DAC0_C1 = 1;
-    //DAC0_C1 |= (0x01); // 0x01 enables the DAC buffer! See note above
-    
-    // prefill the DAC buffer with first 16 values from the lut
-    
+    DAC0_C2 =9;//cycle through the first 10 values in the buffer
+    DAC0_C1 = 1;//enable the dac buffer
     p1 = DAC0_DAT0;
-    for (int i = 0; i < 16; i++) 
+    for (int i = 0; i < 16; i++)//fill the buffer 
     {
         *p1++ = (uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * i) * 460.0 + 2870.0); // 3351.0
         //printf("Pointer: %d\tValue: %d\n\r", (uint32_t)p1,(int) (cos(3.14159265359 * 2 * 10000 * .00001 * i) * 800.0 + 3103.0));
     } 
-    printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT0L,DAC0_DAT0H,DAC0_DAT0L|DAC0_DAT0H<<8);
-    printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT1L,DAC0_DAT1H,DAC0_DAT1L|DAC0_DAT1H<<8);
-    printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT2L,DAC0_DAT2H,DAC0_DAT2L|DAC0_DAT2H<<8);
-    printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT3L,DAC0_DAT3H,DAC0_DAT3L|DAC0_DAT3H<<8);
-    printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT4L,DAC0_DAT4H,DAC0_DAT4L|DAC0_DAT4H<<8);
-    printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT5L,DAC0_DAT5H,DAC0_DAT5L|DAC0_DAT5H<<8);
-    printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT6L,DAC0_DAT6H,DAC0_DAT6L|DAC0_DAT6H<<8);
-    printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT7L,DAC0_DAT7H,DAC0_DAT7L|DAC0_DAT7H<<8);
-    printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT8L,DAC0_DAT8H,DAC0_DAT8L|DAC0_DAT8H<<8);
-    printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT9L,DAC0_DAT9H,DAC0_DAT9L|DAC0_DAT9H<<8);
-    //printf ("data High: %d\n\r",DAC0_DAT0H);
-    
-    //printf ("data Low: %d\n\r",DAC0_DAT1L);
-    //printf ("data High: %d\n\r",DAC0_DAT1H);
-    /*
-    p1 = DAC0_DAT0;
-    for (int i = 0; i < 16; i++) 
-    {
-        *p1++;
-        printf("Pointer: %d\n\r", (uint32_t)*p1);
-    } 
-    
-    DAC0_DAT0L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 0) * 4965.0 + 49650.0))&0xFF);
-    DAC0_DAT1L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 1) * 4965.0 + 49650.0))&0xFF);
-    DAC0_DAT2L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 2) * 4965.0 + 49650.0))&0xFF);
-    DAC0_DAT3L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 3) * 4965.0 + 49650.0))&0xFF);
-    DAC0_DAT4L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 4) * 4965.0 + 49650.0))&0xFF);
-    DAC0_DAT5L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 5) * 4965.0 + 49650.0))&0xFF);
-    DAC0_DAT6L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 6) * 4965.0 + 49650.0))&0xFF);
-    DAC0_DAT7L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 7) * 4965.0 + 49650.0))&0xFF);
-    DAC0_DAT8L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 8) * 4965.0 + 49650.0))&0xFF);
-    DAC0_DAT9L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 9) * 4965.0 + 49650.0))&0xFF);
-    
-    DAC0_DAT0H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 0) * 4965.0 + 49650.0))>>8)&0x0F);
-    DAC0_DAT1H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 1) * 4965.0 + 49650.0))>>8)&0x0F);
-    DAC0_DAT2H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 2) * 4965.0 + 49650.0))>>8)&0x0F);
-    DAC0_DAT3H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 3) * 4965.0 + 49650.0))>>8)&0x0F);
-    DAC0_DAT4H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 4) * 4965.0 + 49650.0))>>8)&0x0F);
-    DAC0_DAT5H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 5) * 4965.0 + 49650.0))>>8)&0x0F);
-    DAC0_DAT6H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 6) * 4965.0 + 49650.0))>>8)&0x0F);
-    DAC0_DAT7H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 7) * 4965.0 + 49650.0))>>8)&0x0F);
-    DAC0_DAT8H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 8) * 4965.0 + 49650.0))>>8)&0x0F);
-    DAC0_DAT9H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 9) * 4965.0 + 49650.0))>>8)&0x0F);
-    */
-    /*
-    DAC0_SR = 0x00;                           
-    SIM_SCGC2 |= SIM_SCGC2_DAC0_MASK; 
-    SIM_SCGC6 |= SIM_SCGC2_DAC0_MASK;
-    DAC0_C0 = 0;
-    //DAC0_C0 |= DAC_C0_DACTRGSEL_MASK;
-    DAC0_C0 |= DAC_C0_DACEN_MASK ;             //The DAC system is enabled.
-    
-    DAC0_C1 = 0;
-    //DAC0_C0 |= DAC_C0_DACTRGSEL_MASK;
-    DAC0_C0 |= 0x80;//DAC_C0_DMAEN_MASK ;
-    DAC0_DAT0L = 0xFF;
-    DAC0_DAT0H = 0x00;
-    */
+
 }
  
 int main()
 {
-    //DAC0_C2 =0;
     
     led_blue = 1;
     led_green = 1;
@@ -145,12 +56,7 @@
 //    }
 //    printf("FINAL TIME: %f\n\r",t1.read());
    
-    
-    
-//    pc.baud(230400);
-    pc.printf("Starting...\r\n");
-
-    
+    pc.printf("Starting...\r\n");    
     for(int i = 0; i < 86; i++) 
     {
         if(NVIC_GetPriority((IRQn_Type) i) == 0) NVIC_SetPriority((IRQn_Type) i, 2);
@@ -170,14 +76,8 @@
     NVIC_SetPriority(ENET_Receive_IRQn,1);
     NVIC_SetPriority(ENET_Error_IRQn,1);
     
-    //quad_init(); // initialize FTM2 quadrature decoder
-    //quad_invert(); // invert the direction of counting
     adc_init(); // initialize ADCs (always initialize adc before dma)
     setUpDac();
-  //  DAC0_C1 |= 0x80;
-  //  DAC0_DAT0L = 0xFF;
-  //  DAC0_DAT0H = 0xFF;
-  //  printf("Dac should be 3.3V");
     dma_init(); // initializes DMAs
     pdb_init(); // initialize PDB0 as the timer for ADCs and DMA2 
     
@@ -192,26 +92,16 @@
     pause_ms(500); 
     led_green = 1;
     pdb_start();
-
-    //timer0.attach_us(&ISR_repeat, 10);
     int startAddress = (int)&sample_array0[0];
-    //timer0.attach_us(&ISR_repeat, 100);
     int destinationIndex = (DMA_TCD0_DADDR-startAddress)/2;
     int currentIndex;
-    /*
-    printf("Dac Control0 Register: %X\n\r",DAC0_C0);
-    printf("Dac Control2 Register: %X\n\r",DAC0_C2);
-    printf("Dac Control1 Register: %X\n\r",DAC0_C1);
-    printf("Dac Control2 Register: %X\n\r\n\r",DAC0_C2);
-    //PDB0_DACINT0 = 0x257;
-    printf("Dac intc Register: %X\n\r",PDB0_DACINTC0);
-    printf("Dac int Register: %X\n\r",PDB0_DACINT0);
-    */
     while (1)
     {
         
     
-        destinationIndex = (DMA_TCD0_DADDR-startAddress)/2;
+        destinationIndex = (DMA_TCD0_DADDR-startAddress)/2;//-1;
+        //if (destinationIndex<0)
+        //    destinationIndex = 1999;
         while (currentIndex!=destinationIndex)
         {
             filter100K(sample_array0[currentIndex], sample_array1[currentIndex]);