Tarea Reloj DS3231-Control Remoto samsung Procesadores 2019-1 Equipo verde Guillermo Eduardo Luna Rozo Susana Buriticá Alvaro Javier Vargas David Cordoba Santiago Yepes Funciona correctamente, con 8 alarmas, se puede modificar la hora ccon encoder, y se configuran las alarmas con encoder y control.

Dependencies:   mbed QEI Debounced ds3231 TextLCD RTC-DS1307 Pulse1

main.cpp

Committer:
gelunar
Date:
2019-08-28
Revision:
1:0075319c0341
Parent:
0:fd394f5c0449

File content as of revision 1:0075319c0341:

//Tarea Reloh DS3231-Control Remoto samsung Procesadores 2019-1 Equipo verde
//Guillermo Eduardo Luna Rozo
//Susana Buriticá
//Alvaro Javier Vargas
//David Cordoba
//Santiago Yepes
//Funciona correctamente, con 8 alarmas, se puede modificar la hora ccon encoder, y se configuran las alarmas con encoder y control

#include "mbed.h"
#include "QEI.h"
#include "TextLCD.h"
#include "DebouncedIn.h"
#include "string.h"
#include "ds3231.h"
#include <Pulse1.h>
#include "Rtc_Ds1307.h"
 
#define ESC 0x1B
 
 
//Use X4 encoding.
//QEI wheel(p29, p30, NC, 624, QEI::X4_ENCODING);
//Use X2 encoding by default.
PulseInOut irda(PTD5);// en este puerto se pone el sensor infrarrojo
Serial pc(USBTX, USBRX);
QEI wheel (PTD7, PTD6, NC, 50);//Encoder
DebouncedIn bot1(PTC5);//Botón encoder
DebouncedIn boton(PTA17);//Botón
DebouncedIn boton2(PTA16);//Botón configurar alarma
TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5, TextLCD::LCD16x2); // rs, e, d4-d7
DigitalOut led1(PTC7);//Salidas
DigitalOut led2(PTC0);
DigitalOut led3(PTC3);
DigitalOut led(LED3);
DigitalOut ledd2(LED1);
 
//-----------parametros IRDA -------
int header =0; //tiempo de cabecera pulso abajo
const int head_H = 4900; //+20% medida con osciloscopio en microsegundos
const int head_L = 3500 ;//-20%  medida con osciloscopio
int il=0, x=0;
const int T_alto=1694;//ponga su tiempo de la prueba
const int T_bajo=632;//ponga su tiempo de la prueba
const int num_bits = 32;//ponga su numero de bits
int num[num_bits];//cadena para almacenar todos los tiempos que conforman los bits de datos
char sel[num_bits];//cadena para almacenar la cadena codificada en binario
char uno[]="1110000011100000001000001101111"; //0,0,1,0,0,0,0,0,1,1,0,1,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,1,0,1,1,0
char dos[]="1110000011100000101000000101111";
char tres[]="1110000011100000011000001001111";
char cuatro[]="1110000011100000000100001110111";
char cinco[]="1110000011100000100100000110111";
char seis[]="1110000011100000010100001010111";
char siete[]="1110000011100000001100001100111";
char ocho[]="1110000011100000101100000100111";
char nueve[]="1110000011100000011100001000111";
char cero[]="1110000011100000100010000111011";
             
char ok[]="1110000011100000000101101110100"; //"00100000110111110000000011111110"; //0,0,1,0,0,0,0,0,1,1,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0
char izq[]="1110000011100000101001100101100";//"00100000110111110010001011011100  
char der[]="1110000011100000010001101011100";
int dato; // tiempo de cada dato que se lee
int irdan(void);
int y=0;
int detectar(void);//funcion detecta irda
int Alarm_IRDA(void);//funcion valor irda
int ConfiReloj(void);  //configurarrelogsinno
 
void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member);
void get_user_input(char* message, uint8_t min, uint8_t max, bool* member);
 
int mode=0;
int numb=34;
 
//
  
