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 void visualizar(); 00002 00003 #include "mbed.h" 00004 #define buffer_length 2000 00005 00006 Ticker tMuestrear; 00007 Ticker tVisualizar; 00008 #include "TextLCD.h" 00009 00010 00011 InterruptIn button(PC_13); 00012 00013 TextLCD lcd(D8, D9, D2, D3, D4, D5, TextLCD::LCD16x2); // rs, e, d4-d7 00014 Serial pc(USBTX, USBRX); // tx, rx 00015 00016 Serial bluetooth(PC_0, PC_1); 00017 00018 AnalogIn adc_in(A0); 00019 AnalogIn adc_in_in(A2); 00020 AnalogIn adc_in_dos(A1); 00021 00022 void muestrear(); 00023 void visualizar(); 00024 int flag=0; 00025 int contador=0; 00026 int contador_boton=0; 00027 bool buffer_activo=0; 00028 struct buffer { 00029 int16_t vA[buffer_length]; 00030 int16_t iA[buffer_length]; 00031 int16_t vB[buffer_length]; 00032 int16_t iB[buffer_length]; 00033 }; 00034 buffer buffers; 00035 struct estr_medidas 00036 { 00037 float vrms; 00038 float irms; 00039 float pot_activa; 00040 float pot_reactiva; 00041 float pot_aparente; 00042 float en_consumida; 00043 float fact_potencia; 00044 }; 00045 estr_medidas medidas; 00046 void procesado(int16_t *datosV, int16_t *datosI, int longi, estr_medidas *medidas) 00047 { 00048 int64_t b=0.0; 00049 int64_t c=0; 00050 int64_t d=0; 00051 for(int j=0;j<longi;j++) 00052 { 00053 b = b + datosV[j]*datosV[j]; 00054 c = c + datosI[j]*datosI[j]; 00055 d = d + datosV[j]*datosI[j]; 00056 } 00057 00058 00059 medidas->vrms=0.01275509675*sqrt(((float)b)/longi); 00060 medidas->irms=0.0004536585089*sqrt(((float)c)/longi); 00061 medidas->pot_activa=(5.786458173e-6*d)/longi; 00062 medidas->pot_aparente=medidas->vrms*medidas->irms; 00063 medidas->pot_reactiva=sqrt((medidas->pot_aparente*medidas->pot_aparente)-(medidas->pot_activa*medidas->pot_activa)); 00064 medidas->fact_potencia=((float)medidas->pot_activa)/medidas->pot_aparente; 00065 medidas->en_consumida+=0.001*medidas->pot_activa*longi; 00066 00067 00068 } 00069 void flip() { 00070 contador_boton++; 00071 if (contador_boton==8) 00072 { 00073 contador_boton=0; 00074 } 00075 } 00076 int main() 00077 { 00078 pc.baud(115200); 00079 bluetooth.baud(9600); 00080 tMuestrear.attach_us(&muestrear, 200); 00081 tVisualizar.attach(&visualizar, 1); 00082 button.rise(&flip); 00083 while(1) { 00084 if(flag==1) { 00085 00086 flag=0; 00087 00088 if(buffer_activo) { 00089 00090 procesado((int16_t *) buffers.vA, (int16_t *)buffers.iA, buffer_length, &medidas); 00091 00092 } else { 00093 procesado((int16_t *)buffers.vB,(int16_t *)buffers.iB, buffer_length, &medidas); 00094 00095 } 00096 00097 00098 } 00099 00100 } 00101 } 00102 void muestrear() 00103 { 00104 int16_t datov=(int16_t)(adc_in.read_u16()^0x8000); 00105 int16_t datoi=(int16_t)(adc_in_in.read_u16()^0x8000); 00106 int16_t continua=(int16_t)(adc_in_dos.read_u16()^0x8000); 00107 if (buffer_activo==0) { 00108 buffers.vA[contador]=datov-continua; 00109 buffers.iA[contador]=datoi-continua; 00110 00111 } else { 00112 buffers.vB[contador]=datov-continua; 00113 buffers.iB[contador]=datoi-continua; 00114 00115 } 00116 contador++; 00117 if(contador==buffer_length) { 00118 //pc.printf("2"); 00119 contador=0; 00120 flag=1; 00121 buffer_activo=!buffer_activo; 00122 } 00123 } 00124 void visualizar(){ 00125 00126 00127 pc.printf("**** El valor Vrms es %f ****\n",medidas.vrms); 00128 pc.printf("**** El valor Irms es %f ****\n",medidas.irms); 00129 pc.printf("**** La potencia activa es %f ****\n",medidas.pot_activa); 00130 pc.printf("**** La potencia reactiva es %f ****\n",medidas.pot_reactiva); 00131 pc.printf("**** La potencia aparente es %f ****\n",medidas.pot_aparente); 00132 pc.printf("**** La energia consumida es %f ****\n",medidas.en_consumida); 00133 pc.printf("**** El factor de potencia es es %f ****\n\n",medidas.fact_potencia); 00134 00135 switch (contador_boton){ 00136 case 1: 00137 00138 lcd.cls(); 00139 lcd.printf("Vrms:\n%3.4f V\n", medidas.vrms); 00140 break; 00141 00142 case 2: 00143 00144 lcd.cls(); 00145 lcd.printf("Irms:\n%3.2f A\n", medidas.irms); 00146 break; 00147 00148 case 3: 00149 00150 lcd.cls(); 00151 lcd.printf("Pot activa\n%3.2f W\n", medidas.pot_activa); 00152 break; 00153 00154 case 4: 00155 00156 lcd.cls(); 00157 lcd.printf("Pot reactiva\n%3.2f VAr\n", medidas.pot_reactiva); 00158 break; 00159 00160 case 5: 00161 00162 lcd.cls(); 00163 lcd.printf("Pot aparente\n%3.2f VA\n", medidas.pot_aparente); 00164 break; 00165 00166 case 6: 00167 00168 lcd.cls(); 00169 lcd.printf("En consumida\n%3.2f kW\n", medidas.en_consumida); 00170 break; 00171 00172 case 7: 00173 00174 lcd.cls(); 00175 lcd.printf("Fact potencia\n%3.2f\n", medidas.fact_potencia); 00176 break; 00177 default: 00178 lcd.cls(); 00179 lcd.printf("Pulsar boton"); 00180 } 00181 bluetooth.printf("%3.2f|%3.2f|%3.2f|%3.2f|%3.2f|%3.2f|%3.2f|\n",medidas.vrms, 00182 medidas.irms,medidas.pot_activa,medidas.pot_reactiva, 00183 medidas.pot_aparente,medidas.en_consumida,medidas.fact_potencia); 00184 }
Generated on Sun Aug 7 2022 14:54:09 by
1.7.2