d.a. santana / Mbed 2 deprecated Reloj_4_alarmas

Dependencies:   Debounced QEI RTC-DS1307 TextLCD mbed

Fork of Reloj_alarma by Gustavo Ramirez

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DebouncedIn.h"
00003 #include "TextLCD.h"
00004 #include "QEI.h"
00005 #include "Rtc_Ds1307.h"
00006 
00007 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
00008 Rtc_Ds1307 rtc(PTE0, PTE1);//SCL,SDA
00009 QEI wheel (PTA16, PTA17, NC, 48);
00010 
00011 DebouncedIn button_enco(PTC5);
00012 //DebouncedIn button_confi(PTB8);
00013 //InterruptIn button_confi(PTB8);
00014 
00015 DigitalOut led1(PTC7);
00016 DigitalOut led2(PTC0);
00017 DigitalOut led3(PTC3);
00018 DigitalOut led4(PTC4);
00019 
00020 PwmOut Sonido(PTA5);
00021 
00022 float periodo;
00023 float Frecuencia=4000;
00024 int C1=0x0C;
00025 int m,k,x1,x2,x3,x4;
00026 int numero=1;
00027 int Conf=0;  // 0: Alarma y 1:Reloj
00028 int Lugar=0; // Definen el punto en el cual va el programa
00029 int i=0;
00030 int visual=0;
00031 int alarma=0; 
00032  
00033 
00034 int main()
00035 {
00036     Rtc_Ds1307::Time_rtc tm = {};
00037     Rtc_Ds1307::Time_rtc Alarm = {};
00038     Rtc_Ds1307::Time_rtc Alarm2 = {};
00039     Rtc_Ds1307::Time_rtc Alarm3 = {};
00040     Rtc_Ds1307::Time_rtc Alarm4 = {};
00041     lcd.cls();
00042     lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD
00043        
00044     periodo=(1/Frecuencia);
00045     Sonido.period(periodo);
00046     
00047 
00048     while(1){
00049         switch (Lugar){
00050             case 0:     //Menu principal
00051                 rtc.getTime(tm); //lee el tiempo del DS1307 
00052                 lcd.locate(0,0);
00053                 lcd.printf("%02d:%02d %02d/%02d/%02d", tm.hour, tm.min, tm.date, tm.mon, tm.year);
00054                 lcd.locate(0,1);
00055                 lcd.printf("-C.Alar  -C.Hora");
00056                 m=wheel.getPulses();  // m son los datos del encoder
00057                 if(m!=0 && Conf==0){
00058                     Conf=1;
00059                     wheel.reset();  
00060                     m=0;              
00061                 }
00062                 if(m!=0 && Conf==1){
00063                     Conf=0;
00064                     wheel.reset();  
00065                     m=0;              
00066                 }
00067                 if(Conf==0){
00068                     lcd.locate(9,1);
00069                     lcd.printf("-");                    
00070                     lcd.locate(0,1);
00071                     lcd.printf(">");
00072                     //lcd.locate(0,1);                    
00073                     wait(0.1);
00074                 }
00075                 if(Conf==1){
00076                     lcd.locate(0,1);
00077                     lcd.printf("-");                    
00078                     lcd.locate(9,1);
00079                     lcd.printf(">");
00080                     //lcd.locate(0,1);                    
00081                     wait(0.1);
00082                 }
00083                 if (button_enco.falling()){  //si se pulsa boton encoder
00084                     Lugar++;
00085                     m=0;       
00086                 }
00087             break;
00088             
00089             case 1://Configurar
00090                 switch(i){
00091                     case 0:
00092                        if(Conf==0){
00093                             lcd.cls();
00094                             lcd.locate(0,0);
00095                             lcd.printf("Config. Alarma ");
00096                             wait(1);
00097                             goto lazo;
00098 sigo:                       i++;
00099                             m=0;
00100                             lcd.cls();
00101                             lcd.locate(0,0);
00102                             lcd.printf("Dia: ");
00103                             tm.date=1;                             
00104                         }
00105                        if(Conf==1){
00106                             lcd.cls();
00107                             lcd.locate(0,0);
00108                             lcd.printf("Config. hora   ");
00109                             wait(1);
00110                             i++;
00111                             m=0;
00112                             lcd.cls();
00113                             lcd.locate(0,0);
00114                             lcd.printf("Dia: ");
00115                         }    
00116                     break;
00117                     
00118                     case 1:
00119                         m=wheel.getPulses();        
00120                         if (m!=0){
00121                             tm.date=tm.date+m;
00122                             if(tm.date>31){
00123                                 tm.date=31;
00124                             }
00125                             if(tm.date<1){
00126                                 tm.date=1;
00127                             }
00128                             wheel.reset();  
00129                             m=0;
00130                         }
00131                         lcd.locate(5,0);
00132                         lcd.printf("%02d",tm.date);
00133                         
00134                         if (button_enco.falling()){
00135                             i++;
00136                             m=0;
00137                             lcd.cls();
00138                             lcd.locate(0,0);
00139                             lcd.printf("Mes: ");
00140                             //tm.mon=1;
00141                         }
00142                     break;
00143                     
00144                     case 2:
00145                         m=wheel.getPulses();        
00146                         if (m!=0){
00147                             tm.mon=tm.mon+m;
00148                             if(tm.mon>12){
00149                                 tm.mon=12;
00150                             }
00151                             if(tm.mon<1){
00152                                 tm.mon=1;
00153                             }
00154                             
00155                             wheel.reset();  
00156                             m=0;
00157                         }
00158                         lcd.locate(5,0);
00159                         lcd.printf("%02d",tm.mon);
00160                         
00161                         if (button_enco.falling()){
00162                             i++;
00163                             m=0;
00164                             lcd.cls();
00165                             lcd.locate(0,0);
00166                             lcd.printf("Year: ");
00167                             
00168                         }
00169                     break;
00170                     
00171                     case 3:
00172                         m=wheel.getPulses();        
00173                         if (m!=0){
00174                             tm.year=tm.year+m;
00175                             
00176                             if(tm.year<2016){
00177                                 tm.year=2016;
00178                             }
00179                             
00180                             wheel.reset();  
00181                             m=0;
00182                         }
00183                         lcd.locate(6,0);
00184                         lcd.printf("%02d",tm.year);
00185                         
00186                         if (button_enco.falling()){
00187                             i++;
00188                             m=0;
00189                             lcd.cls();
00190                             lcd.locate(0,0);
00191                             lcd.printf("Hora: ");
00192                             
00193                         }
00194                     break;
00195                     
00196                      case 4:
00197                         m=wheel.getPulses();
00198                         if (m!=0){
00199                             tm.hour=tm.hour + m;
00200                             if(tm.hour>23){
00201                                 tm.hour=23;
00202                             }
00203                             if(tm.hour<0){
00204                                 tm.hour=0;
00205                             }
00206                             
00207                             wheel.reset();  
00208                             m=0;
00209                         }
00210                         lcd.locate(6,0);
00211                         lcd.printf("%02d",tm.hour);
00212                         
00213                         if (button_enco.falling()){
00214                             i++;
00215                             m=0;
00216                             lcd.cls();
00217                             lcd.locate(0,0);
00218                             lcd.printf("Minuto: ");
00219                             //tm.min=0;
00220                         }
00221                     break;
00222                     
00223                     case 5:
00224                         m=wheel.getPulses();        
00225                         if (m!=0){
00226                             tm.min=tm.min+m;
00227                             if(tm.min>59){
00228                                 tm.min=59;
00229                             }
00230                             if(tm.min<0){
00231                                 tm.min=0;
00232                             }
00233                             wheel.reset();  
00234                             m=0;
00235                         }
00236                         lcd.locate(8,0);
00237                         lcd.printf("%02d",tm.min);
00238                         
00239                         if (button_enco.falling()){
00240                             i++;
00241                             m=0;
00242                             lcd.cls();
00243                             lcd.locate(0,0);
00244                             lcd.printf("Segundo: ");
00245                             //tm.sec=0;
00246                         }
00247                     break;
00248                     
00249                     
00250                     case 6:
00251                         m=wheel.getPulses();        
00252                         if (m!=0){
00253                             tm.sec=tm.sec+m;
00254                             if(tm.sec>59){
00255                                 tm.sec=59;
00256                             }
00257                             if(tm.sec<0){
00258                                 tm.sec=0;
00259                             }
00260                             
00261                             wheel.reset();  
00262                             m=0;
00263                         }
00264                         lcd.locate(9,0);
00265                         lcd.printf("%02d",tm.sec);
00266                         
00267                         if (button_enco.falling()){
00268                             ///////////////////
00269                             if(Conf==0){
00270                                 if(numero ==1){
00271                                     Alarm=tm;
00272                                     x1=1;    
00273                                 }
00274                                 if(numero ==2){
00275                                     Alarm2=tm;
00276                                     x2=1;    
00277                                 }
00278                                 if(numero ==3){
00279                                     Alarm3=tm;    
00280                                     x3=1;
00281                                 }
00282                                 if(numero ==4){
00283                                     Alarm4=tm;    
00284                                     x4=1;
00285                                 }
00286                                 lcd.cls();
00287                                 lcd.locate(0,1);
00288                                 lcd.printf("Alarma %02d config",numero);
00289                                 wait(1);
00290                                 i=0;
00291                                 m=0;
00292                                 Lugar++;
00293                                 lcd.cls();
00294                             }
00295                 
00296                             if(Conf==1){
00297                                 rtc.setTime(tm, false, false);
00298                                 rtc.startClock();
00299                                 lcd.cls();
00300                                 lcd.locate(0,0);
00301                                 lcd.printf("Hora Config.  ");
00302                                 wait(1);
00303                                 i=0;
00304                                 m=0;
00305                                 Lugar=0;
00306                                 rtc.getTime(tm); //lee el tiempo del DS1307 
00307                                 lcd.locate(0,0);
00308                                 lcd.printf("%02:",tm.hour);
00309                                 lcd.locate(0,1);
00310                                 lcd.printf("-C.Alar  -C.Hora");
00311                            } 
00312                         }
00313                     break;
00314                  }            
00315             break;
00316             
00317             case 2:     //Visualizar y Activacion Alarmas
00318                 rtc.getTime(tm);
00319                 m=wheel.getPulses();
00320                 if(x1==1){
00321                     led1=1;
00322                 }
00323                 if(x2==1){
00324                     led2=1;
00325                 }
00326                 if(x3==1){
00327                     led3=1;
00328                 }
00329                 if(x4==1){
00330                     led4=1;
00331                 }                
00332                 if(m!=0){
00333                    visual=visual+m;
00334                    if(visual<0){
00335                       visual=0;
00336                    }
00337                    if(visual>4){
00338                       visual=4;
00339                    }                    
00340                    wheel.reset();  
00341                    m=0;
00342                 }
00343                 
00344                 if(visual==0){//Visualiza hora
00345                    lcd.cls();
00346                    lcd.locate(0,0);    
00347                    lcd.printf(" Ver   %02d:%02d:%02d",tm.hour,tm.min,tm.sec);
00348                    lcd.locate(0,1);
00349                    lcd.printf("Hora :%02d/%02d/%04d",tm.mon,tm.date,tm.year);
00350                    wait(0.1);
00351                 }   
00352                 if(visual==1){//Visualilza alarma1
00353                    lcd.cls();
00354                    lcd.locate(0,0);    
00355                    lcd.printf(" Ver   %02d:%02d:%02d", Alarm.hour, Alarm.min,Alarm.sec);
00356                    lcd.locate(0,1);
00357                    lcd.printf("Alar1:%02d/%02d/%04d",Alarm.mon,Alarm.date,Alarm.year);
00358                    wait(0.1);
00359                 }
00360                 if(visual==2){//Visualiza alarma2
00361                    lcd.cls();
00362                    lcd.locate(0,0);    
00363                    lcd.printf(" Ver   %02d:%02d:%02d", Alarm2.hour, Alarm2.min,Alarm2.sec);
00364                    lcd.locate(0,1);
00365                    lcd.printf("Alar2:%02d/%02d/%04d",Alarm2.mon,Alarm2.date,Alarm2.year);
00366                    wait(0.1);
00367                 }
00368                 if(visual==3){//Visualiza alarma3
00369                    lcd.cls();
00370                    lcd.locate(0,0);    
00371                    lcd.printf(" Ver   %02d:%02d:%02d", Alarm3.hour, Alarm3.min,Alarm3.sec);
00372                    lcd.locate(0,1);
00373                    lcd.printf("Alar3:%02d/%02d/%04d",Alarm3.mon,Alarm3.date,Alarm3.year);
00374                    wait(0.1);
00375                 }
00376                 if(visual==4){//Visualiza alarma4
00377                    lcd.cls();
00378                    lcd.locate(0,0);    
00379                    lcd.printf(" Ver   %02d:%02d:%02d", Alarm4.hour, Alarm4.min,Alarm4.sec);
00380                    lcd.locate(0,1);
00381                    lcd.printf("Alar4:%02d/%02d/%04d",Alarm4.mon,Alarm4.date,Alarm4.year);
00382                    wait(0.1);
00383                 }
00384                      
00385                 if (button_enco.falling()){
00386                     Lugar=1;
00387                 }
00388                 //Activacion  Alarma1               
00389                 if((tm.date==Alarm.date)&&(tm.mon==Alarm.mon)&&(tm.year==Alarm.year)&&(tm.hour==Alarm.hour)&&(tm.min==Alarm.min)&&(tm.sec==Alarm.sec))
00390                 {
00391                     Lugar++;
00392                     lcd.cls();
00393                     lcd.locate(0,0);                        
00394                     lcd.printf("Alarma1  Activa!!");
00395                     led1 =0;
00396                     wait(.2);
00397                     led1 =1;
00398                     wait(.2);
00399                     led1 =0;
00400                     wait(.2);
00401                     led1 =1;
00402                     wait(.2);
00403                     led1 =0;
00404                     lcd.locate(0,0);
00405                     lcd.printf("->Desactivar    ");                       
00406                     lcd.locate(0,1);                        
00407                     lcd.printf("--Aplazar       ");
00408                     m=0;
00409                     k=1;
00410                 }
00411 
00412                 //Activacion  Alarma2               
00413                 if((tm.date==Alarm2.date)&&(tm.mon==Alarm2.mon)&&(tm.year==Alarm2.year)&&(tm.hour==Alarm2.hour)&&(tm.min==Alarm2.min)&&(tm.sec==Alarm2.sec))
00414                 {
00415                     Lugar++;
00416                     lcd.cls();
00417                     lcd.locate(0,0);                        
00418                     lcd.printf("Alarma2  Activa!!");
00419                     led2 =0;
00420                     wait(.2);
00421                     led2 =1;
00422                     wait(.2);
00423                     led2 =0;
00424                     wait(.2);
00425                     led2 =1;
00426                     wait(.2);
00427                     led2 =0;
00428                     lcd.locate(0,0);
00429                     lcd.printf("->Desactivar    ");                       
00430                     lcd.locate(0,1);                        
00431                     lcd.printf("--Aplazar       ");
00432                     m=0;
00433                     k=2;
00434                 }
00435 
00436                 //Activacion  Alarma3               
00437                 if((tm.date==Alarm3.date)&&(tm.mon==Alarm3.mon)&&(tm.year==Alarm3.year)&&(tm.hour==Alarm3.hour)&&(tm.min==Alarm3.min)&&(tm.sec==Alarm3.sec))
00438                 {
00439                     Lugar++;
00440                     lcd.cls();
00441                     lcd.locate(0,0);                        
00442                     lcd.printf("Alarma3  Activa!!");
00443                     led3 =0;
00444                     wait(.2);
00445                     led3 =1;
00446                     wait(.2);
00447                     led3 =0;
00448                     wait(.2);
00449                     led3 =1;
00450                     wait(.2);
00451                     led3 =0;
00452                     lcd.locate(0,0);
00453                     lcd.printf("->Desactivar    ");                       
00454                     lcd.locate(0,1);                        
00455                     lcd.printf("--Aplazar       ");
00456                     m=0;
00457                     k=3;
00458                 }
00459                 //Activacion  Alarma4               
00460                 if((tm.date==Alarm4.date)&&(tm.mon==Alarm4.mon)&&(tm.year==Alarm4.year)&&(tm.hour==Alarm4.hour)&&(tm.min==Alarm4.min)&&(tm.sec==Alarm4.sec))
00461                 {
00462                     Lugar++;
00463                     lcd.cls();
00464                     lcd.locate(0,0);                        
00465                     lcd.printf("Alarma1  Activa!!");
00466                     led4 =0;
00467                     wait(.2);
00468                     led4 =1;
00469                     wait(.2);
00470                     led4 =0;
00471                     wait(.2);
00472                     led4 =1;
00473                     wait(.2);
00474                     led4 =0;
00475                     lcd.locate(0,0);
00476                     lcd.printf("->Desactivar    ");                       
00477                     lcd.locate(0,1);                        
00478                     lcd.printf("--Aplazar       ");
00479                     m=0;
00480                     k=4;
00481                 }
00482                  
00483             break;
00484             
00485             
00486             
00487             
00488             case 3://Aplazar Desactivar Alarma
00489                 
00490                 if(alarma==0){
00491                     Sonido.write(0.5);
00492                     wait(0.2);
00493                     Sonido.write(0);
00494                     wait(0.2);
00495                     Sonido.write(0.5);
00496                     wait(0.2);
00497                     Sonido.write(0);
00498                     wait(0.2);
00499                     m=wheel.getPulses();
00500                     if(m!=0){                        
00501                         alarma=1;
00502                         m=0;
00503                         wheel.reset();
00504                         
00505                         lcd.locate(1,0);
00506                         lcd.printf("-");                    
00507                         lcd.locate(1,1);
00508                         lcd.printf(">");                                          
00509                     }
00510                 }
00511                           
00512                 ///////////////////
00513                 
00514                 if(alarma==1){
00515                     Sonido.write(0.5);
00516                     wait(0.2);
00517                     Sonido.write(0);
00518                     wait(0.2);
00519                     Sonido.write(0.5);
00520                     wait(0.2);
00521                     Sonido.write(0);
00522                     wait(0.2);
00523                     
00524                     m=wheel.getPulses();
00525                     if(m!=0){                        
00526                         alarma=0;
00527                         m=0;
00528                         wheel.reset();
00529                         
00530                         lcd.locate(1,0);
00531                         lcd.printf(">");                    
00532                         lcd.locate(1,1);
00533                         lcd.printf("-");                                          
00534                     }
00535                 }
00536                 
00537                 if (button_enco.falling()){
00538                     if(alarma==0){
00539                         Lugar=0;
00540                         wheel.reset();
00541                         m=0;
00542                         Sonido.write(0);
00543                         lcd.cls();
00544                         rtc.getTime(tm); //lee el tiempo del DS1307 
00545                         lcd.locate(0,0);
00546                         lcd.printf("%02:",tm.hour);
00547                         lcd.locate(0,1);
00548                         lcd.printf("-C.Alar -C.Hora");
00549                         
00550                     }
00551                     if(alarma==1){
00552                         Lugar=2;
00553                         rtc.getTime(tm);
00554 
00555                         switch(k){
00556                             case 1:
00557                                 if(tm.min!=58){
00558                                     Alarm.min=tm.min+1;
00559                                 }
00560                                 if(tm.min==59){
00561                                     Alarm.min=0;
00562                                     Alarm.hour=tm.hour+1;
00563                                 }
00564                                 Alarm.sec=tm.sec;
00565                                 led1 =1;
00566                                 lcd.cls();
00567                                 alarma=0;
00568                             break;
00569                             
00570                             case 2:
00571                                 if(tm.min!=58){
00572                                     Alarm2.min=tm.min+1;
00573                                 }
00574                                 if(tm.min==59){
00575                                     Alarm2.min=0;
00576                                     Alarm2.hour=tm.hour+1;
00577                                 }
00578                                 Alarm2.sec=tm.sec;
00579                                 led2 =1;
00580                                 lcd.cls();
00581                                 alarma=0;
00582                             break;
00583                             
00584                             case 3:
00585                                 if(tm.min!=58){
00586                                     Alarm3.min=tm.min+1;
00587                                 }
00588                                 if(tm.min==59){
00589                                     Alarm3.min=0;
00590                                     Alarm3.hour=tm.hour+1;
00591                                 }
00592                                 Alarm3.sec=tm.sec;
00593                                 led3 =1;
00594                                 lcd.cls();
00595                                 alarma=0;
00596                             break;
00597                             
00598                             case 4:
00599                                 if(tm.min!=58){
00600                                     Alarm4.min=tm.min+1;
00601                                 }
00602                                 if(tm.min==59){
00603                                     Alarm4.min=0;
00604                                     Alarm4.hour=tm.hour+1;
00605                                 }
00606                                 Alarm4.sec=tm.sec;
00607                                 led4 =1;
00608                                 lcd.cls();
00609                                 alarma=0;                            
00610                             break;    
00611                         }
00612                     }
00613                     
00614                 }
00615                 
00616             
00617             break;
00618         }             
00619     
00620     }
00621 // Lazo de Config Alarmas   
00622 lazo:   int t=0;
00623         numero=1;
00624         while(t==0){    
00625             m=wheel.getPulses(); 
00626             lcd.locate(0,0);
00627             lcd.printf("Config Alarma %02d",numero);
00628             lcd.locate(0,1);
00629             lcd.printf("-1  -2   -3  -4",tm.date);
00630             if (m!=0){
00631                 numero=numero+m;
00632                 if(numero<1){
00633                     numero=1;
00634                     }
00635                 if(numero>4){
00636                     numero=4;
00637                     }
00638                 wheel.reset();  
00639                     m=0;
00640                     }
00641                 if(numero==1){
00642                     led1 =1;
00643                     led2 =0;
00644                     led3 =0;
00645                     led4 =0;
00646                     lcd.locate(0,1);
00647                     lcd.printf(">");                    
00648                     lcd.locate(4,1);
00649                     lcd.printf("-");
00650                     lcd.locate(9,1);                    
00651                     lcd.printf("-");
00652                     lcd.locate(13,1);                    
00653                     lcd.printf("-");
00654                     wait(0.1);    
00655                     }
00656                 if(numero==2){
00657                     led1 =0;
00658                     led2 =1;
00659                     led3 =0;
00660                     led4 =0;
00661                     lcd.locate(0,1);
00662                     lcd.printf("-");                    
00663                     lcd.locate(4,1);
00664                     lcd.printf(">");
00665                     lcd.locate(9,1);                    
00666                     lcd.printf("-");
00667                     lcd.locate(13,1);                    
00668                     lcd.printf("-");                  
00669                     wait(0.1);
00670                     }
00671                 if(numero==3){         
00672                     led1 =0;
00673                     led2 =0;
00674                     led3 =1;
00675                     led4 =0;
00676                     lcd.locate(0,1);
00677                     lcd.printf("-");                    
00678                     lcd.locate(4,1);
00679                     lcd.printf("-");
00680                     lcd.locate(9,1);                    
00681                     lcd.printf(">");
00682                     lcd.locate(13,1);                    
00683                     lcd.printf("-");                  
00684                     wait(0.1);    
00685                     }
00686                 if(numero==4){
00687                     led1 =0;
00688                     led2 =0;
00689                     led3 =0;
00690                     led4 =1;
00691                     lcd.locate(0,1);
00692                     lcd.printf("-");                    
00693                     lcd.locate(4,1);
00694                     lcd.printf("-");
00695                     lcd.locate(9,1);                    
00696                     lcd.printf("-");
00697                     lcd.locate(13,1);                    
00698                     lcd.printf(">");                  
00699                     wait(0.1);
00700                     }
00701                         
00702                 if (button_enco.falling()){
00703                     m=0;
00704                     lcd.cls();
00705                     lcd.locate(4,0);                        
00706                     lcd.printf("Alarma %02d ",numero);
00707                     lcd.locate(2,1);                        
00708                     lcd.printf("Seleccionada");
00709                     wait(1.5);
00710                     lcd.cls();
00711                     t=1;
00712                     }
00713         }       
00714         goto sigo;
00715 
00716 }   
00717