int d;
int m;
int mes;
int an,h,min,sec,a; 
int Conf=0;
int s=0;  // 0: si y 1:no alarma ok
int f=0; // 0: si y 1:no otra alrama
int Lugar=1,Lugar2=2,Lugar3=3;
int pos=0,n=0; // Definen el punto en el cual va el programa
int i=0;
int j=1;      //Cuenta las alarmas
int Select=1;   //Alarma Seleccionada
int StarReadIR=0;
int visual=0;  //0: Reloj y 1:Alarm
int alarma=0;  //0: Desactivar y 1:Posponer
int Vec1[7];
int Vec2[7];
int Vec3[7];
int Vec4[7];
int Vec5[7];
int Vec6[7];
Rtc_Ds1307 rtc(PTE0, PTE1);//SCL,SDA
Rtc_Ds1307::Time_rtc tm = {};
time_t epoch_time; 
 
 
int main()
{
 
    Ds3231 rtc(PTE0, PTE1); 
    Rtc_Ds1307::Time_rtc tm = {};
    time_t epoch_time;
    ds3231_cntl_stat_t rtc_control_status = {0,0}; 
    ds3231_time_t rtc_time={0,0,0,0,0};
    ds3231_calendar_t rtc_calendar={0,0,0,0};
    rtc.set_cntl_stat_reg(rtc_control_status);
    
    
    
loop1: 
    epoch_time = rtc.get_epoch();
    rtc.get_time(&rtc_time);
    rtc.get_calendar(&rtc_calendar);
    j=1;
    Conf=0;
    s=0;
    f=0;
    Lugar=1;
    Lugar2=2;
    Lugar3=3;
    pos=0;
    i=0;
    y=0;
    lcd.cls();
    lcd.locate(14,0);
    lcd.printf("-A");
    lcd.locate(2,0);
    lcd.printf("\nFecha:%02d/%02d/%02d",rtc_calendar.date,rtc_calendar.month,rtc_calendar.year);
    m=wheel.getPulses();
 //Reloj corriendo y comparamdo con alarmas//       
     while(!bot1.falling() && !boton.falling() && !boton2.falling()){
            rtc.get_time(&rtc_time);
            lcd.locate(0,1);
            lcd.printf("\nHora:%02d:%02d:%02d", rtc_time.hours,rtc_time.minutes,rtc_time.seconds );
            if(Vec1[1]==rtc_calendar.date && Vec1[2]==rtc_calendar.month && Vec1[3]==rtc_calendar.year && Vec1[4]==rtc_time.hours && Vec1[5]==rtc_time.minutes && Vec1[6]==rtc_time.seconds){
                    led1=1;}
            if(Vec2[1]==rtc_calendar.date && Vec2[2]==rtc_calendar.month && Vec2[3]==rtc_calendar.year && Vec2[4]==rtc_time.hours && Vec2[5]==rtc_time.minutes && Vec2[6]==rtc_time.seconds){
                    led2=1;}
            if(Vec3[1]==rtc_calendar.date && Vec3[2]==rtc_calendar.month && Vec3[3]==rtc_calendar.year && Vec3[4]==rtc_time.hours && Vec3[5]==rtc_time.minutes && Vec3[6]==rtc_time.seconds){
                    led3=1;}
            if(Vec4[1]==rtc_calendar.date && Vec4[2]==rtc_calendar.month && Vec4[3]==rtc_calendar.year && Vec4[4]==rtc_time.hours && Vec4[5]==rtc_time.minutes && Vec4[6]==rtc_time.seconds){
                    led1=0;}
            if(Vec5[1]==rtc_calendar.date && Vec5[2]==rtc_calendar.month && Vec5[3]==rtc_calendar.year && Vec5[4]==rtc_time.hours && Vec5[5]==rtc_time.minutes && Vec5[6]==rtc_time.seconds){
                    led2=0;}
            if(Vec6[1]==rtc_calendar.date && Vec6[2]==rtc_calendar.month && Vec6[3]==rtc_calendar.year && Vec6[4]==rtc_time.hours && Vec6[5]==rtc_time.minutes && Vec6[6]==rtc_time.seconds){
                    led3=0;}  
            }
                
    while(1) 
            {
            
            if (bot1.falling()){ //Para que trabaje con encoder
                Lugar++;
                }
            if(boton.falling()){
                Lugar2++;
                }
            if(boton2.falling()){
                Lugar3++;
                } 
            switch (Lugar)//Encoder
               {
               case 2:
               switch(i)
                  {
                   case 0:
                   if(Conf==0){
                    lcd.cls();   
                    lcd.locate(0,0);
                    lcd.printf("Alarma: ");
                    i++;
                    }
                   break;
                   case 1:
                   m=0;
                   m=wheel.getPulses();
                   if (m!=0){
                    a=m;
                    if(a>9){
                        a=8;
                        }
                    if (a<1){
                        a=1;
                        }
                    }
                   lcd.locate(10,0);
                   lcd.printf("%02d   ",a);
                   if(bot1.falling()){
loop2:
                    lcd.cls();
                    lcd.printf("Config. Alarmas ");
                    wait(1);
                    lcd.cls();
                    i=2;
                    s=0;
                    f=0;
                    Lugar=2;
                    pos=0;
                    lcd.locate(0,0);
                    lcd.printf("Dia: ");
                    m=0;
                    wheel.reset();
                    }
                    break;
                    case 2: // Dia
                    m=0;
                    m=wheel.getPulses();        
                    if (m!=0) 
                        {
                        d=m;
                        if(d>31)
                            {
                            d=31;
                            }
                        if(d<1)
                            {
                            d=1;
                            }
                            
                            
                        }
                        lcd.locate(5,0);
                        lcd.printf("%02d   ",d);
                        
                        if (bot1.falling())
                        {
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Mes: ");
                                              
                            wheel.reset();
                    
                        }
                    break;
                    
                    case 3: // Mes
                        m=0;
                        m=wheel.getPulses();        
                        if (m!=0) 
                        {
                            mes=m;
                            if(mes>12)
                            {
                                mes=12;
                            }
                            if(mes<1)
                            {
                                mes=1;
                            }
                            
                            
                        }
                        lcd.locate(7,0);
                        lcd.printf("%02d",mes);
                        
                        if (bot1.falling())
                        {
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Anno: ");
                            wheel.reset();
                        }
                    
                    break;
                    
                    case 4: //Año
                        m=wheel.getPulses();        
                        if (m!=0) 
                        {
                            an=m;
                            
                            if(an<0)
                            {
                                an=0;
                            }
                            if(an>99){
                                an=99;
                                }
                              
                            
                        }
                        lcd.locate(6,0);
                        lcd.printf("%02d",an);
                        
                        if (bot1.falling())
                        {
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                        lcd.printf("Config. Hora");
                        wait(1);
                        lcd.cls();
                        lcd.printf("Hora: ");
                        wheel.reset();
                                                    
                        }
                    
                    break;
                    
                    case 5: //HORA
                    
 
                        m=0;
                        m=wheel.getPulses();       
                        if (m!=0) 
                        {
                            h= m;
                            if(h>23)
                            {
                                h=23;
                            }
                            if(h<0)
                            {
                                h=0;
                            }  
                            
                        }
                        lcd.locate(6,0);
                        lcd.printf("%02d",h);
                        
                        if (bot1.falling())
                        {
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Minuto: ");
                            wheel.reset();
                        }
                    
                    break;
                    
                    case 6: //MINUTOS
                        m=0;
                        m=wheel.getPulses();        
                        if (m!=0) 
                        {
                            min=m;
                            if(min>59)
                            {
                                min=59;
                            }
                            if(min<0)
                            {
                                min=0;
                            }  
                            
                        }
                        lcd.locate(8,0);
                        lcd.printf("%02d",min);
                        
                        if (bot1.falling())
                        {
                            i++;
                            
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Segundo: ");
                            wheel.reset();
                        }
                    
                    break;
                    
                    
                    case 7: //SEGUNDOS
                    m=0;
                        m=wheel.getPulses();        
                        if (m!=0) 
                        {
                            sec=m;
                            if(sec>59)
                            {
                                sec=59;
                            }
                            if(sec<0)
                            {
                                sec=0;
                            }
 
                        }
                        
                        lcd.locate(8,0);
                        lcd.printf("%02d",sec);
                        if(bot1.falling()){
                            
                            i++;
                            
                            lcd.cls();
                            wheel.reset();
                    
                        }
                    break;
                    case 8:
                        m=0;
                        lcd.locate(0,1);
                        lcd.printf("\nAlarm:%02d:%02d:%02d",h,min,sec);
                        lcd.locate(2,0);
                        lcd.printf("\nFecha:%02d/%02d/%02d",d,mes,an);
                        wait(2);
                        lcd.cls();
                        lcd.locate(0,1);
                        lcd.printf("Alarma OK ?");
                        lcd.locate(2,0);
                        lcd.printf("--Si      --No");
                        i++;
                        wheel.reset();
                break;
        
            case 9:
                
                m=wheel.getPulses();  // m son los datos del encoder
                if(m!=0 && s==0){
                    s=1;
                    wheel.reset();  
                    m=0;              
                }
                
                if(m!=0 && s==1){
                    s=0;
                    wheel.reset();  
                    m=0;              
                }
                
                if(s==0){
                    
                    lcd.locate(11,0);
                    lcd.printf("-");                    
                    lcd.locate(1,0);
                    lcd.printf(">");
                    lcd.locate(0,1);                    
                    wait(0.1);
                }
                 if(s==1){
                    
                    lcd.locate(1,0);
                    lcd.printf("-");                    
                    lcd.locate(11,0);
                    lcd.printf(">");
                    lcd.locate(0,1);                    
                    wait(0.1);
                    }
                
                 if(s==1){
                    
                    lcd.locate(1,0);
                    lcd.printf("-");                    
                    lcd.locate(11,0);
                    lcd.printf(">");
                    lcd.locate(0,1);                    
                    wait(0.1);
                }
                
                if (bot1.falling()){  //si se pulsa boton encoder
                    pos++;
                    m=0;       
                }
            
            break;
            
                       
                     
                }
                switch (pos)
                {
                    case 1:
                        if(s==0){
                            lcd.cls();
                        lcd.locate(0,1);
                        if(a==1){
                        Vec1[1]=d;
                        Vec1[2]=mes;
                        Vec1[3]=an;
                        Vec1[4]=h;
                        Vec1[5]=min;
                        Vec1[6]=sec;
                            }
                        if(a==2){
                        Vec2[1]=d;
                        Vec2[2]=mes;
                        Vec2[3]=an;
                        Vec2[4]=h;
                        Vec2[5]=min;
                        Vec2[6]=sec;
                            }
                        if(a==3){
                        Vec3[1]=d;
                        Vec3[2]=mes;
                        Vec3[3]=an;
                        Vec3[4]=h;
                        Vec3[5]=min;
                        Vec3[6]=sec;
                            }
                        if(a==4){
                        Vec4[1]=d;
                        Vec4[2]=mes;
                        Vec4[3]=an;
                        Vec4[4]=h;
                        Vec4[5]=min;
                        Vec4[6]=sec;
                            }
                        if(a==5){
                        Vec5[1]=d;
                        Vec5[2]=mes;
                        Vec5[3]=an;
                        Vec5[4]=h;
                        Vec5[5]=min;
                        Vec5[6]=sec;
                            }
                        if(a==6){
                        Vec6[1]=d;
                        Vec6[2]=mes;
                        Vec6[3]=an;
                        Vec6[4]=h;
                        Vec6[5]=min;
                        Vec6[6]=sec;
                            }
                        lcd.printf("Otra alarma? %0.2d",Vec1[6]);
                        lcd.locate(2,0);
                        lcd.printf("--Si      --No");
                        wheel.reset();
                        pos++;
                        }
                        
                        if(s==1){
                            lcd.cls();
                            goto loop2;
                            }
                            
                            break;
                    case 2:
                        m=wheel.getPulses();  // m son los datos del encoder
                if(m!=0 && f==0){
                    f=1;
                    wheel.reset();  
                    m=0;              
                }
                
                if(m!=0 && f==1){
                    f=0;
                    wheel.reset();  
                    m=0;              
                }
                
                if(f==0){
                    
                    lcd.locate(11,0);
                    lcd.printf("-");                    
                    lcd.locate(1,0);
                    lcd.printf(">");
                    lcd.locate(0,1);                    
                    wait(0.1);
                }
                 if(f==1){
                    
                    lcd.locate(1,0);
                    lcd.printf("-");                    
                    lcd.locate(11,0);
                    lcd.printf(">");
                    lcd.locate(0,1);                    
                    wait(0.1);
                    }
                
                 if(f==1){
                    
                    lcd.locate(1,0);
                    lcd.printf("-");                    
                    lcd.locate(11,0);
                    lcd.printf(">");
                    lcd.locate(0,1);                    
                    wait(0.1);
                }
                
                if (bot1.falling()){  //si se pulsa boton encoder
                   wheel.reset();
                    lcd.cls(); 
                    pos++;
                    
                
                          
                }
                break;
                    case 3:
                    if(f==0){
                        wheel.reset();
                        lcd.cls();
                        goto loop1;
                        }
                    if(f==1){
                        wheel.reset();
                        lcd.cls();
                        goto loop1; //Va al reloj
                        }
                    break;
                    
                    }
               
        }
        
          switch (Lugar2)//Control
                {
                case 3:
            
               switch(i)
                  {
                   
                   case 0: 
                   if(Conf==0){
loop3:
                    lcd.cls();   
                    lcd.locate(0,0);
                    lcd.printf("Alarma: ");
                    i++;
                    Conf++;
                    m=0;
                    }
                   
                   case 1:
                   if (Conf==1){
                   
                   m=Alarm_IRDA();
                   if(m!=0){
                   a=m;
                   if(a>8){
                        a=8;
                        }
                    if (a<1){
                        a=1;
                        }
                   }
                   lcd.locate(10,0);
                   pc.printf("Alarma: %02d",a);
                   lcd.printf("%02d",a);
                   
loop5:
                    if(Alarm_IRDA()==-8){
                    lcd.cls();
                    lcd.printf("Config. Alarmas ");
                    wait(1);
                    lcd.cls();
                    i=2;
                    s=0;
                    f=0;
                    Lugar=2;
                    pos=0;
                    lcd.locate(0,0);
                    lcd.printf("Dia: ");
                    m=0;
                    }
                    }
                    
                    case 2: // Dia
                    m=0;
                    m=Alarm_IRDA();        
                    if (m!=0) 
                        {
                        d=m;
                        if(d>31)
                            {
                            d=31;
                            }
                        if(d<1)
                            {
                            d=1;
                            }
                            
                            
                        }
                        lcd.locate(5,0);
                        lcd.printf("%02d   ",d);
                        
                        if (Alarm_IRDA()==-8)
                        {
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Mes: ");
                                              
                            
                    
                        }
                    
                    
                    case 3: // Mes
                        m=0;
                        m=Alarm_IRDA();        
                        if (m!=0) 
                        {
                            mes=m;
                            if(mes>12)
                            {
                                mes=12;
                            }
                            if(mes<1)
                            {
                                mes=1;
                            }
                            
                            
                        }
                        lcd.locate(7,0);
                        lcd.printf("%02d",mes);
                        
                        if (Alarm_IRDA()==-8)
                        {
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Anno: ");
                            
                        }
                    
                    
                    
                    case 4: //Año
                        m=Alarm_IRDA();        
                        if (m!=0) 
                        {
                            an=m;
                            
                            if(an<0)
                            {
                                an=0;
                            }
                            if(an>99){
                                an=99;
                                }
                              
                            
                        }
                        lcd.locate(6,0);
                        lcd.printf("%02d",an);
                        
                        if (Alarm_IRDA()==-8)
                        {
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                        lcd.printf("Config. Hora");
                        wait(1);
                        lcd.cls();
                        lcd.printf("Hora: ");
                        
                                                    
                        }
                    
                    
                    
                    case 5: //HORA
                    
 
                        m=0;
                        m=Alarm_IRDA();       
                        if (m!=0) 
                        {
                            h= m;
                            if(h>23)
                            {
                                h=23;
                            }
                            if(h<0)
                            {
                                h=0;
                            }  
                            
                        }
                        lcd.locate(6,0);
                        lcd.printf("%02d",h);
                        
                        if (Alarm_IRDA()==-8)
                        {
                            i++;
                            m=0;
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Minuto: ");
                            
                        }
                    
                    
                    
                    case 6: //MINUTOS
                        m=0;
                        m=Alarm_IRDA();        
                        if (m!=0) 
                        {
                            min=m;
                            if(min>59)
                            {
                                min=59;
                            }
                            if(min<0)
                            {
                                min=0;
                            }  
                            
                        }
                        lcd.locate(8,0);
                        lcd.printf("%02d",min);
                        
                        if (Alarm_IRDA()==-8)
                        {
                            i++;
                            
                            lcd.cls();
                            lcd.locate(0,0);
                            lcd.printf("Segundo: ");
                            
                        }
                    
                    
                    
                    
                    case 7: //SEGUNDOS
                    m=0;
                        m=Alarm_IRDA();        
                        if (m!=0) 
                        {
                            sec=m;
                            if(sec>59)
                            {
                                sec=59;
                            }
                            if(sec<0)
                            {
                                sec=0;
                            }
 
                        }
                        
                        lcd.locate(8,0);
                        lcd.printf("%02d",sec);
                        if(Alarm_IRDA()==-8){
                            
                            i++;
                            
                            lcd.cls();
                            
                    
                        }
                    
                    case 8:
                        m=0;
                        lcd.locate(0,1);
                        lcd.printf("\nAlarm:%02d:%02d:%02d",h,min,sec);
                        lcd.locate(2,0);
                        lcd.printf("\nFecha:%02d/%02d/%02d",d,mes,an);
                        wait(2.5);
                        lcd.cls();
                        lcd.locate(0,1);
                        lcd.printf("Alarma OK ?");
                        lcd.locate(2,0);
                        lcd.printf("--Si      --No");
                        i++;
                        
                
        
            case 9:
                
                m=Alarm_IRDA();  // 
               if(m==-2){
                goto loop5;
                
                }
                if (m==-1){  //
                    pos++;
                          
                }
                m=0;           
        
            
                       
                     
                }
                switch (pos)
                {
                    case 1:
                        if(s==0){
                            lcd.cls();
                        lcd.locate(0,1);
                        if(a==1){
                        Vec1[1]=d;
                        Vec1[2]=mes;
                        Vec1[3]=an;
                        Vec1[4]=h;
                        Vec1[5]=min;
                        Vec1[6]=sec;
                            }
                        if(a==2){
                        Vec2[1]=d;
                        Vec2[2]=mes;
                        Vec2[3]=an;
                        Vec2[4]=h;
                        Vec2[5]=min;
                        Vec2[6]=sec;
                            }
                        if(a==3){
                        Vec3[1]=d;
                        Vec3[2]=mes;
                        Vec3[3]=an;
                        Vec3[4]=h;
                        Vec3[5]=min;
                        Vec3[6]=sec;
                            }
                        if(a==4){
                        Vec4[1]=d;
                        Vec4[2]=mes;
                        Vec4[3]=an;
                        Vec4[4]=h;
                        Vec4[5]=min;
                        Vec4[6]=sec;
                            }
                        if(a==5){
                        Vec5[1]=d;
                        Vec5[2]=mes;
                        Vec5[3]=an;
                        Vec5[4]=h;
                        Vec5[5]=min;
                        Vec5[6]=sec;
                            }
                        if(a==6){
                        Vec6[1]=d;
                        Vec6[2]=mes;
                        Vec6[3]=an;
                        Vec6[4]=h;
                        Vec6[5]=min;
                        Vec6[6]=sec;
                            }
                        lcd.printf("Otra alarma? %0.2d",Vec1[6]);
                        lcd.locate(2,0);
                        lcd.printf("--Si      --No");
                        
                        pos++;
                        m=0;
                        }
                                                    
                    case 2:
                        m=Alarm_IRDA();  // m son los datos del 
                    
                    if(m==-2){
                        
                        lcd.cls();
                        m=0;
                        goto loop1;// Va al reloj
                        }
                    if(m==-1){
                        
                        lcd.cls();
                        m=0;
                        goto loop3; //Va a alarma
                        }
                    break;
                    
                    }
               
        }
        switch (Lugar3)//Control
                {
                case 4:
                ConfiReloj();
                goto loop1;
                break;
}
}
}
// ---- IRDA ----//
 
