Codigo Voltimetro

Dependencies:   mbed

Committer:
elchef
Date:
Thu Mar 06 08:09:30 2014 +0000
Revision:
1:5f4ff0930a17
Parent:
0:8c4e48c681d1
Multimetro

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elchef 0:8c4e48c681d1 1 #include "mbed.h"
elchef 0:8c4e48c681d1 2 #define MEDIDA 10 //Declaracion de una constante para decir el tamaño del arreglo
elchef 0:8c4e48c681d1 3
elchef 0:8c4e48c681d1 4 AnalogIn Volt_Out(A5);
elchef 1:5f4ff0930a17 5 AnalogIn Volt_Ohmetro(A4);
elchef 0:8c4e48c681d1 6 DigitalOut Led_Prueba(LED1);
elchef 0:8c4e48c681d1 7 Serial pc(USBTX, USBRX);
elchef 0:8c4e48c681d1 8 Timer reloj;
elchef 0:8c4e48c681d1 9 DigitalIn boton_Volt(PTB1); //Declaracion de varibales, timer y puerto serial
elchef 0:8c4e48c681d1 10
elchef 1:5f4ff0930a17 11 float Volt_In, Tiempo_Led, Tiempo,Promedio, Vout_val,Ohms;
elchef 0:8c4e48c681d1 12 float Voltajes_por_seg[]= {0}; //Inicializacion de un arreglo con 0 en cada lugar
elchef 0:8c4e48c681d1 13
elchef 0:8c4e48c681d1 14 float promedio (float arreglo[MEDIDA]) //Funcion para sacar el promedio dentro de los valores de un arreglo mediante un contador
elchef 0:8c4e48c681d1 15 {
elchef 0:8c4e48c681d1 16 float acumulado = 0;
elchef 0:8c4e48c681d1 17 for (int count = 0; count < MEDIDA; count++) { //Aumento de lugar en el Arreglo
elchef 0:8c4e48c681d1 18 acumulado += Voltajes_por_seg[count]; //Suma del valor en el lugar count del arreglo
elchef 0:8c4e48c681d1 19 }
elchef 0:8c4e48c681d1 20 return acumulado/MEDIDA; //Regresa el promedio como un valor float
elchef 0:8c4e48c681d1 21 }
elchef 0:8c4e48c681d1 22
elchef 0:8c4e48c681d1 23 int main()
elchef 0:8c4e48c681d1 24 {
elchef 0:8c4e48c681d1 25 reloj.start(); //Comienzo del timer
elchef 0:8c4e48c681d1 26 Tiempo_Led = reloj.read(); //Inicializar una variable que sera la lectura del timer
elchef 0:8c4e48c681d1 27 while (Tiempo_Led <= 1.5) { //Al oprimir el boton reset prendera un led rojo durante 1.5segundos como medida de seguridad para saber que no quemamos nuestra tarjeta
elchef 0:8c4e48c681d1 28 Tiempo_Led = reloj.read();
elchef 0:8c4e48c681d1 29 Led_Prueba = 0;
elchef 0:8c4e48c681d1 30 }
elchef 0:8c4e48c681d1 31 Led_Prueba = 1;
elchef 0:8c4e48c681d1 32 reloj.reset(); //El timer se resetea para tomar las medidas para el voltaje
elchef 0:8c4e48c681d1 33 while(1) {
elchef 0:8c4e48c681d1 34 Vout_val = Volt_Out*5; //Una variable que tendra el valor del voltaje de salida y lo, se multiplica por 5 ya que los puertos analogicos dan lecturas de 0 a 1 y con 5 volts fue alimentada esta practica
elchef 0:8c4e48c681d1 35 Tiempo = reloj.read();
elchef 0:8c4e48c681d1 36 if(Tiempo > 0 && Tiempo < 0.5 ) { //Cada 0.5 segundos tomara las mediciones
elchef 0:8c4e48c681d1 37 for (int indice = 0; indice < MEDIDA; indice++) { //Ciclo que introduce cada valor del voltaje a cada espacio en el arreglo
elchef 0:8c4e48c681d1 38 Voltajes_por_seg[indice]= Voltajes_por_seg[indice+1]; //Se suma al indice un 1 para el lugar siguiente en el arreglo
elchef 0:8c4e48c681d1 39 Volt_In = ((Volt_Out*10330)/10000)*5; //Al voltaje de salida se multiplica por la suma de R1+R2 y se divide entre R1, se multiplica de nuevo por 5 por ser el voltaje con que alimentamos
elchef 0:8c4e48c681d1 40 Voltajes_por_seg[MEDIDA-1]= Volt_In; //Elmininamos el primer valor y lo sustituimos por el siguiente
elchef 0:8c4e48c681d1 41 reloj.reset(); //El timer se resetea para hacer de nuevo mediciones
elchef 0:8c4e48c681d1 42 }
elchef 0:8c4e48c681d1 43 Promedio=promedio(Voltajes_por_seg); //Uso del promedio para saber el voltaje de entrada
elchef 0:8c4e48c681d1 44 pc.printf("Voltaje en promedio es %f \n",Promedio*28/5); //Saber que voltaje de entrada tengo a 28 volts de alimentacion¡
elchef 1:5f4ff0930a17 45 Ohms = 1/(1/(Volt_Ohmetro*3.3/.005-560)-1/100);//Resistencia, conociendo Vo
elchef 1:5f4ff0930a17 46
elchef 0:8c4e48c681d1 47 }
elchef 0:8c4e48c681d1 48 }
elchef 1:5f4ff0930a17 49 }