AAAAAAAAAAAAAAAAAAA

Dependencies:   mbed FastPWM

Revision:
9:996abe9af847
Parent:
8:e68460a69e2e
Child:
10:7b8f697e075d
--- a/main.cpp	Tue Mar 07 11:24:55 2017 +0000
+++ b/main.cpp	Sat Mar 11 14:24:26 2017 +0000
@@ -115,13 +115,13 @@
 int sign;
 int range;
 
+void fout();
+
 void swap();
 int diff;
 
 int ymax;
 void adc_read();
-uint16_t y[40];
-uint16_t x[40];
 int yp;
 float yprint[256];
 
@@ -181,7 +181,7 @@
     ADC_ChannelConfTypeDef sConfig;
     sConfig.Channel = ADC_CHANNEL_14;  // PC_4
     sConfig.Rank = 1;
-    sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
+    sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
     sConfig.Offset = 0;
     HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
     HAL_ADC_Start_DMA(&AdcHandle, (uint32_t *)data, nelems);
@@ -207,42 +207,86 @@
         ddsstart; DDS.write(0x20C2);
         ddsstop; // инициализация dds
         four();
-        /*ctest();
+        ////////////////////////
+        printf("7\r\n");
+        five();
+        ///////////////////////        
+       /* ctest();
         if (ct == 1) {continue;}
          else {
             printf("7\r\n");
             five();};*/
         request();
+        t.reset();
+        t.start();
         rasputte();
         mux_group();
-        form = 1; freqdata = 1; amp = 0.1;sinus();
-        sq=0.0;
-       for (yp = 0; yp < 256; yp++) {printf("%.5g\r\n",yprint[yp]);};
-       memset(yprint,0,sizeof yprint);
+        t.stop(); 
+        //fout();
+        //////////////////////////
+        for (yp = 0; yp < 256; yp++) {printf("%.4f\r\n",yprint[yp]);};
+        /////////////////////////
+   // printf("1000r useconds %i\r\n", t.read_ms());
+          form = 1; freqdata = 1; amp = 0.1;sinus();sq=0.0;
     }
 
 }
 
+void fout()
+{
+    //printf("%.1g,%i\r\n", ampd[ampc], freqdata);
+    printf("# name: data\r\n");
+    printf("# type: scalar struct\r\n");
+    printf("# ndims: 2\r\n");
+    printf(" 1 1\r\n");
+    printf("# length: 4\r\n");
+    printf("# name: description\r\n");
+    printf("# type: sq_string\r\n");
+    printf("# elements: 1\r\n");
+    printf("# length:  20\r\n");
+    if (form == 1){printf("# sin %03i kHz %1.01f mA\r\n",freqdata,amp);}
+    else if (form == 2){printf("# sqr %03i kHz %1.01f mA\r\n",freqdata,amp);}
+    else {printf("# tri %03i kHz %1.01f mA\r\n",freqdata,amp);};
+    printf("\r\n");
+    printf("\r\n");
+    printf("# name: meas_volts\r\n");
+    printf("# type: matrix\r\n");
+    printf("# rows: 256\r\n");
+    printf("# columns: 1\r\n");
+    for (yp = 0; yp < 256; yp++) {printf("%.4g\r\n",yprint[yp]);};
+    memset(yprint,0,sizeof yprint);
+    printf("\r\n");
+    printf("# name: time\r\n");
+    printf("# type: scalar\r\n");
+    printf("    %04i ms\r\n",t.read_ms());
+    printf("\r\n");
+    printf("# name: n_elec\r\n");
+    printf("# type: scalar\r\n");
+    printf(" 16\r\n");
+    
+}
 
 void ctest()
 {
     ct = 0;
     int mg = 0;
     int mj = 0;
+    int aread=0;
     sign=1; PA=1; preamp();
     form = 1; freqdata = 50; amp = 1;
     sinus();
     for (mg = 0; mg < 10; mg++) {
         IE = mg; emux();
         for (mj = 0; mj < 15; mj+=2) {
-            II = mj; IG = mj+1; DI1 = mj; //DI2 = mj+1;
-            igmux(); imux(); DI1mux(); //DI2mux();
-            if ((analog_value.read_u16() > 2500) &&(test_button.read() == 1))
-            {printf("%i,%i\r\n",mg+1, mj+1); printf("%i,%i\r\n",mg+1, mj+2);ct = 1;}
+            II = mj; IG = mj+1; DI1 = mj; DI2 = mj+1;
+            igmux(); imux(); DI1mux(); DI2mux();
+            aread = (int) analog_value.read() * 4096;
+            if ((aread > 3000) &&(test_button.read() == 1))
+            {printf("%i,%i,%i\r\n",mg+1, mj+1,aread); printf("%i,%i\r\n",mg+1, mj+2);ct = 1;}
+        }
+    
     }
-    
-}
-mclkstop;
+    mclkstop;
 }
 
 