int detectar(void){
    int y = 0;
      header=0;
      ledd2=1;
      led=1;
      header = irda.read_low_us();    //funcion para leer un pulso de caida o bajo en header
      if (header > head_L && header < head_H){wait_us(2000);//verificar que este en la tolerancia +-20%
 
      // ES EL TIEMPO DE HEADER QUE NO SE Lee O EL ALTO 
      ledd2=0;
      for(il=0;il<(num_bits-1);++il){ // POR OSCILOSCOPIO se determina que llegan (num_bits),datos
      dato = irda.read_high_us(); //leer un bit de datos que es pulso arriba en este control
      num[il]=dato;
      wait_us(300);   
      }
       //espero un poquito luego de leer todo el arreglo y ponerlo en pantalla 
      
      for(il=0;il<(num_bits-1);++il){  
      if(num[il] > ((T_alto+T_bajo)/2)){ pc.printf("1");sel[il]='1';}
      else {pc.printf("0");sel[il]='0';}
            }
        pc.printf("\n");
        y = 1;
        pc.printf("%d",y);
}
    return y;
} 
 
int irdan(void){
    int numirda;
  
    
    for(il=0; il<32; ++il)
        {
            if(!strcmp(sel,uno)) 
            {
                numirda=1;
                break;
            }
            if(!strcmp(sel,dos)) 
            {
                numirda=2;
            }
            if(!strcmp(sel,tres)) 
            {
                numirda=3;
            }
             if(!strcmp(sel,cuatro)) 
            {
                numirda=4;
            }
            if(!strcmp(sel,cinco))
            {
                numirda=5;
            }
            if(!strcmp(sel,seis)) 
            {
                numirda=6;
            }
             if(!strcmp(sel,siete)) 
            {
                numirda=7;
            }
            if(!strcmp(sel,ocho)) 
            {
                numirda=8;
            }
            if(!strcmp(sel,nueve)) 
            {
                numirda=9;
            }
            if(!strcmp(sel,cero)) 
            {
                numirda=0;
            }
            if(!strcmp(sel,ok)) 
            {
                numirda=-8;
            }
            if(!strcmp(sel,izq)) 
            {
                numirda=-1;
            }
            if(!strcmp(sel,der))
            {
                numirda=-2;
            }
        }
        return numirda;
        }
 
 int ConfiReloj(void){  
            Rtc_Ds1307::Time_rtc tm = {};
            //;config = 0,config = 0,config = 0,ano = 2019,hora = 0, min = 0,
            int configR = 0, cont = 1, set = 0;
            int estado = 1;
            wait(0.2);
while(estado == 1){
            if(configR == 0) {
                lcd.cls();
                lcd.locate(0,0);
                lcd.printf("Configurar reloj");
                set = set + wheel.getPulses();
                pc.printf("%i",set);
                wheel.reset();
                if(set > 1) {
                    set = 0;
                }
                if(set < 0) {
                    set = 1;
                }
                lcd.locate(0, 1);
                if(set == 0) {
                    lcd.printf("       NO       ");
                    //wait_ms(10);
                    
                } else if(set == 1) {
                    lcd.printf("       SI       ");
                    //wait_ms(10);
                }
                if(bot1.falling()){
                    if(set==1){
                        configR=1;
                    }else if(set!=1){
                        configR=0;
                        estado=0;
                        }
            }
            }
            if(configR == 1) {
                cont = cont + wheel.getPulses();
                wheel.reset();
                if(cont > 31) {
                    cont = 1;
                }
                if(cont < 1) {
                    cont = 31;
                }
                lcd.locate(0, 1);
                lcd.printf("Dia: %02d        ", cont);
                tm.date = cont;
                if(bot1.falling()){
                    
                    configR=2;
                    }
            }
            if(configR == 2) {
                cont = cont + wheel.getPulses();
                wheel.reset();
                if(cont > 12) {
                    cont = 1;
                }
                if(cont < 1) {
                    cont = 12;
                }
                lcd.locate(0, 1);
                lcd.printf("Mes: %02d        ", cont);
                
                if(bot1.falling()){
                    tm.mon = cont;    
                    configR = 3;
                    }
            }
            if(configR == 3) {
                cont = cont + wheel.getPulses();
                wheel.reset();
                if(cont > 3000) {
                    cont = 2016;
                }
                if(cont < 2016) {
                    cont = 2016;
                }
                lcd.locate(0, 1);
                lcd.printf("Anno: %02d    ", cont);
                
                if(bot1.falling()){
                    tm.year = cont;    
                    configR = 4;
                    }
            }
            if(configR == 4) {
                cont = cont + wheel.getPulses();
                wheel.reset();
                if(cont > 7) {
                    cont = 1;
                }
                if(cont < 1) {
                    cont = 7;
                }
                lcd.locate(0, 1);
                lcd.printf("Dia semana: %1d", cont);
                //tm.wday = cont;
                
                if(bot1.falling()){
                    tm.wday = cont;    
                    configR = 5;
                    }
            }
            if(configR == 5) {
                cont = cont + wheel.getPulses();
                wheel.reset();
                if(cont > 23) {
                    cont = 0;
                }
                if(cont < 0) {
                    cont = 23;
                }
                lcd.locate(0, 1);
                lcd.printf("Hora: %02d       ", cont);
                //tm.hour = cont;
                if(bot1.falling()){
                    tm.hour = cont;    
                    configR = 6;
                    }
            }
            if(configR == 6) {
                cont = cont + wheel.getPulses();
                wheel.reset();
                if(cont > 59) {
                    cont = 0;
                }
                if(cont < 0) {
                    cont = 59;
                }
                lcd.locate(0, 1);
                lcd.printf("Minutos: %02d  ", cont);
                //tm.min = cont;
                if(bot1.falling()){
                    tm.min = cont;    
                    configR = 7;
                    }
            }
            if(configR == 7) {
                cont = cont + wheel.getPulses();
                wheel.reset();
                if(cont > 59) {
                    cont = 0;
                }
                if(cont < 0) {
                    cont = 59;
                }
                lcd.locate(0, 1);
                lcd.printf("Segundos: %02d  ", cont);
                tm.sec = cont;
                if(bot1.falling()){
                    tm.sec = cont;    
                    configR = 8;
                    }
            }
            if(configR == 8) {
                lcd.locate(0, 1);
                lcd.printf("  Establecido   ");
                rtc.setTime(tm, false, false);
                rtc.startClock();
                //estado = 0;
                configR = 0;
                estado=0;
                wait(2);
                
            }
        }
        }

 
 
