|
1 |
| -bvcbcv |
| 1 | + |
| 2 | +// Unit test program for analogReadFast |
| 3 | +// http://www.avdweb.nl/arduino/libraries/fast-10-bit-adc.html |
| 4 | + |
| 5 | +#include "avdweb_AnalogReadFast.h" |
| 6 | +#include <Albert.h> |
| 7 | +#include <RunningAverage.h> // https://github.com/RobTillaart/Arduino/tree/master/libraries/RunningAverage |
| 8 | +#include <StopWatch.h> |
| 9 | +#include <Streaming.h> |
| 10 | + |
| 11 | +const byte adcPin = A2; // 0.1V (10 Ohm / 330 Ohm to 3.3V) |
| 12 | +const bool doPrintStandardDeviation = 1; |
| 13 | +const bool doprintExecutionTime = 0; |
| 14 | +const int testsamples = 100; // max ~100 for AVR, max ~250 for SAMD21 |
| 15 | +const int ravgSamples = 16; // running average filter |
| 16 | +const int ExcelY1corr = 10; // place graphs above each other in Excel |
| 17 | +const int ExcelY2corr = 15; |
| 18 | +const int ExcelY3corr = 30; |
| 19 | +const int ExcelY4corr = 35; |
| 20 | +const int ExcelY5corr = 53; |
| 21 | +const int ExcelY6corr = 55; |
| 22 | +const bool doExcelYcorr = 1; // correction for Excel graph |
| 23 | + |
| 24 | +// compile time calculations |
| 25 | +const int adcValue = -120; // analogRead value for 0.1V |
| 26 | +const int O1 = doExcelYcorr * (adcValue + ExcelY1corr); |
| 27 | +const int O2 = doExcelYcorr * (adcValue + ExcelY2corr); |
| 28 | +const int O3 = doExcelYcorr * (adcValue + ExcelY3corr); |
| 29 | +const int O4 = doExcelYcorr * (adcValue + ExcelY4corr); |
| 30 | +const int O5 = doExcelYcorr * (adcValue + ExcelY5corr); |
| 31 | +const int O6 = doExcelYcorr * (adcValue + ExcelY6corr); |
| 32 | + |
| 33 | +Stopwatch stopwatch(micros); |
| 34 | +RunningAverage slow10test(testsamples), fast10test(testsamples); // for the test values |
| 35 | +#if defined(__arm__) |
| 36 | +RunningAverage slow12ravg(ravgSamples), fast12ravg(ravgSamples); // runningAverage filter |
| 37 | +RunningAverage slow12test(testsamples), fast12test(testsamples); // for the test values |
| 38 | +RunningAverage slow12ravgTest(testsamples), fast12ravgTest(testsamples); // for the test values |
| 39 | +#endif |
| 40 | + |
| 41 | +void testAnalogRead() |
| 42 | +{ |
| 43 | + for (int i = 0; i < testsamples; i++) |
| 44 | + slow10test.addValue(analogRead(adcPin)); |
| 45 | + for (int i = 0; i < testsamples; i++) |
| 46 | + fast10test.addValue(analogReadFast(adcPin)); |
| 47 | +#if defined(__arm__) |
| 48 | + analogReadResolution(12); |
| 49 | + for (int i = 0; i < testsamples; i++) |
| 50 | + slow12test.addValue(analogRead(adcPin)); |
| 51 | + for (int i = 0; i < testsamples; i++) |
| 52 | + fast12test.addValue(analogReadFast(adcPin)); |
| 53 | + analogReadResolution(10); |
| 54 | +#endif |
| 55 | +} |
| 56 | + |
| 57 | +void testAnalogReadRavg() |
| 58 | +{ |
| 59 | +#if defined(__arm__) |
| 60 | + analogReadResolution(12); |
| 61 | + for (int i = 0; i < testsamples; i++) { |
| 62 | + slow12ravg.addValue(analogRead(adcPin)); // calc runningAverage |
| 63 | + slow12ravgTest.addValue(slow12ravg.getAverage()); // test analogRead with runningAverage |
| 64 | + } |
| 65 | + for (int i = 0; i < testsamples; i++) { |
| 66 | + fast12ravg.addValue(analogReadFast(adcPin)); // calc runningAverage |
| 67 | + fast12ravgTest.addValue(fast12ravg.getAverage()); // test analogReadFast with runningAverage |
| 68 | + } |
| 69 | + analogReadResolution(10); |
| 70 | +#endif |
| 71 | +} |
| 72 | + |
| 73 | +void printAll() |
| 74 | +{ |
| 75 | + const int printFromSample = 3; |
| 76 | + Serial << "slow10 fast10 slow12 fast12 slow12ravg fast12ravg\n"; |
| 77 | + for (int i = printFromSample; i < testsamples; i++) { |
| 78 | + Serial << 4 * (int)slow10test.getElement(i) + O1, 4 * (int)fast10test.getElement(i) + O2, ""; |
| 79 | +#if defined(__arm__) |
| 80 | + Serial << (int)slow12test.getElement(i) + O3, (int)fast12test.getElement(i) + O4, |
| 81 | + (int)slow12ravgTest.getElement(i) + O5, (int)fast12ravgTest.getElement(i) + O6; |
| 82 | +#endif |
| 83 | + Serial << endl; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void printExecutionTime() |
| 88 | +{ |
| 89 | + stopwatch.start(); // default is analogReadResolution(10) |
| 90 | + analogRead(adcPin); // 425us on SAMD21 |
| 91 | + stopwatch.stop(); |
| 92 | + Serial << "\nslow10", stopwatch.interval, "us"; |
| 93 | + |
| 94 | + stopwatch.start(); |
| 95 | + analogReadFast(adcPin); // 23us on SAMD21 |
| 96 | + stopwatch.stop(); |
| 97 | + Serial << "\nfast10", stopwatch.interval, "us"; |
| 98 | + |
| 99 | +#if defined(__arm__) |
| 100 | + analogReadResolution(12); |
| 101 | + |
| 102 | + stopwatch.start(); |
| 103 | + analogRead(adcPin); // 425us on SAMD21 |
| 104 | + stopwatch.stop(); |
| 105 | + Serial << "\nslow12", stopwatch.interval, "us"; |
| 106 | + |
| 107 | + stopwatch.start(); |
| 108 | + analogReadFast(adcPin); // 24us on SAMD21 |
| 109 | + stopwatch.stop(); |
| 110 | + Serial << "\nfast12", stopwatch.interval, "us"; |
| 111 | + |
| 112 | + analogReadResolution(10); |
| 113 | +#endif |
| 114 | +} |
| 115 | + |
| 116 | +void printStandardDeviation() |
| 117 | +{ |
| 118 | + Serial << "\nslow10 SD", slow10test.getStandardDeviation(); |
| 119 | + Serial << "\nfast10 SD", fast10test.getStandardDeviation(); |
| 120 | +#if defined(__arm__) |
| 121 | + Serial << "\nslow12 SD", slow12test.getStandardDeviation(); |
| 122 | + Serial << "\nfast12 SD", fast12test.getStandardDeviation(); |
| 123 | + Serial << "\nslow12ravg SD", slow12ravgTest.getStandardDeviation(); |
| 124 | + Serial << "\nfast12ravg SD", fast12ravgTest.getStandardDeviation(); |
| 125 | +#endif |
| 126 | +} |
| 127 | + |
| 128 | +void setup(void) |
| 129 | +{ |
| 130 | + Serial.begin(9600); |
| 131 | + while (!Serial) |
| 132 | + ; |
| 133 | + analogRead(adcPin); // first analogRead is wrong |
| 134 | + testAnalogRead(); // without running average |
| 135 | + testAnalogReadRavg(); // with running average |
| 136 | + printAll(); |
| 137 | + if (doprintExecutionTime) |
| 138 | + printExecutionTime(); |
| 139 | + if (doPrintStandardDeviation) |
| 140 | + printStandardDeviation(); |
| 141 | +} |
| 142 | + |
| 143 | +void loop(void) |
| 144 | +{ |
| 145 | +} |
0 commit comments