Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem STATIC_COLORS WIZnetInterface mbed
Fork of WIZwiki-W7500_ADC_Sampling by
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "SDFileSystem.h" 00004 #include <stdio.h> 00005 #include <string.h> 00006 00007 // https://developer.mbed.org/users/chris/notebook/Getting-best-ADC-performance/ 00008 // https://developer.mbed.org/users/chris/code/ADCPerformanceMeter/ 00009 00010 #if defined(TARGET_WIZwiki_W7500) 00011 Serial uart(USBTX, USBRX); 00012 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500 00013 #include "static_colors.h" 00014 // LED RED : server listning status 00015 // LED GREEN : socket connecting status Ok 00016 // LED BLUE : socket connecting status Busy 00017 #endif 00018 00019 #define HTTPD_MAX_FNAME_LENGTH 127 00020 #define HTTPD_MAX_DNAME_LENGTH 127 00021 00022 char fileName[HTTPD_MAX_FNAME_LENGTH+1]; 00023 char dirName[HTTPD_MAX_DNAME_LENGTH+1]; 00024 char *uristr; 00025 char *eou; 00026 char *qrystr; 00027 00028 FILE *fp; 00029 int rdCnt; 00030 00031 // Initialize a pins to perform analog input and digital output fucntions 00032 AnalogIn ain0(A0); 00033 00034 #define NUM_SAMPLES 500000 // size of sample series 00035 #define SAMPLE_BLOCKS 5 00036 00037 Timer t; 00038 00039 Ticker ledTick; 00040 00041 char *pch; 00042 char ext[5]; 00043 00044 int pos_ext; 00045 int extLen; 00046 00047 //--------------------------- 00048 int samples = 0; 00049 int num_4 = 0; 00050 int num_8 = 0; 00051 int num_16 = 0; 00052 int num_32 = 0; 00053 int num_64 = 0; 00054 int num_128 = 0; 00055 int num_256 = 0; 00056 int num_512 = 0; 00057 int num_1024 = 0; 00058 int num_spike = 0; 00059 00060 float AVERAGE = 0.5; 00061 float AVERAGE_MOY = 0.5; 00062 float average_min = 0.0; 00063 float average_max = 3.3; 00064 float average_error = 3.0303; // Par défaut : 3.0303% sur la valeur centrale 1.65V 00065 00066 #define __AVERAGE_AUTO__ 1 00067 00068 void PerformanceMeter(void) 00069 { 00070 float r = 0.0; 00071 //--------------------------------- 00072 // Take the average over 500,000 samples 00073 // This is because the bias to 1.65v has a tolerance 00074 // adjust the analog input to 1.65V, if NO AUTO MODE 00075 00076 uart.printf("Taking an average over %d samples\r\n",NUM_SAMPLES); 00077 00078 if( __AVERAGE_AUTO__ ) 00079 { 00080 COLOR(_BLUE_); 00081 samples = 0; 00082 average_error = 5.0; // 5% initiale 00083 uart.printf("AVERAGE AUTO : error %f%%\r\n", average_error); 00084 00085 while(samples < NUM_SAMPLES) 00086 { 00087 r = ain0.read(); 00088 00089 AVERAGE_MOY += r; 00090 samples++; 00091 } 00092 00093 AVERAGE_MOY /= NUM_SAMPLES; 00094 average_min = AVERAGE_MOY - (AVERAGE_MOY * average_error / 100.0); 00095 average_max = AVERAGE_MOY + (AVERAGE_MOY * average_error / 100.0); 00096 uart.printf("Average Moyenne (for 5%%) = %f , Min = %f , Max = %f\r\n",3.3 * AVERAGE, 3.3 * average_min, 3.3 * average_max); 00097 samples = 0; 00098 00099 while(samples < NUM_SAMPLES) 00100 { 00101 r = ain0.read(); 00102 00103 if((r > average_min) && (r < average_max)) 00104 { 00105 AVERAGE += r; 00106 samples++; 00107 } 00108 } 00109 00110 AVERAGE /= NUM_SAMPLES; 00111 average_min = AVERAGE - (AVERAGE * average_error / 100.0); 00112 average_max = AVERAGE + (AVERAGE * average_error / 100.0); 00113 uart.printf("Average (for 5%%) = %f , Min = %f , Max = %f\r\n",3.3 * AVERAGE, 3.3 * average_min, 3.3 * average_max); 00114 // étalonage à 1% -------------------- 00115 average_error = 1.0; // 1% 00116 COLOR(_YELLOW_); 00117 samples = 0; 00118 00119 while(samples < NUM_SAMPLES) 00120 { 00121 r = ain0.read(); 00122 00123 if((r > average_min) && (r < average_max)) 00124 { 00125 AVERAGE += r; 00126 samples++; 00127 } 00128 } 00129 00130 AVERAGE /= NUM_SAMPLES; 00131 average_min = AVERAGE - (AVERAGE * average_error / 100.0); 00132 average_max = AVERAGE + (AVERAGE * average_error / 100.0); 00133 uart.printf("Average (for 1%%) = %f , Min = %f , Max = %f\r\n",3.3 * AVERAGE, 3.3 * average_min, 3.3 * average_max); 00134 } 00135 else 00136 { 00137 COLOR(_PURPLE_); 00138 average_min = 1.65 - (1.65 * average_error / 100.0); // 0.45 --> -3,0303% of 1.65V --> 1.485V 00139 average_max = 1.65 + (1.65 * average_error / 100.0); // 0.55 --> +3,0303% of 1.65V --> 1.815V 00140 samples = 0; 00141 00142 while(samples < NUM_SAMPLES) 00143 { 00144 r = ain0.read(); 00145 00146 if((r > average_min) && (r < average_max)) // 0.7425V à 0.9075 00147 { 00148 AVERAGE += r; 00149 samples++; 00150 } 00151 } 00152 00153 AVERAGE /= NUM_SAMPLES; 00154 uart.printf("Average = %f , Min = %f , Max = %f\r\n",3.3 * AVERAGE, 3.3 * average_min, 3.3 * average_max); 00155 } 00156 00157 samples = 0; 00158 00159 // Now start sampling series of 500,000 00160 // acculumating the errors seen in each range 00161 uart.printf("Profiling %d samples\r\n",SAMPLE_BLOCKS*NUM_SAMPLES); 00162 float a; 00163 00164 for(int j=0; j < SAMPLE_BLOCKS ; j++) 00165 { 00166 t.reset(); 00167 t.start(); 00168 COLOR(_WHITE_); 00169 //uart.printf("%d SAMPLE BLOCKS\r\n",j); 00170 00171 for(int i = 0; i < NUM_SAMPLES ; i++) 00172 { 00173 a = ain0.read(); 00174 00175 if(a == 1.0) { 00176 num_spike++; // > 2048 lsb 00177 } else if (a > (AVERAGE + 0.2500)) { 00178 num_1024++; // > 1024 lsb 00179 } else if (a > (AVERAGE + 0.0625)) { 00180 num_512++; // > 512 lsb 00181 } else if (a > (AVERAGE + 0.0312)) { 00182 num_256++; // > 256 lsb 00183 } else if (a > (AVERAGE + 0.0312)) { 00184 num_128++; // > 128 lsb 00185 } else if (a > (AVERAGE + 0.0156)) { 00186 num_64++; // > 64 lsb 00187 } else if (a > (AVERAGE + 0.0078)) { 00188 num_32++; // > 32 lsb 00189 } else if (a > (AVERAGE + 0.0039)) { 00190 num_16++; // > 16 lsb 00191 } else if (a > (AVERAGE + 0.0019)) { 00192 num_8++; // > 8 lsb 00193 } else if (a > (AVERAGE + 0.0009)) { 00194 num_4++; // > 8 lsb 00195 } 00196 00197 samples++; 00198 } 00199 t.stop(); 00200 COLOR(_BLACK_); 00201 if(j==0) 00202 { 00203 uart.printf("Samples\t"); 00204 uart.printf("4\t"); 00205 uart.printf("8\t"); 00206 uart.printf("16\t"); 00207 uart.printf("32\t"); 00208 uart.printf("64\t"); 00209 uart.printf("128\t"); 00210 uart.printf("256\t"); 00211 uart.printf("512\t"); 00212 uart.printf("1024\t"); 00213 uart.printf("Spikes\t"); 00214 uart.printf("Time\r\n"); 00215 } 00216 00217 // Every 500,000 print the results 00218 uart.printf("%d\t",samples); 00219 uart.printf("%d\t",num_4); 00220 uart.printf("%d\t",num_8); 00221 uart.printf("%d\t",num_16); 00222 uart.printf("%d\t",num_32); 00223 uart.printf("%d\t",num_64); 00224 uart.printf("%d\t",num_128); 00225 uart.printf("%d\t",num_256); 00226 uart.printf("%d\t",num_512); 00227 uart.printf("%d\t",num_1024); 00228 uart.printf("%d\t",num_spike); 00229 uart.printf("%fs\r\n",t.read()); 00230 } 00231 00232 uart.printf("==== Test Complete ====\r\n"); 00233 COLOR(_GREEN_); 00234 //------------------------------- 00235 // TEST continu 00236 uart.printf("==== Test Continu avec %d Samples ====\r\n", NUM_SAMPLES / 10); 00237 samples = 0; 00238 00239 while(1) 00240 { 00241 a = ain0.read(); 00242 samples++; 00243 00244 if(a == 1.0) { 00245 num_spike++; // > 2048 lsb 00246 } else if (a > (AVERAGE + 0.2500)) { 00247 num_1024++; // > 1024 lsb 00248 } else if (a > (AVERAGE + 0.0625)) { 00249 num_512++; // > 512 lsb 00250 } else if (a > (AVERAGE + 0.0312)) { 00251 num_256++; // > 256 lsb 00252 } else if (a > (AVERAGE + 0.0312)) { 00253 num_128++; // > 128 lsb 00254 } else if (a > (AVERAGE + 0.0156)) { 00255 num_64++; // > 64 lsb 00256 } else if (a > (AVERAGE + 0.0078)) { 00257 num_32++; // > 32 lsb 00258 } else if (a > (AVERAGE + 0.0039)) { 00259 num_16++; // > 16 lsb 00260 } else if (a > (AVERAGE + 0.0019)) { 00261 num_8++; // > 8 lsb 00262 } else if (a > (AVERAGE + 0.0009)) { 00263 num_4++; // > 8 lsb 00264 } 00265 00266 if(samples == NUM_SAMPLES / 10) 00267 { 00268 uart.printf("Samples\t"); 00269 uart.printf("4\t"); 00270 uart.printf("8\t"); 00271 uart.printf("16\t"); 00272 uart.printf("32\t"); 00273 uart.printf("64\t"); 00274 uart.printf("128\t"); 00275 uart.printf("256\t"); 00276 uart.printf("512\t"); 00277 uart.printf("1024\t"); 00278 uart.printf("Spikes\r\n"); 00279 // Every 500,000 print the results 00280 uart.printf("%d\t",samples); 00281 uart.printf("%d\t",num_4); 00282 uart.printf("%d\t",num_8); 00283 uart.printf("%d\t",num_16); 00284 uart.printf("%d\t",num_32); 00285 uart.printf("%d\t",num_64); 00286 uart.printf("%d\t",num_128); 00287 uart.printf("%d\t",num_256); 00288 uart.printf("%d\t",num_512); 00289 uart.printf("%d\t",num_1024); 00290 uart.printf("%d\r\n\r\n",num_spike); 00291 num_4 = 0; 00292 num_8 = 0; 00293 num_16 = 0; 00294 num_32 = 0; 00295 num_64 = 0; 00296 num_128 = 0; 00297 num_256 = 0; 00298 num_512 = 0; 00299 num_1024 = 0; 00300 num_spike = 0; 00301 samples = 0; 00302 } 00303 } 00304 } 00305 00306 //-------------------------------------------- 00307 00308 int main(void) 00309 { 00310 // initialisation des variables 00311 00312 00313 //-------------- 00314 COLOR(_BLACK_); 00315 // Serial Interface eth; 00316 // Serial port configuration (valeurs par defaut) : 9600 baud, 8-bit data, no parity, stop bit 00317 uart.baud(9600); 00318 uart.format(8, SerialBase::None, 1); 00319 COLOR(_RED_); 00320 uart.printf("WIZwiki-W7500 - mBED ( Compiled at : %s and %s )\r\n", __DATE__ , __TIME__); 00321 uart.printf("Initializing\n\r"); 00322 wait(1.0); 00323 00324 PerformanceMeter(); 00325 }
Generated on Wed Jul 13 2022 07:20:26 by
1.7.2
