Reloj, se programan 7 alarmas para los días de la semana y se enciende y se apaga un aire acondicionado a determinada hora del día.

Dependencies:   Debounced QEI RTC-DS1307 TextLCD mbed

main.cpp

Committer:
PROCESADORES_2017_2
Date:
2017-11-30
Revision:
0:fdeda656e0e6

File content as of revision 0:fdeda656e0e6:

#include "mbed.h"
#include "DebouncedIn.h"
#include "TextLCD.h"
#include "QEI.h"
#include "Rtc_Ds1307.h"

Rtc_Ds1307 rtc(PTE0, PTE1);//SCL,SDA

TextLCD lcd(PTB8, PTB9,PTB10,PTB11,PTE2, PTE3); //Puertos LCD rs, e, d4, d5, d6, d7
QEI wheel(PTA1, PTA2, NC, 48);               //Puertos de la tarjeta asignados para el Encoder
DebouncedIn button_enco(PTC17);


DigitalOut led1(LED1);

DigitalOut led2(PTC0);



DigitalOut led(PTE31);//al transistor del led infrarrojo pin
int i2,j;
int t=0;
int H;
int L;

float periodo;
float Frecuencia=4000;
int C1=0x0C;
int m,k,x1,x2,x3,x4,x5,x6,x7;
int numero=1;
int Conf=0;  // 0: Alarmas y 1:Reloj
int Lugar=0; // Definen el punto en el cual va el programa
int i=0;
int visual=0;
int alarma=0; 
int code[65];
int cabeceraL=9000;
int cabeceraH=4555;
int Tdescanso=500;
int Tlow=630;
int Thigh=1750;
int numb = 33;
int duracion; //duracion de un pulso bajo
void pwr();
int descanso(int duracion);
int sendcode(int numb, int *code, int cabeceraL, int cabeceraH, int Tdescanso, int Tlow, int Thigh);
const unsigned pwrHY[]={575,548,548,575,549,549,575,548,1686,1686,1686,1687,1685,1687,549,1686,
                         1686,1686,576,548,549,576,549,549,576,549,1687,1687,1686,1686,1687,1686};
                         
 

