trabajo de creacion de un despertador por i2c configurado por un encoder

Dependencies:   DebouncedIn QEI RTC-DS1307 mbed

Fork of I2C by German Camilo Diaz Mejia

Revision:
4:fa48e78e9bba
Parent:
3:9e5e0b8d0e2a
Child:
5:d38b972a1813
--- a/main.cpp	Fri Nov 20 03:48:18 2015 +0000
+++ b/main.cpp	Fri Nov 20 13:49:02 2015 +0000
@@ -2,308 +2,252 @@
 #include "Rtc_Ds1307.h"
 #include "DebouncedIn.h" 
 #include "QEI.h"
-
-//RtcCls rtc(p28, p27, p29, true);
-Rtc_Ds1307 rtc(PTE0,PTE1);
-
-Serial pc(USBTX, USBRX, "pc");
-
-//Configuracion encoder
-QEI wheel (PTD5, PTD0, NC, 100);
+ 
+Rtc_Ds1307 rtc(PTE0,PTE1);        //Puertos para el modulo
+Serial pc(USBTX, USBRX, "pc");    //Puertos para el PC
+QEI wheel (PTD5, PTD0, NC, 100);  //Configuracion encoder
+DebouncedIn Boton(PTA13);        //Boton Encoder
+DigitalOut led1(LED1);          //Salida
 
-//Botones
-DebouncedIn BotonENC(PTA13);  //Boton Encoder
-
-DebouncedIn Boton2(PTE20);  //Boton para confirmar
-
-//salida led
-DigitalOut myled(PTE21);
-int dia=0,mes=0,ano=2015,hh=0,mm=0,ss=0;
-
+int Dia=0;
+int Mes=0;
+int Anio=2015;
+int Horas=0;
+int Minutos=0;
+int Segundos=0;
+ 
 char buffer[128];
 int readptr = 0;
