Team Unibilbao / Mbed 2 deprecated Muestreo_JAVI

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #define bufferLeng 500
00003 #define constante 5.035400390625e-5
00004 struct eResult {
00005     float Vrms;
00006     float Vmax;
00007     float Vmin;
00008 };
00009 
00010 AnalogIn    adc_in(A0);
00011 AnalogOut   dac_out(A2);
00012 Ticker tMuestr;
00013   Ticker tVis;
00014 
00015 void vis();
00016 void procDat(uint16_t *ent, uint16_t *sal, int longi);
00017 
00018 int flag=0;
00019 int contador=0;
00020 
00021 struct buffers {
00022     uint16_t A[bufferLeng];
00023     uint16_t B[bufferLeng];
00024 };
00025 
00026 bool bufferAct=0;
00027 void muestr();
00028 
00029 buffers bufferEnt;
00030 buffers bufferSal;
00031 
00032 eResult result;
00033 
00034 int main()
00035 {
00036 
00037     tMuestr.attach_us(&muestr, 100);
00038       tVis.attach(&vis, 5);
00039 
00040     while(1) {
00041         if (flag) {
00042             flag=0;
00043             if(bufferAct) {
00044                 procDat(bufferSal.B, bufferEnt.A,bufferLeng);
00045             } else {
00046                 procDat(bufferSal.A,bufferEnt.B,bufferLeng);
00047             }
00048 
00049             wait_us(1);
00050 
00051         }
00052     }
00053 }
00054 void muestr()
00055 {
00056     uint16_t dato=adc_in.read_u16();
00057     if (bufferAct) {
00058         bufferEnt.A[contador]=dato;
00059         dato=bufferSal.B[contador];
00060     } else {
00061         bufferEnt.B[contador]=dato;
00062         dato=bufferSal.A[contador];
00063     }
00064     dac_out.write_u16(dato);
00065     contador++;
00066     if (contador==bufferLeng) {
00067         bufferAct=!bufferAct;
00068         contador=0;
00069         flag=1;
00070     }
00071 }
00072 
00073 
00074 void procDat(uint16_t *sal, uint16_t *ent, int longi)
00075 {
00076       uint16_t max=0;
00077       uint16_t min=65535;
00078     uint16_t dato;
00079       uint64_t sumatorio=0;
00080     for(int i=0; i<longi; i++) {
00081         dato=ent[i];
00082           sumatorio= sumatorio + (dato*dato);
00083         sal[i]=dato;
00084           if(dato>max) {
00085               max=dato;
00086           }
00087           if(dato<min) {
00088               min=dato;
00089           }
00090 
00091     }
00092       result.Vrms= constante*sqrt(((float)sumatorio)/longi);
00093       result.Vmax=max*constante;
00094       result.Vmin=min*constante;
00095 }
00096   void vis()
00097   {
00098       printf ("El valor maxima es %f\n",result.Vmax);
00099       printf ("El valor minimo es %f\n",result.Vmin);
00100       printf ("El valor RMS es %f\n\n",result.Vrms);
00101 
00102   }