Mario Bambagini / Mbed 2 deprecated currentReader

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**********************************************************
00002  * CONFIGURATION: before starting, configure the reader
00003  **********************************************************/
00004 
00005 //define the number of channels to use:
00006 // -) 1 channel: p20
00007 // -) 2 channels: p19 and p20
00008 #define NCHANNELS 1
00009 //#define nChannels 1
00010 
00011 //number of samples to read before printing their average
00012 #define NSAMPLES 2500
00013 
00014 //number of samplng operation after an user trigger
00015 //(total samples: NSAMPLES*NSAMPLING)
00016 #define NSAMPLING 50
00017 
00018 //read NSAMPLES values when the pin p9 is high
00019 #define PIN_TRIGGERED 2
00020 
00021 //the sampling is run only when the user presses the character 's'
00022 //the operation lasts for NSAMPLING iterations
00023 #define USER_TRIGGERED 1
00024 
00025 //read all the time
00026 #define ENDLESS_READ 0
00027 
00028 #define ACQ_TYPE USER_TRIGGERED
00029 
00030 
00031 /**********************************************************
00032  * CONFIGURATION CHECK
00033  **********************************************************/
00034 #if !defined(NCHANNELS) || (NCHANNELS!=1 && NCHANNELS!=2)
00035 #error "The number of channels is wrong"
00036 #endif
00037 
00038 #if !defined(ACQ_TYPE) || (ACQ_TYPE!=ENDLESS_READ && \
00039          ACQ_TYPE!=PIN_TRIGGERED && ACQ_TYPE!=USER_TRIGGERED)
00040 #error "Acquisition type not detected or not valid"       
00041 #endif
00042 
00043 #if ACQ_TYPE==USER_TRIGGERED && !defined(NSAMPLING)
00044 #error "Define a number of samplings after the user trigger"
00045 #endif
00046 
00047 
00048 /**********************************************************
00049  * CODE
00050  **********************************************************/
00051  
00052 #include "mbed.h"
00053 
00054 AnalogIn ain_1(p20);
00055 #if NCHANNELS==2
00056 AnalogIn ain_2(p19);
00057 #endif
00058 
00059 #if ACQ_TYPE==PIN_TRIGGERED
00060 DigitalIn firer (p9);
00061 #endif
00062 
00063 #define V_max 3.3
00064 
00065 int main()
00066 {
00067     double vref_1 = 0.0;
00068 #if NCHANNELS==2
00069     double vref_2 = 0.0;
00070 #endif
00071     
00072     //acquiring v_ref
00073     for (int i=0; i<NSAMPLES; i++) {
00074         vref_1 += ain_1;
00075 #if NCHANNELS==2
00076         vref_2 += ain_2;
00077 #endif
00078     }
00079     vref_1 /= NSAMPLES;
00080     printf("Vref 1: %f\n\r", vref_1);
00081 #if NCHANNELS==2
00082     vref_2 /= NSAMPLES;
00083     printf("Vref 2: %f\n\r", vref_2);
00084 #endif
00085 
00086     //main loop
00087     while(true) {
00088         //start condition
00089 #if ACQ_TYPE==USER_TRIGGERED
00090         printf("press \'s\' to start\n\r");
00091         int c;
00092         do {
00093             c = getchar();
00094         } while (c!='s');
00095         for (int i=0; i<NSAMPLING; i++) {
00096 #elif ACQ_TYPE==PIN_TRIGGERED
00097         while(firer==0);
00098 #elif ACQ_TYPE==ENDLESS_READ
00099 #else
00100 //already signaled
00101 //#error "Acquisition type not detected"
00102 #endif
00103 
00104             //acquisition
00105             double sum_1 = 0.0;
00106 #if NCHANNELS==2
00107             double sum_2 = 0.0;
00108 #endif
00109             for (int samples = 0; samples<NSAMPLES; samples++) {
00110                 sum_1 += ain_1;
00111 #if NCHANNELS==2
00112                 sum_2 += ain_2;
00113 #endif
00114             }
00115             sum_1 /= NSAMPLES;
00116             sum_1 = (sum_1-vref_1)*V_max/0.185;
00117 #if NCHANNELS==2
00118             sum_2 /= NSAMPLES;
00119             sum_2 = (sum_2-vref_2)*V_max/0.185;
00120             printf("%f\n\r", sum_1+sum_2);
00121 #else
00122             printf("%f\n\r", sum_1);
00123 #endif
00124 
00125             //final condition
00126 #if ACQ_TYPE==USER_TRIGGERED
00127         }
00128 #elif ACQ_TYPE==PIN_TRIGGERED
00129 #elif ACQ_TYPE==ENDLESS_READ
00130 #else
00131 #endif
00132     }
00133 }