menu-teclado-lcd-pwm(funca todo) (y el puerto serie tambien)

Dependencies:   TextLCD mbed

Revision:
8:5cfa4b38ddf1
Parent:
7:e27232481540
Child:
9:364316a55827
--- a/main.cpp	Mon Dec 07 21:34:24 2015 +0000
+++ b/main.cpp	Mon Dec 14 15:53:14 2015 +0000
@@ -1,6 +1,7 @@
 #include "mbed.h"
 #include "TextLCD.h"
 #include "AnalogIn.h"
+#include "Serial.h"
 
 //clases
 Timer t;
@@ -10,18 +11,85 @@
 //entradas
 DigitalIn key1(PTE31), key4(PTE17), key2(PTE19), key3(PTE18);
 InterruptIn Enable(PTA13);
-AnalogIn externo(PTC2),interno(PTB3), presionin(PTB2), pwment(PTB1);
+AnalogIn externo(PTC2),interno(PTB3), presionin(PTB2), pident(PTB1);
+AnalogOut vref(PTE30);
 PwmOut pwmsal(PTD2);
 
 //declaracion de variables
-
 char tipo, modo = 'M';
-float tinterno, texterno, presion, casexterno, casinterno, caspresion, valorpwm;
+float tinterno, texterno, presion, casexterno, casinterno, caspresion, valorpwm, valorpid, vrefe = 1;
+float Tres=0, optimo=0;
 int i = 0, j = 0, sel = 1, hora=0, min=0, seg=0, horain = 8, minin = 0, horafin = 22, minfin = 0, temp = 26;
 int hh1, hh0, mm0, mm1, B1, B2, B3, B4, num, tempauto = 0, tipobanio = 0, pdig, sdig, tempmanual = 40, set = 0;
 void menu();
 void selec();
 void func();
+//recibe tension de referencia cambiar por que reciba la de el teclado o del externo
+void modotemp()
+{
+    if (modo == 'A') {
+        switch (tempauto) {
+
+            case 0:
+                if ((hora > horain||(hora == horain && min >= minin)) && ( hora < horafin ||(hora == horafin && min <= minfin))) {
+                    optimo= -(11/10*texterno)+50;
+                    if (optimo > 25 && optimo < 65) {
+                        Tres = optimo;
+                    } else {
+                        Tres = tinterno;
+                    }
+                } else {
+                    Tres = tinterno;
+                }
+                break;
+            case 1:
+                if ((hora > horain||(hora == horain && min >= minin)) && ( hora < horafin ||(hora == horafin && min <= minfin))) {
+                    optimo= -(11/10*texterno)+55;
+                    if (optimo > 25 && optimo < 65) {
+                        Tres = optimo;
+                    } else {
+                        Tres = tinterno;
+                    }
+                } else {
+                    Tres = tinterno;
+                }
+                break;
+            case 2:
+                if ((hora > horain||(hora == horain && min >= minin)) && ( hora < horafin ||(hora == horafin && min <= minfin))) {
+                    optimo= -(11/10*texterno)+65;
+                    if (optimo > 25 && optimo < 65 ) {
+                        Tres = optimo;
+
+                    } else {
+                        Tres = tinterno;
+                    }
+                } else {
+                    Tres = tinterno;
+                }
+                break;
+        }
+    }
+    if (modo == 'M') {
+        Tres = tempmanual;
+    }
+    vrefe = ((Tres * 8 / 100) - 2) / 3.3;
+}
+//funcion del pwm
+
+void pwm()
+{
+    if(vrefe <= 1 && vrefe >= 0) {
+        vref.write(vrefe);
+    }
+    valorpid = pident.read() * 3.3;
+    if(valorpid >1)
+        pwmsal.write(1);
+    else if(valorpid <= 1 && valorpid >= 0.6) {
+        valorpwm = 2.5 * valorpid - 1.5;
+        pwmsal.write(valorpwm);
+    } else
+        pwmsal.write(0);
+}
 
 //funcion de set de hora
 void sethora()
@@ -88,7 +156,6 @@
                     lcd.locate(0,1);
                     lcd.printf("%.0fC %.0fC %c ", texterno, tinterno, modo);
                     i++;
-
                 }                                                           //fin 2do digito minuto
 }
 // fin funcion set de hora
@@ -114,7 +181,6 @@
     B2 = key2.read();
     B3 = key3.read();
     B4 = key4.read();
-
     num = 2*2*2*B1 + 2*2*B2 + 2*B3 + B4;
     pc.printf("llama a func\n\r");
     if(num == 10) {
@@ -227,8 +293,10 @@
                     sdig = num;
                     pc.printf("%.1d\n\r", sdig);
                     tempmanual = (pdig * 10) + sdig;
-                    if(tempmanual >= 65){
+                    if(tempmanual >= 65) {
                         tempmanual = 65;}
+                    if(tempmanual <= 25){
+                        tempmanual = 25;}
                     pc.printf("%.1d\n\r", tempmanual);
                     imprimehora();
                     lcd.locate(0,0);
@@ -349,7 +417,6 @@
     }
 }
 
-
 void selec()
 {
     switch (sel) {
@@ -404,7 +471,6 @@
             set = 6;
             break;
     }
-
 }
 
 //programa principal
@@ -415,6 +481,7 @@
     Enable.rise(&func);
 
     while(1) {
+        modotemp();
 //reloj
         seg = t.read();
         if(seg == 60) {
@@ -430,10 +497,7 @@
             hora=0;
         }
         imprimehora();
-//pwm
-        pwmsal.period(5);
-        valorpwm = pwment.read();
-        pwmsal.write(valorpwm);
+
 //convierte voltaje a temperatura
         casexterno = externo.read() * 3.3;
         texterno = (casexterno + 0.2) * 100 / 5;
@@ -453,7 +517,7 @@
 
         lcd.locate(0,1);
         lcd.printf("%.0fC %.0fC %c ", texterno, tinterno, modo);
-
+        pwm();
         wait(1);
-    }
+    }           //fin wile
 }
\ No newline at end of file