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.
main.cpp
00001 #include "mbed.h" 00002 00003 #define NUM_SAMPLES 500000 // size of sample series 00004 #define SAMPLE_BLOCKS 5 00005 00006 Timer t; 00007 00008 Serial pc(USBTX,USBRX); 00009 00010 AnalogIn ain(p17); 00011 00012 // initialise error counters 00013 int num_4; 00014 int num_8; 00015 int num_16; 00016 int num_32; 00017 int num_64; 00018 int num_128; 00019 int num_256; 00020 int num_512; 00021 int num_1024; 00022 int num_spike; 00023 00024 int main() { 00025 00026 int num_samples = 0; 00027 00028 num_4 = 0; 00029 num_8 = 0; 00030 num_16 = 0; 00031 num_32 = 0; 00032 num_64 = 0; 00033 num_128 = 0; 00034 num_256 = 0; 00035 num_512 = 0; 00036 num_1024 = 0; 00037 num_spike = 0; 00038 00039 00040 // Take the average over 500,000 samples 00041 // This is because the bias to 1.65v has a tolerance 00042 float AVERAGE = 0.5; 00043 00044 pc.printf("Taking an average over %d samples\n",NUM_SAMPLES); 00045 while (num_samples < NUM_SAMPLES) { 00046 float r = ain.read(); 00047 00048 if ((r > 0.45) && (r < 0.55)) { 00049 AVERAGE += r; 00050 num_samples++; 00051 } 00052 } 00053 00054 AVERAGE /= NUM_SAMPLES; 00055 00056 num_samples = 0; 00057 pc.printf("Average = %f\n",AVERAGE); 00058 00059 00060 // Now start sampling series of 500,000 00061 // acculumating the errors seen in each range 00062 pc.printf("Profiling %d samples\n",SAMPLE_BLOCKS*NUM_SAMPLES); 00063 00064 00065 for (int j=0; j < SAMPLE_BLOCKS ; j++) { 00066 00067 t.reset(); 00068 t.start(); 00069 00070 for (int i = 0; i < NUM_SAMPLES ; i++) { 00071 float a = ain.read(); 00072 00073 00074 if (a == 1.0) { 00075 num_spike++; // > 2048 lsb 00076 } else if (a > (AVERAGE + 0.2500)) { 00077 num_1024++; // > 1024 lsb 00078 } else if (a > (AVERAGE + 0.0625)) { 00079 num_512++; // > 512 lsb 00080 } else if (a > (AVERAGE + 0.0312)) { 00081 num_256++; // > 256 lsb 00082 } else if (a > (AVERAGE + 0.0312)) { 00083 num_128++; // > 128 lsb 00084 } else if (a > (AVERAGE + 0.0156)) { 00085 num_64++; // > 64 lsb 00086 } else if (a > (AVERAGE + 0.0078)) { 00087 num_32++; // > 32 lsb 00088 } else if (a > (AVERAGE + 0.0039)) { 00089 num_16++; // > 16 lsb 00090 } else if (a > (AVERAGE + 0.0019)) { 00091 num_8++; // > 8 lsb 00092 } else if (a > (AVERAGE + 0.0009)) { 00093 num_4++; // > 8 lsb 00094 } 00095 00096 num_samples++; 00097 } 00098 t.stop(); 00099 00100 if (j==0) { 00101 pc.printf("Samples\t"); 00102 pc.printf("4\t"); 00103 pc.printf("8\t"); 00104 pc.printf("16\t"); 00105 pc.printf("32\t"); 00106 pc.printf("64\t"); 00107 pc.printf("128\t"); 00108 pc.printf("256\t"); 00109 pc.printf("512\t"); 00110 pc.printf("1024\t"); 00111 pc.printf("Spikes\t"); 00112 pc.printf("Time\n"); 00113 } 00114 00115 00116 // Every 500,000 print the results 00117 pc.printf("%d\t",num_samples); 00118 pc.printf("%d\t",num_4); 00119 pc.printf("%d\t",num_8); 00120 pc.printf("%d\t",num_16); 00121 pc.printf("%d\t",num_32); 00122 pc.printf("%d\t",num_64); 00123 pc.printf("%d\t",num_128); 00124 pc.printf("%d\t",num_256); 00125 pc.printf("%d\t",num_512); 00126 pc.printf("%d\t",num_1024); 00127 pc.printf("%d\t",num_spike); 00128 pc.printf("%fs\n",t.read()); 00129 } 00130 00131 pc.printf("==== Test Complete ====\n"); 00132 }
Generated on Wed Jul 20 2022 23:39:05 by
1.7.2