reloj despertador con snoze y un ds1307

Dependencies:   PinDetect QEI RTC-DS1307 TextLCD mbed

Fork of Tarea_Reloj by Hernán Maya

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*Reloj despertador con módulo DS1307
00002 */
00003 
00004 #include "mbed.h"
00005 #include "PinDetect.h"
00006 #include "QEI.h"
00007 #include "Rtc_Ds1307.h"
00008 #include "TextLCD.h"
00009 
00010 // Configuracion puertos del microcontrolador
00011 Rtc_Ds1307 rtc(PTE0, PTE1);                 // pines para la comunicacion I2C, SDA, SCL
00012 Serial pc(USBTX, USBRX, "pc");              // puerto serie (USB)
00013 QEI wheel(PTD5, PTD0, NC, 100);             // pines para el encoder
00014 PinDetect Boton(PTA13, PullUp);             // boton encoder
00015 PinDetect BotonA(PTE20, PullUp);            // boton apagar alarma
00016 DigitalOut led1(LED_RED);                   // led rojo
00017 DigitalOut led2(LED_GREEN);                 // led verde
00018 DigitalOut led3(LED_BLUE);                  // led azul
00019 DigitalOut buzzer(PTD3);                    // alarma
00020 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // RS, E, D4, D5, D6, D7
00021 
00022 // Declaracion de variables globales
00023 int estado = 0, config = 0, dia = 1, mes = 1, ano = 2016, hora = 0, min = 0, configR = 0, cont = 1, set = 0, alarma = 0;
00024 
00025 // Rutina presion boton
00026 void keyPressed(void)
00027 {
00028     if(estado == 0 && alarma == 1) {
00029         min++;
00030     }
00031     if(estado == 1 && set == 1) {
00032         config++;
00033     }
00034     if(estado == 2 && set == 1) {
00035         configR++;
00036         cont = 1;
00037     }
00038 }
00039 
00040 // Rutina presion sostenida boton
00041 void keyPressedHeld(void)
00042 {
00043     //wait(0.5);
00044     lcd.locate(0, 0);
00045     lcd.printf("                ");
00046     lcd.locate(0, 1);
00047     lcd.printf("                ");
00048     estado++;
00049     //lcd.cls();
00050     if(estado > 2) {
00051         estado = 0;
00052     }
00053 }
00054 
00055 // Rutina para apagar la alarma
00056 void apagarAlarma(void)
00057 {
00058     min--;
00059 }
00060 // Rutina principal
00061 int main()
00062 {
00063     // Configuracion de inicio y bienvenida
00064     Rtc_Ds1307::Time_rtc tm = {};
00065     Boton.setAssertValue(0);
00066     Boton.attach_asserted(&keyPressed);
00067     Boton.attach_asserted_held(&keyPressedHeld);
00068     Boton.setSamplesTillHeld(200);
00069     Boton.setSampleFrequency();
00070     BotonA.setAssertValue(0);
00071     BotonA.attach_asserted_held(&apagarAlarma);
00072     BotonA.setSamplesTillHeld(200);
00073     BotonA.setSampleFrequency();
00074     led1 = 1;
00075     led2 = 1;
00076     led3 = 1;
00077     buzzer = 0;
00078     lcd.printf("     Reloj");
00079     lcd.locate(0, 1);
00080     lcd.printf("  despertador");
00081     wait(3);
00082     lcd.locate(0, 0);
00083     lcd.printf("                ");
00084     lcd.locate(0, 1);
00085     lcd.printf("                ");
00086     wait(0.5);
00087 
00088     // Ciclo infinito
00089     while(1) {
00090         // Reloj en funcionamiento
00091         while(estado == 0) {
00092             led1 = 1;
00093             led2 = 0;
00094             led3 = 1;
00095             rtc.getTime(tm);
00096             lcd.locate(0, 0);
00097             lcd.printf("    %02d:%02d:%02d   ", tm.hour, tm.min, tm.sec);
00098             lcd.locate(0, 1);
00099             lcd.printf(" %s %02d/%02d/%04d", rtc.weekdayToString(tm.wday), tm.date, tm.mon, tm.year);
00100             if(tm.hour == hora && tm.min == min && tm.mon == mes && tm.date == dia && tm.year == ano) {
00101                 buzzer = !buzzer;
00102                 alarma = 1;
00103             } else {
00104                 buzzer = 0;
00105                 alarma = 0;
00106             }
00107             if(tm.hour <= hora && tm.min <= min && tm.mon <= mes && tm.date <= dia && tm.year <= ano) {
00108                 lcd.locate(15, 0);
00109                 lcd.printf("@");
00110             } else {
00111                 lcd.locate(15, 0);
00112                 lcd.printf(" ");
00113             }
00114             wait(1);
00115         }
00116         // Configuracion de la alarma
00117         while(estado == 1) {
00118             led1 = 1;
00119             led2 = 1;
00120             led3 = 0;
00121             wait(0.2);
00122             if(config == 0) {
00123                 lcd.locate(0, 0);
00124                 lcd.printf("   Establecer");
00125                 lcd.locate(0, 1);
00126                 lcd.printf("alarma: ");
00127                 set = set + wheel.getPulses();
00128                 wheel.reset();
00129                 if(set > 1) {
00130                     set = 0;
00131                 }
00132                 if(set < 0) {
00133                     set = 1;
00134                 }
00135                 lcd.locate(8, 1);
00136                 if(set == 0) {
00137                     lcd.printf("NO       ");
00138                 } else if(set == 1) {
00139                     lcd.printf("SI       ");
00140                 }
00141             }
00142             if(config == 1) {
00143                 dia = dia + wheel.getPulses();
00144                 wheel.reset();
00145                 if(dia > 31) {
00146                     dia = 1;
00147                 }
00148                 if(dia < 1) {
00149                     dia = 31;
00150                 }
00151                 lcd.locate(0, 1);
00152                 lcd.printf("Dia: %02d           ", dia);
00153             }
00154             if(config == 2) {
00155                 mes = mes + wheel.getPulses();
00156                 wheel.reset();
00157                 if(mes > 12) {
00158                     mes = 1;
00159                 }
00160                 if(mes < 1) {
00161                     mes = 12;
00162                 }
00163                 lcd.locate(0, 1);
00164                 lcd.printf("Mes: %02d           ", mes);
00165             }
00166             if(config == 3) {
00167                 ano = ano + wheel.getPulses();
00168                 wheel.reset();
00169                 if(ano > 3000) {
00170                     ano = 2016;
00171                 }
00172                 if(ano < 2016) {
00173                     ano = 3000;
00174                 }
00175                 lcd.locate(0, 1);
00176                 lcd.printf("Anno: %02d       ", ano);
00177             }
00178             if(config == 4) {
00179                 hora = hora + wheel.getPulses();
00180                 wheel.reset();
00181                 if(hora > 23) {
00182                     hora = 0;
00183                 }
00184                 if(hora < 0) {
00185                     hora = 23;
00186                 }
00187                 lcd.locate(0, 1);
00188                 lcd.printf("Hora: %02d       ", hora);
00189             }
00190             if(config == 5) {
00191                 min = min + wheel.getPulses();
00192                 wheel.reset();
00193                 if(min > 59) {
00194                     min = 0;
00195                 }
00196                 if(min < 0) {
00197                     min = 59;
00198                 }
00199                 lcd.locate(0, 1);
00200                 lcd.printf("Minutos: %02d        ", min);
00201             }
00202             if(config == 6) {
00203                 lcd.locate(0, 1);
00204                 lcd.printf("  Establecida   ");
00205                 estado = 0;
00206                 config = 0;
00207                 wait(2);
00208             }
00209         }
00210         // Configuracion del reloj
00211         while(estado == 2) {
00212             led1 = 0;
00213             led2 = 1;
00214             led3 = 1;
00215             wait(0.2);
00216             if(configR == 0) {
00217                 lcd.locate(0, 0);
00218                 lcd.printf("Configurar reloj");
00219                 set = set + wheel.getPulses();
00220                 wheel.reset();
00221                 if(set > 1) {
00222                     set = 0;
00223                 }
00224                 if(set < 0) {
00225                     set = 1;
00226                 }
00227                 lcd.locate(0, 1);
00228                 if(set == 0) {
00229                     lcd.printf("       NO       ");
00230                 } else if(set == 1) {
00231                     lcd.printf("       SI       ");
00232                 }
00233             }
00234             if(configR == 1) {
00235                 cont = cont + wheel.getPulses();
00236                 wheel.reset();
00237                 if(cont > 31) {
00238                     cont = 1;
00239                 }
00240                 if(cont < 1) {
00241                     cont = 31;
00242                 }
00243                 lcd.locate(0, 1);
00244                 lcd.printf("Dia: %02d        ", cont);
00245                 tm.date = cont;
00246             }
00247             if(configR == 2) {
00248                 cont = cont + wheel.getPulses();
00249                 wheel.reset();
00250                 if(cont > 12) {
00251                     cont = 1;
00252                 }
00253                 if(cont < 1) {
00254                     cont = 12;
00255                 }
00256                 lcd.locate(0, 1);
00257                 lcd.printf("Mes: %02d        ", cont);
00258                 tm.mon = cont;
00259             }
00260             if(configR == 3) {
00261                 cont = cont + wheel.getPulses();
00262                 wheel.reset();
00263                 if(cont > 3000) {
00264                     cont = 2016;
00265                 }
00266                 if(cont < 2016) {
00267                     cont = 2016;
00268                 }
00269                 lcd.locate(0, 1);
00270                 lcd.printf("Anno: %02d    ", cont);
00271                 tm.year = cont;
00272             }
00273             if(configR == 4) {
00274                 cont = cont + wheel.getPulses();
00275                 wheel.reset();
00276                 if(cont > 7) {
00277                     cont = 1;
00278                 }
00279                 if(cont < 1) {
00280                     cont = 7;
00281                 }
00282                 lcd.locate(0, 1);
00283                 lcd.printf("Dia semana: %1d", cont);
00284                 tm.wday = cont;
00285             }
00286             if(configR == 5) {
00287                 cont = cont + wheel.getPulses();
00288                 wheel.reset();
00289                 if(cont > 23) {
00290                     cont = 0;
00291                 }
00292                 if(cont < 0) {
00293                     cont = 23;
00294                 }
00295                 lcd.locate(0, 1);
00296                 lcd.printf("Hora: %02d       ", cont);
00297                 tm.hour = cont;
00298             }
00299             if(configR == 6) {
00300                 cont = cont + wheel.getPulses();
00301                 wheel.reset();
00302                 if(cont > 59) {
00303                     cont = 0;
00304                 }
00305                 if(cont < 0) {
00306                     cont = 59;
00307                 }
00308                 lcd.locate(0, 1);
00309                 lcd.printf("Minutos: %02d  ", cont);
00310                 tm.min = cont;
00311             }
00312             if(configR == 7) {
00313                 cont = cont + wheel.getPulses();
00314                 wheel.reset();
00315                 if(cont > 59) {
00316                     cont = 0;
00317                 }
00318                 if(cont < 0) {
00319                     cont = 59;
00320                 }
00321                 lcd.locate(0, 1);
00322                 lcd.printf("Segundos: %02d  ", cont);
00323                 tm.sec = cont;
00324             }
00325             if(configR == 8) {
00326                 lcd.locate(0, 1);
00327                 lcd.printf("  Establecido   ");
00328                 rtc.setTime(tm, false, false);
00329                 rtc.startClock();
00330                 estado = 0;
00331                 configR = 0;
00332                 wait(2);
00333             }
00334         }
00335     }
00336 }