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 #include "mbed.h" 00002 #define period 0.0002 //1/5000 00003 #define sizeVector 100 00004 #define Constant_v 1.2092e-06 00005 #define Constant_i 5.5641e-06//((3,3*2000/(2^16*Rburn))^2)*(1/100) 00006 //USER_BUTTON o PC_13 00007 #include "TextLCD.h" 00008 #define dato_p 202.6327 //para calcular el precio 00009 00010 InterruptIn button(PC_13); 00011 AnalogIn valuesV(A0); 00012 AnalogIn valuesOffset(A1); 00013 AnalogIn valuesI(A5); 00014 00015 Serial bluetooth(D10,D2); 00016 TextLCD lcd(D8, D9, D3, D4, D5, D6 ,TextLCD::LCD16x2); // rs, e, d4-d7 00017 00018 int16_t vA[sizeVector]; 00019 int16_t vB[sizeVector]; 00020 int16_t iA[sizeVector]; 00021 int16_t iB[sizeVector]; 00022 uint16_t valorOffset; 00023 00024 uint32_t temporal; 00025 00026 int counter=0; 00027 int flag=0; 00028 int whatVector=0; 00029 int state=0; 00030 int16_t temp_v=0; 00031 int16_t temp_i=0; 00032 int64_t sum_values_v=0; 00033 int64_t sum_values_i=0; 00034 float VRMS=0; 00035 float IRMS=0; 00036 float Pact=0; 00037 float Preactiva=0; 00038 float Paparente=0; 00039 float EnergiActiva=0; 00040 float EnergiaReactiva=0; 00041 float cosPHI=0; 00042 float precio=0; 00043 00044 00045 00046 Ticker sample; //Puntero para el attach 00047 Ticker visualize; 00048 00049 void fsample() 00050 { 00051 valorOffset=(int16_t)(valuesOffset.read_u16()^0x8000); 00052 if (whatVector) { 00053 temporal=((valuesV.read_u16()+valuesV.read_u16()+valuesV.read_u16()+valuesV.read_u16())/4); 00054 00055 vB[counter]=((int16_t)(temporal^0xFFFF8000))-valorOffset; 00056 //printf("%u %d\r\n",vB[counter], counter); 00057 temporal=((valuesI.read_u16()+valuesI.read_u16()+valuesI.read_u16()+valuesI.read_u16())/4); 00058 iB[counter]=((int16_t)(temporal^0xFFFF8000))-valorOffset; 00059 } else { 00060 temporal=((valuesV.read_u16()+valuesV.read_u16()+valuesV.read_u16()+valuesV.read_u16())/4); 00061 vA[counter]=((int16_t)(temporal^0xFFFF8000))-valorOffset; 00062 00063 temporal=((valuesI.read_u16()+valuesI.read_u16()+valuesI.read_u16()+valuesI.read_u16())/4); 00064 iA[counter]=((int16_t)(temporal^0xFFFF8000))-valorOffset; 00065 } 00066 counter++; 00067 if (counter==100) { 00068 counter=0; 00069 flag=1; 00070 whatVector=!whatVector; 00071 } 00072 } 00073 void change_button() 00074 { 00075 state++; 00076 if(state>8) { 00077 state=0; 00078 } 00079 } 00080 void visualiza() 00081 { 00082 lcd.cls(); 00083 bluetooth.printf("%f|%f|%f|%f|%f|%f|%f\n\r",VRMS,IRMS,Pact,EnergiActiva,EnergiaReactiva,Preactiva,Paparente); 00084 switch(state) { 00085 00086 case 0: 00087 lcd.printf("VRMS: \n %f",VRMS); 00088 break; 00089 case 1: 00090 lcd.printf("IRMS: \n %f",IRMS); 00091 break; 00092 case 2: 00093 lcd.printf("Potencia Activa: \n %f",Pact); 00094 break; 00095 case 3: 00096 lcd.printf("Energia Activa:\n\r%f",EnergiActiva); 00097 break; 00098 case 4: 00099 lcd.printf("Energía Reactiva:\n\r%f",EnergiaReactiva); 00100 break; 00101 case 5: 00102 lcd.printf("Potecia Reactiva:\n\r%f",Preactiva); 00103 break; 00104 case 6: 00105 lcd.printf("Potencia Aparente:\n\r%f",Paparente); 00106 break; 00107 case 7: 00108 lcd.printf("Coseno de phi:\n\r%f",cosPHI); 00109 break; 00110 00111 bluetooth.printf("%f | %f | %f | %f | %f \n",VRMS,IRMS,Pact,Preactiva,Paparente); 00112 } 00113 } 00114 int main() 00115 { 00116 sample.attach(&fsample,period); 00117 visualize.attach(&visualiza,1); 00118 button.rise(&change_button); 00119 int32_t productoTemp; 00120 00121 while(1) { 00122 if (flag) { 00123 flag=0; //Para que entre en el if cuando haya un cambio a 1 de flag, es decir, counter=100 00124 sum_values_v=0; //Inicializar a 0 00125 sum_values_i=0; 00126 //printf("****************************\n\r"); 00127 for(int n=0; n<100; n++) { 00128 if(whatVector) { 00129 temp_v=vA[n]; 00130 temp_i=iA[n]; 00131 } else { 00132 temp_v=vB[n]; 00133 temp_i=iB[n]; 00134 } 00135 //printf("%d\n\r",temp_v); 00136 productoTemp=temp_v*temp_v; 00137 sum_values_v=sum_values_v+productoTemp; 00138 productoTemp=temp_i*temp_i; 00139 sum_values_i=sum_values_i+productoTemp; 00140 sum_values_pa=sum_values_pa+(sum_values_i*sum_values_v); 00141 sum_values_pr= 00142 sum_values_papa= 00143 00144 00145 } 00146 00147 } 00148 printf("%f\n\r",(float)sum_values_v); 00149 VRMS=sqrt(Constant_v*(float)sum_values_v); 00150 //VRMS=sqrt((((float)Constant_v*(float)Constant_v)*(1/100))*(float)sum_values_v); 00151 //IRMS=sqrt((((float)Constant_i*(float)Constant_i)*(1/100))*(float)sum_values_i); 00152 IRMS=sqrt(Constant_i*(float)sum_values_i); 00153 //Pact=(((((float)Constant_v*(float)Constant_i)*(1/100))*(float)sum_values_v)*(float)sum_values_i); 00154 Pact=202.63*sum_values_pa; 00155 EnergiActiva=EnergiActiva+Pact*20e-3/(3600*1000); //kWh 00156 Paparente=VRMS*IRMS; 00157 Preactiva=sqrt((Paparente*Paparente)-(Pact*Pact)); 00158 EnergiaReactiva=EnergiaReactiva+Preactiva*20e-3/(3600*1000); 00159 cosPHI=Pact/Paparente; 00160 precio=EnergiActiva*dato_p; 00161 00162 } 00163 } 00164 00165
Generated on Tue Aug 2 2022 04:44:41 by
1.7.2