int main()
{
    Rtc_Ds1307::Time_rtc tm = {};
    Rtc_Ds1307::Time_rtc Alarm = {};
    Rtc_Ds1307::Time_rtc Alarm2 = {};
    Rtc_Ds1307::Time_rtc Alarm3 = {};
    Rtc_Ds1307::Time_rtc Alarm4 = {};
    Rtc_Ds1307::Time_rtc Alarm5 = {};
    Rtc_Ds1307::Time_rtc Alarm6 = {};
    Rtc_Ds1307::Time_rtc Alarm7 = {};
    lcd.cls();
    lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD
       led1=1;

    

    while(1){
        switch (Lugar){
            case 0:     //Menu principal
                rtc.getTime(tm); //lee el tiempo del DS1307 
                lcd.locate(0,0);
                lcd.printf("%02d:%02d %02d/%02d/%02d", tm.hour, tm.min, tm.date, tm.mon, tm.year);
                lcd.locate(0,1);
                lcd.printf("-Alarmas  Setup");
                m=wheel.getPulses();  // m es el valor  del encoder
                
                if(m!=0 && Conf==0){
                    Conf=1;
                    wheel.reset();  
                    m=0;              
                }
                
                if(m!=0 && Conf==1){
                    Conf=0;
                    wheel.reset();  
                    m=0;              
                }
                
                if(Conf==0){
                    lcd.locate(9,1);
                    lcd.printf("-");                    
                    lcd.locate(0,1);
                    lcd.printf(">");
                    //lcd.locate(0,1);                    
                    wait(0.1);
                }
                if(Conf==1){
                    lcd.locate(0,1);
                    lcd.printf("-");                    
                    lcd.locate(9,1);
                    lcd.printf(">");
                    //lcd.locate(0,1);                    
                    wait(0.1);
                }
                if (button_enco.falling()){  //si se pulsa boton encoder
                    Lugar++;
                    m=0;       
                }
            break;
            
            case 1://Configurar
            
                switch(i){
                    case 0:
                       if(Conf==0){
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Config. Alarma ");
                            wait(1);
                            goto lazo;
sigo:                       i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Dia: ");
                            tm.date=1;                             
                        }
                       if(Conf==1){
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Config. hora   ");
                            wait(1);
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Dia: ");
                        }    
                    break;
                    
                    case 1:
                        m=wheel.getPulses();        
                        if (m!=0){
                            tm.date=tm.date+m;
                            if(tm.date>31){
                                tm.date=31;
                            }
                            if(tm.date<1){
                                tm.date=1;
                            }
                            wheel.reset();  
                            m=0;
                        }
                        lcd.locate(5,0);
                        lcd.printf("%02d",tm.date);
                        
                        if (button_enco.falling()){
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Mes: ");
                            //tm.mon=1;
                        }
                    break;
                    
                    case 2:
                        m=wheel.getPulses();        
                        if (m!=0){
                            tm.mon=tm.mon+m;
                            if(tm.mon>12){
                                tm.mon=12;
                            }
                            if(tm.mon<1){
                                tm.mon=1;
                            }
                            
                            wheel.reset();  
                            m=0;
                        }
                        lcd.locate(5,0);
                        lcd.printf("%02d",tm.mon);
                        
                        if (button_enco.falling()){
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Year: ");
                            
                        }
                    break;
                    
                    case 3:
                        m=wheel.getPulses();        
                        if (m!=0){
                            tm.year=tm.year+m;
                            
                            if(tm.year<2016){
                                tm.year=2016;
                            }
                            
                            wheel.reset();  
                            m=0;
                        }
                        lcd.locate(6,0);
                        lcd.printf("%02d",tm.year);
                        
                        if (button_enco.falling()){
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Hora: ");
                            
                        }
                    break;
                    
                     case 4:
                        m=wheel.getPulses();
                        if (m!=0){
                            tm.hour=tm.hour + m;
                            if(tm.hour>23){
                                tm.hour=23;
                            }
                            if(tm.hour<0){
                                tm.hour=0;
                            }
                            
                            wheel.reset();  
                            m=0;
                        }
                        lcd.locate(6,0);
                        lcd.printf("%02d",tm.hour);
                        
                        if (button_enco.falling()){
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Minuto: ");
                            //tm.min=0;
                        }
                    break;
                    
                    case 5:
                        m=wheel.getPulses();        
                        if (m!=0){
                            tm.min=tm.min+m;
                            if(tm.min>59){
                                tm.min=59;
                            }
                            if(tm.min<0){
                                tm.min=0;
                            }
                            wheel.reset();  
                            m=0;
                        }
                        lcd.locate(8,0);
                        lcd.printf("%02d",tm.min);
                        
                        if (button_enco.falling()){
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("dia semana: ");
                            
                        }
                    break;
                    
                    
                    case 6:
                        m=wheel.getPulses();        
                        if (m!=0){
                            tm.wday=tm.wday+m;
                            if(tm.wday>7){
                                tm.wday=7;
                            }
                            if(tm.wday<0){
                                tm.wday=1;
                            }
                            
                            wheel.reset();  
                            m=0;
                        }
                        lcd.locate(9,0);
                        lcd.printf("%02d",tm.wday);
                        
                        if (button_enco.falling()){
                          
                            ///////////////////
                            if(Conf==0){
                                if(numero ==1){
                                    Alarm=tm;
                                    x1=1;    
                                }
                                if(numero ==2){
                                    Alarm2=tm;
                                    x2=1;    
                                }
                                if(numero ==3){
                                    Alarm3=tm;    
                                    x3=1;
                                }
                                if(numero ==4){
                                    Alarm4=tm;    
                                    x4=1;
                                }
                                if(numero ==5){
                                    Alarm5=tm;    
                                    x5=1;
                                }
                                if(numero ==6){
                                    Alarm6=tm;    
                                    x6=1;
                                }
                                if(numero ==7){
                                    Alarm7=tm;    
                                    x7=1;
                                }
                                
                                lcd.cls();
                                lcd.locate(0,1);
                                lcd.printf("Alarma %02d config",numero);
                                wait(1);
                                i=0;
                                m=0;
                                Lugar++;
                                lcd.cls();
                            }
                
                            if(Conf==1){
                                rtc.setTime(tm, false, false);
                                rtc.startClock();
                                lcd.cls();
                                lcd.locate(0,0);
                                lcd.printf("Hora Config.  ");
                                wait(1);
                                i=0;
                                m=0;
                                Lugar=0;
                                rtc.getTime(tm); //lee el tiempo del DS1307 
                                lcd.locate(0,0);
                                lcd.printf("%02:",tm.hour);
                                lcd.locate(0,1);
                                lcd.printf("-Alarmas  -Set up");
                           } 
                        }
                    break;
                 }            
            break;
            
            case 2:     //Visualizar y Activacion Alarmas
                rtc.getTime(tm);
                m=wheel.getPulses();
                if(x1==1){
                 led2=1;
                    
                }
                if(x2==1){
                    led2=1;
                     
                }
                if(x3==1){
                 led2=1;
                  
                }
                if(x4==1){
                  led2=1;
                   
                }                
                if(x5==1){
                 led2=1;
                 
                }                
                 if(x6==1){
               
                led2=1;
                
                }                
                 
                 if(x7==1){
                 
                led2=1;
                }                
                
                                
                if(m!=0){
                   
                   visual=visual+m;
                   
                   if(visual<0){
                      visual=0;
                   }
                   if(visual>7){
                      visual=7;
                   }                    
                   wheel.reset();  
                   m=0;
                }
                //Visualizacion de la hora
                
                if(visual==0){
                   lcd.cls();
                   lcd.locate(0,0);    
                   lcd.printf(" Hora   %02d:%02d",tm.hour,tm.min);
                   lcd.locate(0,1);
                   lcd.printf("%02d/%02d/%s",tm.mon,tm.date,rtc.weekdayToString(tm.wday));
                   wait(0.1);
                }   
                //Visualilzacion de la alarma 1
                
                if(visual==1){
                   lcd.cls();
                   lcd.locate(0,0);    
                   lcd.printf(" Ver   %02d:%02d", Alarm.hour,Alarm.min);
                   lcd.locate(0,1);
                   lcd.printf("Alar1:%02d/%02d/%03d",Alarm.mon,Alarm.date,Alarm.wday);
                   wait(0.1);
                }
                //Visualilzacion de la alarma 2
                if(visual==2){
                   lcd.cls();
                   lcd.locate(0,0);    
                   lcd.printf(" Ver   %02d:%02d", Alarm2.hour, Alarm2.min);
                   lcd.locate(0,1);
                   lcd.printf("Alar2:%02d/%02d/%03d",Alarm2.mon,Alarm2.date,Alarm2.wday);
                   wait(0.1);
                }
                //Visualilzacion de la alarma 3
                if(visual==3){
                   lcd.cls();
                   lcd.locate(0,0);    
                   lcd.printf(" Ver   %02d:%02d", Alarm3.hour, Alarm3.min);
                   lcd.locate(0,1);
                   lcd.printf("Alar3:%02d/%02d/%03d",Alarm3.mon,Alarm3.date,Alarm3.wday);
                   wait(0.1);
                }
                //Visualilzacion de la alarma 4
                if(visual==4){
                   lcd.cls();
                   lcd.locate(0,0);    
                   lcd.printf(" Ver   %02d:%02d", Alarm4.hour, Alarm4.min);
                   lcd.locate(0,1);
                   lcd.printf("Alar4:%02d/%02d/%03d",Alarm4.mon,Alarm4.date,Alarm4.wday);
                   wait(0.1);
                }
                //Visualilzacion de la alarma 5
                 if(visual==5){
                   lcd.cls();
                   lcd.locate(0,0);    
                   lcd.printf(" Ver   %02d:%02d", Alarm5.hour, Alarm5.min);
                   lcd.locate(0,1);
                   lcd.printf("Alar5:%02d/%02d/%03d",Alarm5.mon,Alarm5.date,Alarm5.wday);
                   wait(0.1);
                }    
                //Visualilzacion de la alarma 6
                if(visual==6){
                   lcd.cls();
                   lcd.locate(0,0);    
                   lcd.printf(" Ver   %02d:%02d", Alarm6.hour, Alarm6.min);
                   lcd.locate(0,1);
                   lcd.printf("Alar6:%02d/%02d/%03d",Alarm6.mon,Alarm6.date,Alarm6.wday);
                   wait(0.1);
                }    
                
                //Visualilzacion de la alarma 7
                if(visual==7){
                   lcd.cls();
                   lcd.locate(0,0);    
                   lcd.printf(" Ver   %02d:%02d", Alarm7.hour, Alarm7.min);
                   lcd.locate(0,1);
                   lcd.printf("Alar7:%02d/%02d/%03d",Alarm7.mon,Alarm7.date,Alarm7.wday);
                   wait(0.1);
                }    
                //PARA CONFIGURAR OTRA ALARMA:
                
                if (button_enco.falling()){
                    Lugar=1;
                }
                
                
                //Activacion  Alarma1               
                if((tm.date==Alarm.date)&&(tm.mon==Alarm.mon)&&(tm.hour==Alarm.hour)&&(tm.min==Alarm.min)&&(tm.wday==Alarm.wday))
                {
                    Lugar++;
                    lcd.cls();
                    lcd.locate(0,0);                        
                    lcd.printf("Alarma 1: Activa!!");
                    led1=0;
                    pwr();
                    m=0;
                    k=1;
                }

                //Activacion  Alarma2               
                if((tm.date==Alarm2.date)&&(tm.mon==Alarm2.mon)&&(tm.hour==Alarm2.hour)&&(tm.min==Alarm2.min)&&(tm.wday==Alarm2.wday))
                {
                    Lugar++;
                    lcd.cls();
                    lcd.locate(0,0);                        
                    lcd.printf("Alarma 2:  Activa!!");
                    led1=0;
                    pwr();
                    m=0;
                    k=2;
                }

                //Activacion  Alarma3               
                if((tm.date==Alarm3.date)&&(tm.mon==Alarm3.mon)&&(tm.hour==Alarm3.hour)&&(tm.min==Alarm3.min)&&(tm.wday==Alarm3.wday))
                {
                    Lugar++;
                    lcd.cls();
                    lcd.locate(0,0);                        
                    lcd.printf("Alarma3: Activa!!");
                    led1=0;
                    pwr();
                    m=0;
                    k=3;
                }
                //Activacion  Alarma4               
                if((tm.date==Alarm4.date)&&(tm.mon==Alarm4.mon)&&(tm.hour==Alarm4.hour)&&(tm.min==Alarm4.min)&&(tm.wday==Alarm4.wday))
                {
                    Lugar++;
                    lcd.cls();
                    lcd.locate(0,0);                        
                    lcd.printf("Alarma 4 :Activa!!");
                    led1=0;
                    pwr();
                    m=0;
                    k=4;
                }
                 //Activacion  Alarma5               
                if((tm.date==Alarm5.date)&&(tm.mon==Alarm5.mon)&&(tm.hour==Alarm5.hour)&&(tm.min==Alarm5.min)&&(tm.wday==Alarm5.wday))
                {
                    Lugar++;
                    lcd.cls();
                    lcd.locate(0,0);                        
                    lcd.printf("Alarma 5: Activa!!");
                    led1=0;
                    pwr();
                    m=0;
                    k=5;
                }
                //Activacion  Alarma6               
                if((tm.date==Alarm6.date)&&(tm.mon==Alarm6.mon)&&(tm.hour==Alarm6.hour)&&(tm.min==Alarm6.min)&&(tm.wday==Alarm6.wday))
                {
                    Lugar++;
                    lcd.cls();
                    lcd.locate(0,0);                        
                    lcd.printf("Alarma 6: Activa!!");
                    led1=0;
                    pwr();
                    m=0;
                    k=6;
                }
                //Activacion  Alarma7               
                if((tm.date==Alarm7.date)&&(tm.mon==Alarm7.mon)&&(tm.hour==Alarm7.hour)&&(tm.min==Alarm7.min)&&(tm.wday==Alarm7.wday))
                {
                    Lugar++;
                    lcd.cls();
                    lcd.locate(0,0);                        
                    lcd.printf("Alarma 7:Activa!!");
                    led1=0;
                    pwr();
                    m=0;
                    k=7;
                }
            break;
            
            case 3:
                    
                    lcd.locate(0,0);                        
                    lcd.printf("  AIRE ENCENDIDO  ");
                    //Configura la hora de apagado y los minutos
                    H=9;
                    L=0;
                    if (k==1){
                        rtc.getTime(tm);
                        if((tm.hour==H)&&(tm.min==L))
                        {led1=1;
                        pwr();
                        lcd.locate(0,0); 
                        Lugar=2;                       
                        }
                        }
                    if (k==2){
                        rtc.getTime(tm);
                        if((tm.hour==H)&&(tm.min==L))
                        {led1=1;
                        pwr();
                        lcd.locate(0,0);                        
                        Lugar=2;
                        }
                        }
                    if (k==3){
                        rtc.getTime(tm);
                        if((tm.hour==H)&&(tm.min==L))
                        {led1=1;
                        pwr();
                        lcd.locate(0,0);                        
                        Lugar=2;
                        }
                        }
                    if (k==4){
                        rtc.getTime(tm);
                        if(((tm.hour==H)&&(tm.min==L)))
                        {led1=1;
                        pwr();
                        lcd.locate(0,0);                        
                        Lugar=2;
                        }
                        }
                    if (k==5){
                        rtc.getTime(tm);
                        if((tm.hour==H)&&(tm.min==L))
                        {led1=1;
                        pwr();
                        lcd.locate(0,0);                        
                        Lugar=2;
                        }
                        }
                        if (k==6){
                        rtc.getTime(tm);
                        //(tm.min==(Alarm6.min+1))
                        if((tm.hour==H)&&(tm.min==L))
                        {led1=1;
                        pwr();
                        lcd.locate(0,0);                        
                        Lugar=2;
                        }
                        }
                        if (k==7){
                        rtc.getTime(tm);
                        if((tm.hour==H)&&(tm.min==L))
                        {led1=1;
                        pwr();
                        lcd.locate(0,0);                        
                        Lugar=2;
                        }
                        }    
            break;
            }
            
            }
// Lazo de Config Alarmas   
lazo:   int t=0;
        numero=1;
        while(t==0){    
            m=wheel.getPulses(); 
            lcd.locate(0,0);
            
            lcd.printf("-1  -2   -3  -4",tm.date);
            lcd.locate(0,1);
            lcd.printf("-5  -6   -7 ",tm.date);
            
            if (m!=0){
                numero=numero+m;
                if(numero<1){
                    numero=1;
                    }
                if(numero>7){
                    numero=7;
                    }
                wheel.reset();  
                    m=0;
                    }
                if(numero==1){
                    
                    lcd.locate(0,0);
                    lcd.printf(">");                    
                    lcd.locate(4,0);
                    lcd.printf("-");
                    lcd.locate(9,0);                    
                    lcd.printf("-");
                    lcd.locate(13,0);                    
                    lcd.printf("-");
                    lcd.locate(0,1);
                    lcd.printf("-");                    
                    lcd.locate(4,1);
                    lcd.printf("-");
                    lcd.locate(9,1);                    
                    lcd.printf("-");
                    wait(0.1);    
                    }
                if(numero==2){
                    
                    lcd.locate(0,0);
                    lcd.printf("-");                    
                    lcd.locate(4,0);
                    lcd.printf(">");
                    lcd.locate(9,0);                    
                    lcd.printf("-");
                    lcd.locate(13,0);                    
                    lcd.printf("-");
                    lcd.locate(0,1);
                    lcd.printf("-");                    
                    lcd.locate(4,1);
                    lcd.printf("-");
                    lcd.locate(9,1);                    
                    lcd.printf("-");                  
                    wait(0.1);
                    }
                if(numero==3){         
                    
                    lcd.locate(0,0);
                    lcd.printf("-");                    
                    lcd.locate(4,0);
                    lcd.printf("-");
                    lcd.locate(9,0);                    
                    lcd.printf(">");
                    lcd.locate(13,0);                    
                    lcd.printf("-");
                    lcd.locate(0,1);
                    lcd.printf("-");                    
                    lcd.locate(4,1);
                    lcd.printf("-");
                    lcd.locate(9,1);                    
                    lcd.printf("-");                  
                    wait(0.1);    
                    }
                if(numero==4){
                    
                    lcd.locate(0,0);
                    lcd.printf("-");                    
                    lcd.locate(4,0);
                    lcd.printf("-");
                    lcd.locate(9,0);                    
                    lcd.printf("-");
                    lcd.locate(13,0);                    
                    lcd.printf(">");
                    lcd.locate(0,1);
                    lcd.printf("-");                    
                    lcd.locate(4,1);
                    lcd.printf("-");
                    lcd.locate(9,1);                    
                    lcd.printf("-");                  
                    wait(0.1);
                    }
                     
                if(numero==5){
                   
                    lcd.locate(0,0);
                    lcd.printf("-");                    
                    lcd.locate(4,0);
                    lcd.printf("-");
                    lcd.locate(9,0);                    
                    lcd.printf("-");
                    lcd.locate(13,0);                    
                    lcd.printf("-");   
                    lcd.locate(0,1);
                    lcd.printf(">");                    
                    lcd.locate(4,1);
                    lcd.printf("-");
                    lcd.locate(9,1);                    
                    lcd.printf("-");
                    
                    wait(0.1);    
                    }   
                if(numero==6){
                    
                    lcd.locate(0,0);
                    lcd.printf("-");                    
                    lcd.locate(4,0);
                    lcd.printf("-");
                    lcd.locate(9,0);                    
                    lcd.printf("-");
                    lcd.locate(13,0);                    
                    lcd.printf("-");   
                    lcd.locate(0,1);
                    lcd.printf("-");                    
                    lcd.locate(4,1);
                    lcd.printf(">");
                    lcd.locate(9,1);                    
                    lcd.printf("-");
                                     
                    wait(0.1);
                    }    
                     if(numero==7){
                    
                    lcd.locate(0,0);
                    lcd.printf("-");                    
                    lcd.locate(4,0);
                    lcd.printf("-");
                    lcd.locate(9,0);                    
                    lcd.printf("-");
                    lcd.locate(13,0);                    
                    lcd.printf("-");   
                    lcd.locate(0,1);
                    lcd.printf("-");                    
                    lcd.locate(4,1);
                    lcd.printf("-");
                    lcd.locate(9,1);                    
                    lcd.printf(">");
                                      
                    wait(0.1);
                    }    
                                   
                    
                    
                    
                    
                if (button_enco.falling()){
                    m=0;
                    lcd.cls();
                    lcd.locate(4,0);                        
                    lcd.printf("Alarma %02d ",numero);
                    lcd.locate(2,1);                        
                    lcd.printf("Seleccionada");
                    wait(1.5);
                    lcd.cls();
                    t=1;
                    }
        }       
        goto sigo;

}   
int descanso(int duracion){
     Timer t2;
     t2.reset();
     t2.start();
     while(1) 
     {
      led=1;
      wait_us(8);
      led=0;
      led=0;
      wait_us(9);
      if(t2.read_us() > duracion) return 0;
          
      } 
      }   
                     
int sendcode(int numb, int *code, int cabeceraL, int cabeceraH, int Tdescanso, int Tlow, int Thigh)
{
      led=0;
      wait_ms(100);
      descanso(cabeceraL);
      wait_us(cabeceraH);
      
      for(i2=0;i2<numb;i2++){
          if (code[i2]>1000){
              descanso(Tdescanso);
              wait_us(Thigh);
              }
         if (code[i2]<1000){
              descanso(Tdescanso);
              wait_us(Tlow);
              }     
                            }
              descanso(Tdescanso);                
              return 0;
}        
//**********************************************************
void pwr(){
  for(i2=0;i2<numb;i2++){
       code[i2]=pwrHY[i2];
       }   
sendcode(numb,code,cabeceraL, cabeceraH, Tdescanso, Tlow, Thigh);
}
//****************************