Tobias Wulf / Mbed 2 deprecated ADC_Test

Dependencies:   Terminal adc mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* ADC Test
00002  * 
00003  * Project:         DiscoTech
00004  * HDK/ SDK Eng:    Tobias Wulf
00005  * Date:            21.09.2013
00006  *
00007  */
00008  
00009 #include "mbed.h"
00010 #include "Terminal.h"
00011 
00012 #include "adc.h"
00013 
00014 
00015 #include "stdlib.h"
00016 
00017 #define SAMPLE_RATE 48000 //Hz
00018 #define ADC_BIAS 1.65 //Volts
00019 #define ULSB 3.3 / 4095
00020 #define RESOLUTION 256
00021 
00022 #define MASK 0x07 // for shift register
00023 
00024 #define NUMBER_OF_SAMPLES 1024
00025 
00026 /* declair your functions
00027  * isr routine doesn't have to be declaired
00028  */
00029 void mainInit();
00030 
00031 
00032 ADC adc(SAMPLE_RATE, 1);
00033 
00034 /* Serial communication via usb uplink to pc
00035  * to display what ever you want in the terminal
00036  * or to send what ever you want from the terminal
00037  */
00038 Terminal usbPC(USBTX, USBRX);
00039 
00040 
00041 /* global variables to work in the adc interrupt
00042  * service routine to get the data from interrupt
00043  */
00044 
00045 double newSample;
00046 
00047 bool save;
00048 
00049 double adcMin;
00050 double adcMax;
00051 double adcAverage;
00052 double adcSavings[NUMBER_OF_SAMPLES];
00053 
00054 int main() {
00055         
00056     usbPC.printf("...start program\n");
00057     wait(0.2);
00058 
00059     mainInit();
00060     usbPC.printf("...init completed\n");
00061     wait(0.2);
00062 
00063 
00064     while(1) {
00065         
00066         if(save == false) {
00067         
00068            usbPC.locate(0,0);
00069            usbPC.cls();
00070            usbPC.printf("Max = %1.4f\tMin = %1.4f\nAverage = %1.4f\tact. Sample = %1.4f\n", adcMax, adcMin, adcAverage, newSample);
00071            
00072            for(int i=0; i<NUMBER_OF_SAMPLES; i++)
00073                 usbPC.printf("%1.4f\n",adcSavings[i]);
00074         }
00075         
00076         if(save == false) break;
00077         
00078     }        
00079 }/* end main */
00080 
00081 
00082 
00083 
00084 /* interrupt service routine from adc object
00085  * here should be done the data processing
00086  * variable and output updates
00087  */
00088 void getADC(int chan, uint32_t value) {
00089     //static double adcMax_temp;
00090     //static double adcMin_temp; //volts max value value
00091     static double adcTotal = 0.0;
00092     static double adcSample;
00093     static long cycleCounter = 0;
00094 
00095     adcSample = (double) adc.read(p15);
00096     adcSample = adcSample * ULSB; // - ADC_BIAS;
00097   
00098     adcTotal += adcSample;
00099     cycleCounter++;
00100     
00101     if (adcSample < adcMin)
00102                 adcMin = adcSample;
00103                 
00104     if (adcSample > adcMax)
00105                 adcMax = adcSample;
00106                 
00107     
00108     if(cycleCounter < NUMBER_OF_SAMPLES) {
00109        adcSavings[cycleCounter] = adcSample;
00110        if((cycleCounter == NUMBER_OF_SAMPLES-1) && save) {
00111             save = false;
00112             
00113             newSample = adcSample; 
00114             //adcMax = adcMax_temp;
00115             adcAverage = (double)(adcTotal / cycleCounter);
00116             //adcMin = adcMin_temp;
00117             adcMax = 0.0;
00118             adcMin = 3.3;
00119             adcTotal = 0.0;
00120             cycleCounter = 0;
00121        }
00122     }
00123 }        
00124 
00125 /* function to initialize the main program, objects and 
00126  * implement all variables
00127  */
00128 void mainInit() {
00129 
00130     save = true;
00131 
00132     adcMin = 3.3;
00133     adcMax = 0.0;
00134     adcAverage = 0.0;
00135     
00136     for (int i = 0; i < NUMBER_OF_SAMPLES; i++)
00137         adcSavings[i] = 0.0f;      
00138     
00139 
00140 
00141     usbPC.printf("...variables implemented\n");
00142     wait(0.2);
00143     
00144     
00145     /* setup adc input
00146      */
00147     adc.append(&getADC);
00148     
00149     adc.startmode(0,0);
00150     adc.burst(1);
00151    
00152     adc.setup(p15,1);
00153     adc.setup(p16,0);
00154     adc.setup(p17,0);
00155     adc.setup(p18,0);
00156     adc.setup(p19,0);
00157     adc.setup(p20,0);
00158    
00159     adc.interrupt_state(p15,1);
00160     
00161     
00162     usbPC.printf("%u, %u, %u, %u\n", adc.setup(p15), 
00163                                      adc.burst(),
00164                                      adc.interrupt_state(p15), 
00165                                      adc.actual_sample_rate());   
00166     
00167     usbPC.printf("...ADC initialized\n");
00168     wait(0.2);
00169     
00170 
00171 }