@@ -261,7 +305,7 @@
         fflush (stdout);
         scanf ("%2s",buffer);
     } while (strcmp (key5,buffer) != 0);
-    fflush (stdout);
+       fflush (stdout);
 }
 
 void amplitude ()
@@ -488,27 +532,32 @@
 
 void ranging()//определение коэффициента усиления и знака
 {
+    int xa = (int)5000/freqdata;
+    if (xa < 50){xa=50;}
+    else if (xa > 500){xa=500;};
+    int xaa = (int)xa/10;
+    uint16_t x[xa];
     int s_ok=0;
     //IG = 0; II = 1;DI1 = II;DI2=IG;igmux();imux();DI1mux();DI2mux();wait_ms(10);
     PA=1; preamp();
-    while(s_ok==0){
+    //while(s_ok==0){
     adc_init();
     dma_init();
     memset(x,0,sizeof x);
-    t.reset();
-    t.start();
-    adc_readn(x,40);
-    t.stop(); 
-    printf("1000r useconds %i\r\n", t.read_us());
-    sort(x,x + 40);
-    ymax = (x[38]);
-        if (ymax <= 0) {} 
-        else if (((ymax >= 2049)&&(ymax < 2050))) {range=1000;s_ok=1;} 
-        else if (((ymax >= 2050)&&(ymax < 2068))) {range=100;s_ok=1;} 
-        else if(((ymax >= 2068)&&(ymax < 2248))) {range=10;s_ok=1;} 
-        else {range=1;s_ok=1;};
-}
-PA = range; preamp();
+    adc_readn(x,xa);
+    printf("non_sorted\r\n");
+    for (s_ok = 0; s_ok < xa; s_ok++) {printf("%i\r\n",x[s_ok]);};
+    sort(x+xaa,x + xa);
+     printf("sorted\r\n");
+    for (s_ok = 0; s_ok < xa; s_ok++) {printf("%i\r\n",x[s_ok]);};
+    ymax = (int)(x[xa-1]-x[xaa]);
+        if (ymax < 2) {range=1000;s_ok=1;wait_us(50);} 
+        else if (((ymax >= 2)&&(ymax < 20))) {range=100;s_ok=1;wait_us(10);} 
+        else if(((ymax >= 20)&&(ymax < 205))) {range=10;s_ok=1;wait_us(10);} 
+        else {range=1;s_ok=1;wait_us(10);};
+//}
+    PA = range; preamp();
+    //printf("%i,%i,%i,%i\r\n",ymax,x[xa-(xaa+1)], x[0], range);
 memset(x,0,sizeof x);
 }
 
@@ -523,15 +572,17 @@
 
 void adc_read()//измерение и вывод со всеми коэффициентами
 {
-    
-    
+    int ya = (int)10000/freqdata;
+    float ym = 0;
+    uint16_t y[ya];
     adc_init();
     dma_init();
     memset(y,0,sizeof y);
-    adc_readn(y,40);
+    adc_readn(y,ya);
    // for (yp = 0; yp < 400; yp++) {printf("%i\r\n",y[yp]);};
-    sort(y,y + 40);
-    yprint[yp]=(float)(((y[39]-2048)*3.3*10)/(4096*range));yp++;
+    sort(y,y + ya);
+    ym = ((y[ya-2]+y[ya-3]+y[ya-4]+y[ya-5]+y[ya-6])-(y[1]+y[2]+y[3]+y[4]+y[5]))/10;
+    yprint[yp]=(((ym)*3.3*10)/(4096*range));yp++;
     memset(y,0,sizeof y);
 
 }