Juan Salvador Payares Iriarte / Mbed 2 deprecated Tarea5_Reloj_y_Alarma_DS1307

Dependencies:   DS1307 DebouncedIn TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "ds1307.h"
00002  #include "TextLCD.h"
00003  #include "mbed.h"
00004 #include "DebouncedIn.h"
00005  
00006  Serial pc(USBTX, USBRX); // tx, rx  for debug and usb pc comunications
00007  //TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD40x2); // rs, e, d4-d7
00008  TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); 
00009  DS1307 my1307(PTE0,PTE1); // start DS1307 class and give it pins for connections of the DS1307 device
00010 DebouncedIn p1(PTE20);
00011 DebouncedIn p2(PTE21);
00012 DebouncedIn p3(PTE22);
00013 int contador=1;
00014 int contador2 = 0;
00015  int hours1;
00016  int min1=-1;
00017  int sec1=0; 
00018  int column;
00019  int fila;
00020  int C2=0x18; // desplaza izquierda
00021  int C3=0x1A; // desplaza derecha
00022  int C4=0x0C; // quito cursor bajo
00023  int C1=0x0F;
00024 
00025  DigitalOut sound(PTC5); //Salida de Audio Digital
00026 bool k=false;  //bandera que indica que la alarma esta o no activada
00027 bool on=false;  //Bandera que indica que la alarma esta o no sonando
00028 bool apagar=false;
00029  int sec = 48;       // Values to set time with
00030  int min = 59;
00031  int hours = 06;
00032  int day = 2;
00033  int date = 12;
00034  int month = 5;
00035  int year = 16;
00036  int loop = 10;     // Non zero value for the while loop
00037  
00038  
00039  void test_rw(int test) {
00040     if (test == 0) pc.printf("Last R/W operation passed!\n\r");
00041    else pc.printf("Last R/W operation failed!\n\r");
00042  }
00043  
00044  int main() {  
00045  
00046  test_rw(my1307.settime( sec, min, hours, day, date, month, year));    // Set the time on the DS1307
00047  
00048  
00049         while (loop>0){        // Loop continously
00050         test_rw(my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year));
00051         lcd.locate(0,0);            // Print and refresh data on line 2 of the LCD display     
00052         lcd.printf("%.2D",hours);
00053         lcd.printf(":%.2D",min);
00054         lcd.printf(":%.2D",sec);
00055         lcd.locate(0,1);  
00056         lcd.printf("%.2D",month);
00057         lcd.printf("/%.2D",date);
00058         lcd.printf("/%.2D",year);
00059         
00060         
00061                     //Presionar Pulsador 1
00062                     if(p1.falling()){  //Configurar Hora 
00063                                          
00064                                          if(k==true & apagar==false){ //Stop Alarm, recuerda agregar mensaje de pausa
00065                                              k =false;
00066                                                      wait(1);
00067                                                      if(p1==0){
00068                                                          apagar = true;
00069                                                          lcd.locate(0,0);
00070                                                          lcd.printf("Alarm Desact"); 
00071                                                          wait(1);
00072                                                          lcd.locate(0,0);
00073                                                          lcd.printf("                  "); 
00074                                                          lcd.locate(0,1);
00075                                                          lcd.printf("Alarm Desact"); 
00076                                                          wait(1);
00077                                                          lcd.locate(0,1);
00078                                                          lcd.printf("                  "); 
00079                                                          hours1=0;
00080                                                          min1=0;
00081                                                          sec1=0;
00082                                                       }//cierro p1==0
00083                                                 if(apagar==false){     
00084                                                     sec1=sec+10;
00085                                                     if(sec1>59){
00086                                                         min1 = min1 +1;
00087                                                         sec1 = sec1-60;
00088                                                         lcd.locate(9,1); 
00089                                                         lcd.printf("M.A: %.2d",min1); //actualiza, Minutos de Alarma En pantalla
00090                                                      }//cierro if sec > 59
00091                                              
00092                                                  }//cierro Apagar == false
00093                                              }//cierro k = true && apagar == false
00094                                          
00095                                          
00096                                          if(contador ==2){
00097                                         
00098                                         //lcd.locate(9,0);
00099                                         ++hours;
00100                                         
00101                                         if(hours>23){//ajuste de hora
00102                                             hours = 0;
00103                                             }//Fin if hoours >23
00104                                             
00105                                         //lcd.printf("Hora: %.2D ", hours);
00106                                         test_rw(my1307.settime( sec, min, hours, day, date, month, year)); // Set the time on the DS1307
00107                                         //wait(0.5);
00108                                          
00109                                          
00110                                 }//Cierro if contador ==2
00111                                 
00112                                 if(contador == 3){
00113                                     ++min;
00114                                     
00115                                     if(min>59){ //Ajuste de minutos
00116                                         min = 0;
00117                                         } //cierro if min > 59
00118                                     test_rw(my1307.settime( sec, min, hours, day, date, month, year)); // Set the time on the DS1307
00119                                     } //Cierro if contador ==3
00120                         
00121                                 if(contador == 4){//configurar mes
00122                                 ++month; 
00123                                 if(month > 12){ //ajuste de meses
00124                                     month =1;
00125                                     }//cierro mont > 12
00126                                     
00127                                  test_rw(my1307.settime( sec, min, hours, day, date, month, year)); // Set the time on the DS1307
00128                                 }//cierro contador ==4
00129                         
00130                                 
00131                                 if(contador ==5){
00132                                         ++date;
00133                                         if(month==1 || month ==3 || month == 5 || month == 7 || month ==8 || month ==10 || month ==12 ){//ajuste de 31 dias 
00134                                                 if(date > 31){
00135                                                     date=01;
00136                                                 }//Cierro day >31
00137                                         }//cierro condicion meses con 31 dias   
00138                                     
00139                                         if( month ==4 || month == 6 || month == 9 || month ==11){//ajuste de 30 dias 
00140                                                 if(date >30){
00141                                                     date=01;
00142                                                 }//Cierro day >30
00143                                         }//Cierro condicion meses de 30 dias
00144                                           
00145                                         if(month==2 ){//ajuste de 28 dias 
00146                                                 
00147                                                         if((year+2000) % 4 ==0){ //configuracion año viciesto
00148                                                             if(date >29){
00149                                                                 date=1;
00150                                                             }//cierro if date> 29        
00151                                                                                                
00152                                                         
00153                                                         }//Cierro año viciesto
00154                                                         
00155                                                         if((year+2000) % 4 != 0){ //configuracion año viciesto
00156                                                             if(date >28){
00157                                                                 date=1;
00158                                                             }//cierro if date> 28        
00159                                                                                                
00160                                                         
00161                                                         }//Cierro año NO viciesto
00162                                                     
00163                                         
00164                                          }//cierro if month ==2 
00165                                     
00166                                 test_rw(my1307.settime( sec, min, hours, day, date, month, year)); // Set the time on the DS1307
00167                                 }//cierro if contador ==5
00168                                 
00169                                     if(contador == 6){ //Aumentar años
00170                                         ++year;
00171                                      test_rw(my1307.settime( sec, min, hours, day, date, month, year)); // Set the time on the DS1307   
00172                                     }//cierro if contador ==6
00173                  
00174                     
00175                                 //Configuracin De Alarma
00176                                 if(contador2==1){
00177                                     hours1++;
00178                                     if(hours1>23){
00179                                     hours1= 0;
00180                                     }//Cierro hours1>23                                        
00181                                     lcd.locate(9,0);
00182                                     lcd.printf("H.A: %.2d",hours1);
00183                                     }//Cierro if contador2==1
00184                                 
00185                                 if(contador2==2){
00186                                     min1++;
00187                                     if(min1>59){
00188                                     min1= 0;
00189                                     }//Cierro hours1>23                                        
00190                                     lcd.locate(9,1);
00191                                     lcd.printf("M.A: %.2d",min1);
00192                                     
00193                                     }//Cierro if contador2==1
00194                                 
00195                     
00196                     }//Fin p1.falling 
00197                     
00198                     //Presionar Pulsador 2
00199                     if(p2.falling()){ //Reset
00200                         contador = contador+1;
00201                         
00202                         
00203                                 if(contador>0){
00204                                     contador2=0;
00205                                     }
00206                                     
00207                                 if(contador ==1){
00208                                     lcd.locate(0,0);
00209                                     lcd.printf("1-Reset");
00210                                     wait(0.6);
00211                                     lcd.printf("               ");
00212                                     }//cierro if contador ==1
00213                                     
00214                                 if(contador==2){ //configurar Hora
00215                                     lcd.locate(0,0);
00216                                     
00217                                     lcd.printf("2-Conf. hora");
00218                                     wait(0.6);
00219                                     lcd.locate(0,0);
00220                                     lcd.printf("                 ");
00221                                     
00222                                     }//Cierro if contador ==2
00223                                     
00224                                     if(contador==3){ //configurar minutos
00225                                     lcd.locate(0,0);
00226                                     
00227                                     lcd.printf("3-Conf. Min");
00228                                     wait(0.6);
00229                                     lcd.locate(0,0);
00230                                     lcd.printf("                   ");
00231                                     
00232                                     }//Cierro if contador ==3
00233                                     
00234                                     if(contador ==4){
00235                                         lcd.locate(0,0);
00236                                         lcd.printf("4-Conf. Mes");
00237                                         
00238                                         wait(0.6);
00239                                         lcd.locate(0,0);
00240                                         lcd.printf("                   ");
00241                                         }//cierro if contador ==4
00242                                         
00243                                     if(contador == 5){
00244                                         lcd.locate(0,0);
00245                                         lcd.printf("5-Conf. Dia");
00246                                         
00247                                         wait(0.6);
00248                                         lcd.locate(0,0);
00249                                         lcd.printf("                   ");
00250                                         
00251                                         }//Cierro if contador == 5
00252                             
00253                                     if(contador ==6){
00254                                         lcd.locate(0,0);
00255                                         lcd.printf("5-Conf. año"); //Se aumentan años en p1 y se disminuyen con p3 (ver p3 debajo)
00256                                         
00257                                         wait(0.6);
00258                                         lcd.locate(0,0);
00259                                         lcd.printf("                   ");
00260                                         }//Cierro if contador ==6
00261                                         
00262                                         if(contador>6){ //Reset Contador
00263                                             contador =1;
00264                                             lcd.locate(0,0);
00265                                             lcd.printf("Config. Correct");
00266                                             wait(0.6);
00267                                             lcd.locate(0,0);
00268                                             lcd.printf("                   ");
00269                                             }//Cierro contador > 6
00270                                         
00271                         }//Cierro if p2.falling()
00272                         
00273                         //Presionar Pulsador 3
00274                         if(p3.falling()){
00275                             
00276                             if(contador==6){
00277                                 --year;
00278                                 test_rw(my1307.settime( sec, min, hours, day, date, month, year)); // Set the time on the DS1307   
00279                                 contador2=0;    
00280                                         
00281                                 }//Cierro if contador == 6
00282                             
00283                             if(contador != 6){
00284                             
00285                             contador=1; //Se asegura que no interceda a configurar la hora como loco cuando se configura la alarma
00286                             contador2++; 
00287                                   if(contador2==1){      
00288                                   lcd.locate(0,0);
00289                                   lcd.printf("Alarm Hour");
00290                                   wait(0.6);
00291                                   lcd.locate(0,0);
00292                                   lcd.printf("                  ");                         
00293                                   } //cierro contador2 ==1
00294                                   
00295                                   if(contador2==2){      
00296                                   lcd.locate(0,1);
00297                                   lcd.printf("Alarm Minutes");
00298                                   wait(0.6);
00299                                   lcd.locate(0,1);
00300                                   lcd.printf("                  ");                         
00301                                   } //cierro contador2 ==1
00302                                   
00303                                   if(contador2>2){
00304                                     contador2=0;
00305                                     lcd.locate(0,0);
00306                                     lcd.printf("Activate");
00307                                     wait(0.6);
00308                                     k = true; //alarma activada
00309                                     apagar=false;
00310                                     }//cierro if contador2 >2
00311                                     
00312                             }//Cierro contador != 6
00313                             
00314                             }//cierro if p3.falling()
00315                     
00316                     //Activacion De Alarma
00317                     if(hours == hours1 && k == true && apagar ==false){
00318                         if(min == min1){
00319                             if(sec>=sec1 && sec<=(sec1+10)){
00320                                 on = true;
00321                                 for(int i=1; i<= 1000; ++i){ //ONDA CUADRADA, OSCILACIONES A 10 KHZ.... A VER SI FUNCIONA
00322                                 sound = 0;
00323                                 wait(0.00015);  // Medio ciclo de dureza Arriba
00324                                 sound=1;
00325                                 wait(0.0005); // Medio ciclo Abajo
00326                                 
00327                                     if(k==false){
00328                                     break; 
00329                                      }//cierro if on==false
00330                               }//Cierro For
00331                                 
00332                             }//Cierro Sec>=0 && sec <30
00333                           }//Cierro min == min1
00334                         
00335                         }//cierro hours==hours1
00336                    
00337                     if(sec1>0 & k==false){ //alarma encendida, reactiva k para poder iniciar nuevamente cuando el tiempo se alcance otra vez
00338                     k = true;
00339                     }//Cierro if sec1>0
00340                     
00341         } //Cierro While loop >0
00342         
00343         
00344         
00345     }//Cierro Main()
00346  
00347  
00348  
00349