ccvcv
Fork of PRACTICA3 by
Revision 5:18feff1607c9, committed 2017-11-13
- Comitter:
- jon07
- Date:
- Mon Nov 13 12:42:08 2017 +0000
- Parent:
- 4:71a955ddc240
- Commit message:
- fgfgfg
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Nov 10 13:05:19 2017 +0000
+++ b/main.cpp Mon Nov 13 12:42:08 2017 +0000
@@ -1,118 +1,99 @@
#include "mbed.h"
-#include "TextLCD.h"
+#define period 1/5000
+#define sizeVector 100
+#define Constant (0.000001661707484)
+//USER_BUTTON o PC_13
-#define constante (3,3^2)/((2^16)-1)*100) // valor constante para el cálculo de Vrms
-#define vector 100 // valor para definir el tamaño de los búfferes de muestreo
+InterruptIn button(PC_13);
+
+uint32_t vA[sizeVector];
+uint32_t vB[sizeVector];
+
+AnalogIn valors(A0);
-//TextLCD lcd(6 puertos, TextLCD::LCD16x2); rs,e,d4-d7
-InterruptIn Sw (); //pulsador en el puerto
-AnalogIn leer (); // leer del puerto
-Ticker muestreo;
-Ticker visualizar;
-int contt=0;
-int contm=0;
-int contvpp=0;
-int dato=0, // variable para seleccionar el dato a mostrar
-int flag1; // variable para indicar cuando se tiene que cambiar de buffer
-int flag2; // variable para indicar cuando se tiene que
-int flag3; // variable para indicar cuando se tiene que calcular Vpp
-int flag4; // variable para indicar cuando se llena el buffer
-int Vpmax=0; // variable para guardar el valor del pico superior
-int Vpmin=0; // variable para guardar el valor del pico inferior
-int Vpp=0;
-float buffer1 [vector]; // buffer de muestreo 1 de tamaño 100
-float buffer2 [vector]; // buffer de muestreo 2 de tamaño 100
-float Vrms; // variabla para guardar el valor de RMS
-float valor;
-float sumatorio; // variable que es la suma de los valores de la muestra
-float muestra; // variabla para hacer las operaciones
+int counter=0;
+int flag=0;
+int whatVector=0;
+uint64_t sum_values=0;
+float RMS_value=0;
+float Vpp_value=0;
+float Max_value=0;
+float Min_value=1000;
+int state=0;
+
+Ticker sample;
+Ticker visualize;
-
-//void temporizador ()
-//{
-// if (flag2) {
-//
-// }
-// contt++;
-// if (contt==100) {
-// contt =0;
-// flag2 =!flag2;
-// bufferlleno=0;
-// }
-//}
-
-void boton()
+void fsample() //??
{
- dato++;
- if (dato>1) {
- dato = 0;
+ if (whatVector) {
+ vB[counter]=(valors.read_u16()+valors.read_u16()+valors.read_u16()+valors.read_u16())/4;
+ }
+ else {
+ vA[counter]=(valors.read_u16()+valors.read_u16()+valors.read_u16()+valors.read_u16())/4;
+ }
+ counter++;
+ if (counter==100) {
+ counter=1;
+ flag=1;
+ whatVector=!whatVector; //??
}
}
-
-//void LCD()
-//{
-// lcd.cls();
-// switch (dato) {
-// case 0:
-// lcdprintf("Vrms: %n V",Vrms); //Vrms :\n%3.3f V,Vrms
-// break;
-// case 1:
-// lcdprintf("Vpp: %n V",Vpp); //
-// break;
-// }
-//}
-
-void calculovpp ()
+void change() //??
+{
+ state=!state;
+ }
+void visualiza()
{
- for(contvpp=0; contvpp=99; contvpp++) {
- if (muestra(contvpp)>Vpmax) {
- Vpmax=muestra(contvpp);
- } else {
- if (muestra(contvpp)<Vpmin) {
- Vpmin=muestra(contvpp);
- }
- }
- else {
- if(!flag3) {
- Vpp = Vpmax-Vpmin;
- flag3=!flag3;
- }
- }
+ if(state){
+ printf("RMS value is =%f",RMS_value);
}
-}
-
-void calculovrms ()
-{
- for (contvrms=0; contvrms=99; contvrms++) {
- valor = muestra(contvrms);
- valor = valor*valor;
- sumatorio = sumatorio + valor;
+ else {
+ printf("Vpp value is =%f",Vpp_value);
}
- Vrms = (constante*sumatorio)^(0.5);
}
int main()
{
- muestreo.attach_us (&temporizador,200);
- visualizar.attach (&mostrar, 1);
- Sw.rise(&boton);
-
- //printf("pwm set to %.2f %%\n", mypwm.read() * 100);
-
+ uint32_t temp;
+ sample.attach(&fsample,period); //& para llamar a la función
+ visualize.attach(&visualiza,1);
+ button.rise(&change);
while(1) {
- if (flag4) {
- valor=0;
- sumatorio=0;
+ if (flag) {
+ flag=0;
+ sum_values=0;
+ if(whatVector) {
+
+ for(int n=0; n<99; n++) {
+ temp=vA[n];
+ sum_values=sum_values+temp;
- flag3=0;
-for (contm=0; contm=99: contm++) {
- if (flag1==1) {
- muestra=buffer1[contm];
- } else {
- muestra=buffer2[contm];
+ if(Max_value<temp) {
+ Max_value=temp;
+ }
+ if(Min_value>temp) {
+ Min_value=temp;
+ }
}
}
- }
+ else {
+
+ for(int n=0; n<99; n++) {
+ temp=vB[n];
+ sum_values=sum_values+temp;
+ if(Max_value<temp) {
+ Max_value=temp;
+ }
+ if(Min_value>temp) {
+ Min_value=temp;
+ }
+ }
+ }
+ RMS_value=sqrt(Constant*(float)sum_values);
+ Vpp_value=Max_value-Min_value;
+
+ }
}
-}
+}
\ No newline at end of file