-int start=0;
-int Config;
-
-int main() {
+ 
+int main() 
+    {
     char c;
     Rtc_Ds1307::Time_rtc tm = {};
     
-    while(1) {
+    while(1) 
+    {      
+    pc.printf("*************************************\n");
+    pc.printf("* Menu for RTC Test :               *\n");
+    pc.printf("* read  - reads the clock           *\n");
+    pc.printf("* start - start the clock           *\n");
+    pc.printf("* stop  - stop the clock            *\n");
+    pc.printf("* write - write the clock           *\n");
+    pc.printf("* ena   - enable Square wave output *\n");
+    pc.printf("* dis   - disable square wave outp. *\n");
+    pc.printf("*************************************\n");
         
-        set_reloj:
-       
-        pc.printf("*************************************\n");
-        pc.printf("* Menu for RTC Test :               *\n");
-        pc.printf("* read  - reads the clock           *\n");
-        pc.printf("* start - start the clock           *\n");
-        pc.printf("* stop  - stop the clock            *\n");
-        pc.printf("* write - write the clock           *\n");
-        pc.printf("* ena   - enable Square wave output *\n");
-        pc.printf("* dis   - disable square wave outp. *\n");
-        pc.printf("*************************************\n");
-        
-        while( (c = pc.getc()) != '\n') {
-            buffer[readptr++] = c;
+        while( (c = pc.getc()) != '\n')
+        {
+        buffer[readptr++] = c;
         }
         buffer[readptr++] = 0;
-        if (strncmp(buffer, "read", 4) == 0) {
-            //  perform read
+            if (strncmp(buffer, "read", 4) == 0) 
+            {
             pc.printf("Performing read operation\n");
-            if (rtc.getTime(tm) ) {
-                pc.printf("The current time is : %02d:%02d:%02d\n", tm.hour, tm.min, tm.sec);
-                pc.printf("The current date is : %s, %02d/%02d/%04d\n", rtc.weekdayToString(tm.wday), tm.mon, tm.date, tm.year);
-            }
+                if (rtc.getTime(tm) ) 
+                {
+                pc.printf("La hora actual es: %02d:%02d:%02d\n", tm.hour, tm.min, tm.sec);
+                pc.printf("La fecha actual es: %s, %02d/%02d/%04d\n", rtc.weekdayToString(tm.wday), tm.mon, tm.date, tm.year);
+                }
             
-        }
-        else if (strncmp(buffer, "write", 5) == 0) {
-            //  perform write
-            pc.printf("Enter the date (date 0..31)");
+             }   
+        else if (strncmp(buffer, "write", 5) == 0) 
+            {
+            pc.printf("Dia (0..31)");
             pc.scanf("%d", &tm.date);
-            pc.printf("Enter the date (month 1..12)");
+            pc.printf("Mes (1..12)");
             pc.scanf("%d", &tm.mon);
-            pc.printf("Enter the date (year)");
+            pc.printf("Anno");
             pc.scanf("%d", &tm.year);
-            pc.printf("Enter the time (hours 0..23)");
+            pc.printf("Hora (0..23)");
             pc.scanf("%d", &tm.hour);
-            pc.printf("Enter the time (minutes 0..59)");
+            pc.printf("Minutos (0..59)");
             pc.scanf("%d", &tm.min);
-            pc.printf("Enter the time (seconds 0..59)");
+            pc.printf("Segundos (0..59)");
             pc.scanf("%d", &tm.sec);
             pc.printf("Performing write operation\n");
             
             while(pc.readable()) 
-                pc.getc();
+            pc.getc();
             rtc.setTime(tm, false, false);
-        }
-        else if (strncmp(buffer, "start", 5) == 0) {
-            //  start
+            }
+        else if (strncmp(buffer, "start", 5) == 0) 
+            {
             readptr = 0;
             pc.printf("Dispositivo Iniciado\n");
             rtc.startClock();
-            goto set_prev;
-            
-            
+            goto set_alarma;
+                        
         }
-        else if (strncmp(buffer, "stop", 4) == 0) {
-            //  stop
+        else if (strncmp(buffer, "stop", 4) == 0) 
+        {
             pc.printf("Performing stop operation\n");
             rtc.stopClock();
         }
-        else if (strncmp(buffer, "ena", 3) == 0) {
+        else if (strncmp(buffer, "ena", 3) == 0) 
+        {
             int rs;
             pc.printf("Please specify the frequency : [0 = 1Hz, 1 = 4.096kHz, 2 = 8.192kHz, 3 = 32.768kHz] ");
             scanf("%d", &rs);
             pc.printf("Enabling the output with %d option\n", rs);
             rtc.setSquareWaveOutput(true, (Rtc_Ds1307::SqwRateSelect_t)rs);
         }
-        else if (strncmp(buffer, "dis", 3) == 0) {
+        else if (strncmp(buffer, "dis", 3) == 0)
+        {
             pc.printf("Disableing square wave output\n");
             rtc.setSquareWaveOutput(false, Rtc_Ds1307::RS1Hz);
         }
-        else {
+        else 
+        {
             pc.printf("syntax error\n");
         }
         readptr = 0;
-        //pc.printf("\n\n\n");
         }
-        ///////////////////////////////////////////////////////////////7
-        set_prev:
-        pc.printf("Dispositivo Configurado ? ---- 1 = No   2 = Si\n");
-        pc.scanf("%d", &Config);
-        
-        if (Config ==1){
-            readptr = 0;
-            goto set_reloj;
-            }
-        
-                  
-        else if (Config ==2){
-             
-             pc.printf("Dispositivo Configurado Correctamente  Espere ....\n");
-             wait(5);
-             goto set_alarma;
-        }
-        else {
-            pc.printf("syntax error\n");
-            goto set_prev;
-        }
-        /////////////////////////////////7
-        
-        set_alarma:
-        pc.printf("******* Configuracion Alarma *******\n");
+                
+        set_alarma:          //se inicia a configurar la alarma
+        pc.printf("Configuracion de la alarma\n");
         
         
-        set_dia:
-        while(1){
-         
-            dia=dia+wheel.getPulses();
+        set_Dia:             //empieza con el dia 
+        while(1)
+        {         
+            Dia=Dia+wheel.getPulses();  //con el encoder se aumenta el dia 
             wheel.reset();
-            ////////////////////////////////////////7 
      
-            if(dia>=31){
-                dia=31;
+            if(Dia>=31)
+            {               //como el dia no tiene mas de 31 dias, se mantiene en 31 al ingresar un valor diferente
+            Dia=31;
             }
-            else if (dia<=0){
-             dia=0;
+            else if (Dia<=0)
+            {          //no hay dias negativos, el valor se mantiene en 0 en caso de ingresar uno asi
+             Dia=0;
             }
-     pc.printf(">Dia=%d    Mes=%d    Ano=%d    Hora=%d    Min=%d    Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+     pc.printf("Dia=  %d\n",Dia);        //muestra
     
     
-    if(BotonENC.falling()){
-        goto set_mes;
-        }
-    if(Boton2.falling()){
-        goto set_comp;
+    if(Boton.falling())
+    {              //con el enconder presionado pasamos a mes
+    goto set_Mes;
     }
-           
-            
     }
         
-        set_mes:
-        while(1){
+set_Mes:                           //Configuracion del Mes para la alarma
+
+        while(1)
+        {
          
-            mes=mes+wheel.getPulses();
-            wheel.reset();
-            ////////////////////////////////////////7 
-     
-            if(mes>=12){
-                mes=12;
+        Mes=Mes+wheel.getPulses();     //con el encoder aumentamos el mes 
+        wheel.reset();
+        
+        if(Mes>=12)
+        {                   //maximo a 12 meses si es mayor, matiene a 12
+                Mes=12;
             }
-            else if (mes<=0){
-             mes=0;
+            else if (Mes<=0)
+            {              //de igual forma mantiene en 0 a un valor menor
+             Mes=0;
             }
-     pc.printf(" Dia=%d   >Mes=%d    Ano=%d    Hora=%d    Min=%d    Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+     pc.printf("Mes=  %d\n ",Mes);          //muestra el mes
     
     
-    if(BotonENC.falling()){
-        goto set_ano;
+    if(Boton.falling())
+    {
+        goto set_Anio;
     }
-    if(Boton2.falling()){
-        goto set_comp;
-    }
-           
+    
             
     }
-     set_ano:
-        while(1){
-         
-            ano=ano+wheel.getPulses();
+     set_Anio:
+        while(1)
+        {         
+            Anio=Anio+wheel.getPulses();
             wheel.reset();
-            ////////////////////////////////////////7 
+ 
      
-            if(ano>=3000){
-                ano=3000;
+            if(Anio>=3000) //se mantiene hasta el anio 3000 si se ingresa un anio mas grande
+            {
+                Anio=3000;
             }
-            else if (ano<=2015){
-             ano=2015;
+            else if (Anio<=2015){ //como estamos en el 2015 una alarma no puede sonar antes de esa fecha
+             Anio=2015;
             }
-     pc.printf(" Dia=%d    Mes=%d   >Ano=%d    Hora=%d    Min=%d    Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+     pc.printf("Anno=  %d\n",Anio);
     
     
-    if(BotonENC.falling()){
-        goto set_hh;
+    if(Boton.falling())
+    {
+        goto set_Horas;
     }
-    if(Boton2.falling()){
-        goto set_comp;
-    }       
-            
+        
     }
-    set_hh:
-        while(1){
-         
-            hh=hh+wheel.getPulses();
+    set_Horas:
+        while(1)
+        {
+        
+        Horas=Horas+wheel.getPulses();
             wheel.reset();
-            ////////////////////////////////////////7 
+        
      
-            if(hh>=23){
-                hh=23;
+            if(Horas>=23)
+            {
+                Horas=23;
             }
-            else if (hh<=0){
-             hh=0;
+            else if (Horas<=0)
+            {
+             Horas=0;
             }
-     pc.printf(" Dia=%d    Mes=%d    Ano=%d   >Hora=%d    Min=%d    Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+     pc.printf(" Hora=%d\n ",Horas);
     
     
-    if(BotonENC.falling()){
-        goto set_mm;
+    if(Boton.falling()){
+        goto set_Minutos;
     }
-    if(Boton2.falling()){
-        goto set_comp;
-    }
-           
+            
             
     }
-     set_mm:
-        while(1){
+     set_Minutos:
+        while(1)
+        {
          
-            mm=mm+wheel.getPulses();
+            Minutos=Minutos+wheel.getPulses();
             wheel.reset();
-            ////////////////////////////////////////7 
+     
      
-            if(mm>=59){
-                mm=59;
+            if(Minutos>=59)
+            {
+                Minutos=59;
             }
-            else if (hh<=0){
-             mm=0;
+            else if (Horas<=0)
+            {
+             Minutos=0;
             }
-     pc.printf(" Dia=%d    Mes=%d    Ano=%d    Hora=%d   >Min=%d    Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+     pc.printf(" Min=%d\n ",Minutos);
     
     
-    if(BotonENC.falling()){
-        goto set_ss;
-    }
-    if(Boton2.falling()){
+    if(Boton.falling())
+    {
         goto set_comp;
     }
-           
-            
-    }
-    
-    set_ss:
-        while(1){
-         
-            ss=ss+wheel.getPulses();
-            wheel.reset();
-            ////////////////////////////////////////7 
-     
-            if(ss>=59){
-                ss=59;
-            }
-            else if (ss<=0){
-             ss=0;
-            }
-     pc.printf(" Dia=%d    Mes=%d    Ano=%d    Hora=%d    Min=%d   >Seg=%d\n ",dia,mes,ano,hh,mm,ss);
-    
-    
-    if(BotonENC.falling()){
-        goto set_dia;
-    }
-    if(Boton2.falling()){
-        goto set_comp;
-    }
-           
-            
+   
     }
     
     set_comp:
     
-    pc.printf("***Datos ingresados para alarma****\n");
-    pc.printf(" Dia=%d    Mes=%d    Ano=%d    Hora=%d    Min=%d    Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+    pc.printf("La alarma se programara el:   \n");
+    pc.printf(" Dia=%d    Mes=%d    Anio=%d    Hora=%d    Min=%d    Seg=%d\n ",Dia,Mes,Anio,Horas,Minutos,Segundos);
     
-    while(1){
+    while(1)
+    {
        rtc.getTime(tm);
-       pc.printf("The current time is : %02d:%02d:%02d\n", tm.hour, tm.min, tm.sec);
-       pc.printf("The current date is : %s, %02d/%02d/%04d\n", rtc.weekdayToString(tm.wday), tm.mon, tm.date, tm.year);
-    if(tm.hour>=hh && tm.min>=mm &&tm.sec>=ss && tm.mon>=mes && tm.date>=dia && tm.year>=ano){
-        myled=0;
+       pc.printf("La hora actual es:  %02d:%02d:%02d\n", tm.hour, tm.min, tm.sec);
+       pc.printf("La fecha actual es:  %02d/%02d/%04d\n", tm.mon, tm.date, tm.year);
+    if(tm.hour>=Horas && tm.min>=Minutos &&tm.sec>=Segundos && tm.mon>=Mes && tm.date>=Dia && tm.year>=Anio){
+        led1=0;
+        pc.printf("La alarma se ha activado:   \n");
     }
     else {
-        myled=1;
+        led1=1;
         }
         wait(1);
-    }
-     ////   
-     
-    }
-
+    } 
+    }
\ No newline at end of file