int Alarm_IRDA(void){
    int x1 = 0, x2 = 0, x3 = 0, c = 0;
    
    while(1){
        if (detectar() == 1){
            c++;
            if(c == 1){
                x1 = irdan();
                pc.printf("\nx1: %d c: %d\n",x1,c);
                x = x1;
                if(x1==-8){
                    x=x1;
                    break;
                    }
                pc.printf("\nx: %02d",x);
                pc.printf("num: %032d",irdan());
            }//c1
            
            if(c == 2){
                x2 = irdan();
                pc.printf("\nx2: %d c: %d\n",x2,c);
                if( x2 == -8){
                    x = x1;
                    pc.printf("\nx: %02d",x);
                     pc.printf("num: %032d",sel);
                    break;
                }
                else{
                     x = x1*10 + x2;
                     pc.printf("\nx: %02d",x);
                      pc.printf("num: %032d",sel);
                
                }
                
                
            }//c2
            if (c == 3){
                x3 = irdan();
                pc.printf("\nx3: %d c: %d\n",x3,c);
                pc.printf("\nx: %02d",x);
                pc.printf("num: %032d",sel);
                if( x3 == -8){
                    x = ((x1*10) + x2);
                    pc.printf("\nx: %02d",x);
                     pc.printf("num: %032d",sel);
                    break;
                } 
            }  //c3
        }//detectar
        
        }//while
        return x; 
          
    }//funcion