![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Reloj con alarma y snooze
Dependencies: RTC-DS1307-Modificada TextLCD mbed QEI
main.cpp@0:1c2ae7e04432, 2016-05-17 (annotated)
- Committer:
- Susanaranjo
- Date:
- Tue May 17 03:52:27 2016 +0000
- Revision:
- 0:1c2ae7e04432
- Child:
- 2:32f3a407a8ea
Reloj en tiempo real con alarma y snooze
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Susanaranjo | 0:1c2ae7e04432 | 1 | #include "mbed.h" |
Susanaranjo | 0:1c2ae7e04432 | 2 | #include "Rtc_Ds1307.h" |
Susanaranjo | 0:1c2ae7e04432 | 3 | #include "TextLCD.h" |
Susanaranjo | 0:1c2ae7e04432 | 4 | |
Susanaranjo | 0:1c2ae7e04432 | 5 | //Configuración de la pantalla |
Susanaranjo | 0:1c2ae7e04432 | 6 | TextLCD lcd(PTE20,PTE21,PTE22,PTE23,PTE29,PTE30, TextLCD::LCD16x2); //Rs, E, d4, d5, d6, d7 |
Susanaranjo | 0:1c2ae7e04432 | 7 | |
Susanaranjo | 0:1c2ae7e04432 | 8 | //Configuración del reloj |
Susanaranjo | 0:1c2ae7e04432 | 9 | Rtc_Ds1307 rtc(PTE0, PTE1); // SDA, SCL |
Susanaranjo | 0:1c2ae7e04432 | 10 | |
Susanaranjo | 0:1c2ae7e04432 | 11 | //Configuración del puerto |
Susanaranjo | 0:1c2ae7e04432 | 12 | Serial pc(USBTX, USBRX, "pc"); |
Susanaranjo | 0:1c2ae7e04432 | 13 | |
Susanaranjo | 0:1c2ae7e04432 | 14 | DigitalIn alarma(PTC12); |
Susanaranjo | 0:1c2ae7e04432 | 15 | DigitalIn snooze(PTC13); |
Susanaranjo | 0:1c2ae7e04432 | 16 | PwmOut speaker(PTA13); |
Susanaranjo | 0:1c2ae7e04432 | 17 | |
Susanaranjo | 0:1c2ae7e04432 | 18 | |
Susanaranjo | 0:1c2ae7e04432 | 19 | char buffer[128]; |
Susanaranjo | 0:1c2ae7e04432 | 20 | int readptr = 0; |
Susanaranjo | 0:1c2ae7e04432 | 21 | int hora=0; |
Susanaranjo | 0:1c2ae7e04432 | 22 | int minutos=0; |
Susanaranjo | 0:1c2ae7e04432 | 23 | int seg=0; |
Susanaranjo | 0:1c2ae7e04432 | 24 | int mes=0; |
Susanaranjo | 0:1c2ae7e04432 | 25 | int fecha=0; |
Susanaranjo | 0:1c2ae7e04432 | 26 | int anno=0; |
Susanaranjo | 0:1c2ae7e04432 | 27 | int pulsador; |
Susanaranjo | 0:1c2ae7e04432 | 28 | int pulsador2; |
Susanaranjo | 0:1c2ae7e04432 | 29 | int a=0; |
Susanaranjo | 0:1c2ae7e04432 | 30 | |
Susanaranjo | 0:1c2ae7e04432 | 31 | |
Susanaranjo | 0:1c2ae7e04432 | 32 | int main() |
Susanaranjo | 0:1c2ae7e04432 | 33 | { |
Susanaranjo | 0:1c2ae7e04432 | 34 | char c; |
Susanaranjo | 0:1c2ae7e04432 | 35 | Rtc_Ds1307::Time_rtc tm = {}; |
Susanaranjo | 0:1c2ae7e04432 | 36 | |
Susanaranjo | 0:1c2ae7e04432 | 37 | lcd.cls(); |
Susanaranjo | 0:1c2ae7e04432 | 38 | lcd.locate(0,0); |
Susanaranjo | 0:1c2ae7e04432 | 39 | lcd.printf("Configurando"); |
Susanaranjo | 0:1c2ae7e04432 | 40 | lcd.locate(0,1); |
Susanaranjo | 0:1c2ae7e04432 | 41 | lcd.printf("el reloj"); |
Susanaranjo | 0:1c2ae7e04432 | 42 | |
Susanaranjo | 0:1c2ae7e04432 | 43 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 44 | pc.printf("* Configurar el reloj *\n"); |
Susanaranjo | 0:1c2ae7e04432 | 45 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 46 | |
Susanaranjo | 0:1c2ae7e04432 | 47 | while(1) { |
Susanaranjo | 0:1c2ae7e04432 | 48 | |
Susanaranjo | 0:1c2ae7e04432 | 49 | |
Susanaranjo | 0:1c2ae7e04432 | 50 | pc.printf("Ingrese la fecha (entre 1 y 31)"); |
Susanaranjo | 0:1c2ae7e04432 | 51 | pc.scanf("%d", &tm.date); |
Susanaranjo | 0:1c2ae7e04432 | 52 | pc.printf("Ingrese el mes (entre 1 y 12)"); |
Susanaranjo | 0:1c2ae7e04432 | 53 | pc.scanf("%d", &tm.mon); |
Susanaranjo | 0:1c2ae7e04432 | 54 | pc.printf("Ingrese el anno (ejemplo: 2016)"); |
Susanaranjo | 0:1c2ae7e04432 | 55 | pc.scanf("%d", &tm.year); |
Susanaranjo | 0:1c2ae7e04432 | 56 | pc.printf("Ingrese la hora (entre 0 y 23)"); |
Susanaranjo | 0:1c2ae7e04432 | 57 | pc.scanf("%d", &tm.hour); |
Susanaranjo | 0:1c2ae7e04432 | 58 | pc.printf("Ingrese los minutos (entre 0 y 59)"); |
Susanaranjo | 0:1c2ae7e04432 | 59 | pc.scanf("%d", &tm.min); |
Susanaranjo | 0:1c2ae7e04432 | 60 | pc.printf("Ingrese los segundos (entre 0 y 59)"); |
Susanaranjo | 0:1c2ae7e04432 | 61 | pc.scanf("%d", &tm.sec); |
Susanaranjo | 0:1c2ae7e04432 | 62 | pc.printf("Ingrese el dia (siendo Domingo=1)"); |
Susanaranjo | 0:1c2ae7e04432 | 63 | pc.scanf("%d", &tm.wday); |
Susanaranjo | 0:1c2ae7e04432 | 64 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 65 | pc.printf("Configurando el reloj\n"); |
Susanaranjo | 0:1c2ae7e04432 | 66 | |
Susanaranjo | 0:1c2ae7e04432 | 67 | |
Susanaranjo | 0:1c2ae7e04432 | 68 | while(pc.readable()) { |
Susanaranjo | 0:1c2ae7e04432 | 69 | pc.getc(); |
Susanaranjo | 0:1c2ae7e04432 | 70 | rtc.setTime(tm, false, false);} |
Susanaranjo | 0:1c2ae7e04432 | 71 | |
Susanaranjo | 0:1c2ae7e04432 | 72 | wait_ms(200); |
Susanaranjo | 0:1c2ae7e04432 | 73 | |
Susanaranjo | 0:1c2ae7e04432 | 74 | pc.printf("Inicializando el reloj\n"); |
Susanaranjo | 0:1c2ae7e04432 | 75 | rtc.startClock(); |
Susanaranjo | 0:1c2ae7e04432 | 76 | |
Susanaranjo | 0:1c2ae7e04432 | 77 | wait_ms(200); |
Susanaranjo | 0:1c2ae7e04432 | 78 | |
Susanaranjo | 0:1c2ae7e04432 | 79 | pc.printf("Leyendo el reloj\n"); |
Susanaranjo | 0:1c2ae7e04432 | 80 | |
Susanaranjo | 0:1c2ae7e04432 | 81 | loop1: if (rtc.getTime(tm) ) { |
Susanaranjo | 0:1c2ae7e04432 | 82 | pulsador=alarma.read(); |
Susanaranjo | 0:1c2ae7e04432 | 83 | |
Susanaranjo | 0:1c2ae7e04432 | 84 | a=(abs(tm.hour-hora))+(abs(tm.min-minutos))+(abs(tm.sec-seg))+(abs(tm.mon-mes))+(abs(tm.date-fecha))+(abs(tm.year-anno)); // Compara la hora y la fecha actual con la fecha y hora de la alarma |
Susanaranjo | 0:1c2ae7e04432 | 85 | |
Susanaranjo | 0:1c2ae7e04432 | 86 | //pc.printf("%d\n",a); |
Susanaranjo | 0:1c2ae7e04432 | 87 | |
Susanaranjo | 0:1c2ae7e04432 | 88 | if (a==0) |
Susanaranjo | 0:1c2ae7e04432 | 89 | { |
Susanaranjo | 0:1c2ae7e04432 | 90 | loop3: lcd.cls(); |
Susanaranjo | 0:1c2ae7e04432 | 91 | lcd.printf("ALARMA"); |
Susanaranjo | 0:1c2ae7e04432 | 92 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 93 | pc.printf("* ALARMA *\n"); |
Susanaranjo | 0:1c2ae7e04432 | 94 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 95 | speaker.period(1.0/500.0); |
Susanaranjo | 0:1c2ae7e04432 | 96 | speaker=0.5; |
Susanaranjo | 0:1c2ae7e04432 | 97 | |
Susanaranjo | 0:1c2ae7e04432 | 98 | loop4: pulsador2=snooze.read(); // Estado del pulsador para detener o posponer la alarma |
Susanaranjo | 0:1c2ae7e04432 | 99 | |
Susanaranjo | 0:1c2ae7e04432 | 100 | if (pulsador2==0){ |
Susanaranjo | 0:1c2ae7e04432 | 101 | speaker=0.0; |
Susanaranjo | 0:1c2ae7e04432 | 102 | pc.printf("Pulsar 5 segundos para detener\n"); |
Susanaranjo | 0:1c2ae7e04432 | 103 | lcd.cls(); |
Susanaranjo | 0:1c2ae7e04432 | 104 | lcd.printf("Mantenga pulsado"); |
Susanaranjo | 0:1c2ae7e04432 | 105 | lcd.locate(0,1); |
Susanaranjo | 0:1c2ae7e04432 | 106 | lcd.printf("para detener"); |
Susanaranjo | 0:1c2ae7e04432 | 107 | wait(5); |
Susanaranjo | 0:1c2ae7e04432 | 108 | pulsador2=snooze.read(); |
Susanaranjo | 0:1c2ae7e04432 | 109 | if (pulsador2==0){ // Si despues de 5 segundos el pulsador todavia está oprimido, se detiene la alarma |
Susanaranjo | 0:1c2ae7e04432 | 110 | lcd.cls(); |
Susanaranjo | 0:1c2ae7e04432 | 111 | lcd.printf("Alarma"); |
Susanaranjo | 0:1c2ae7e04432 | 112 | lcd.locate(0,1); |
Susanaranjo | 0:1c2ae7e04432 | 113 | lcd.printf("desactivada"); |
Susanaranjo | 0:1c2ae7e04432 | 114 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 115 | pc.printf("* Alarma desactivada *\n"); |
Susanaranjo | 0:1c2ae7e04432 | 116 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 117 | wait(2); |
Susanaranjo | 0:1c2ae7e04432 | 118 | goto loop1; |
Susanaranjo | 0:1c2ae7e04432 | 119 | }else{ // Si el pulsador ya no está oprimido, se pospone la alarma un minuto |
Susanaranjo | 0:1c2ae7e04432 | 120 | lcd.cls(); |
Susanaranjo | 0:1c2ae7e04432 | 121 | lcd.printf("Snooze"); |
Susanaranjo | 0:1c2ae7e04432 | 122 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 123 | pc.printf("* Snooze *\n"); |
Susanaranjo | 0:1c2ae7e04432 | 124 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 125 | wait(60); |
Susanaranjo | 0:1c2ae7e04432 | 126 | goto loop3; |
Susanaranjo | 0:1c2ae7e04432 | 127 | } |
Susanaranjo | 0:1c2ae7e04432 | 128 | |
Susanaranjo | 0:1c2ae7e04432 | 129 | }else { |
Susanaranjo | 0:1c2ae7e04432 | 130 | goto loop4; } |
Susanaranjo | 0:1c2ae7e04432 | 131 | } |
Susanaranjo | 0:1c2ae7e04432 | 132 | |
Susanaranjo | 0:1c2ae7e04432 | 133 | //pc.printf("La hora actual es: %02d:%02d:%02d\n", tm.hour, tm.min, tm.sec); |
Susanaranjo | 0:1c2ae7e04432 | 134 | //pc.printf("La fecha actual es : %s, %02d/%02d/%04d\n", rtc.weekdayToString(tm.wday), tm.mon, tm.date, tm.year); |
Susanaranjo | 0:1c2ae7e04432 | 135 | |
Susanaranjo | 0:1c2ae7e04432 | 136 | else if (pulsador==0) { |
Susanaranjo | 0:1c2ae7e04432 | 137 | lcd.cls(); |
Susanaranjo | 0:1c2ae7e04432 | 138 | lcd.printf("Ver/Configurar"); |
Susanaranjo | 0:1c2ae7e04432 | 139 | lcd.locate(0,1); |
Susanaranjo | 0:1c2ae7e04432 | 140 | lcd.printf("alarma"); |
Susanaranjo | 0:1c2ae7e04432 | 141 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 142 | pc.printf("* alarma - configurar alarma *\n"); |
Susanaranjo | 0:1c2ae7e04432 | 143 | pc.printf("* ver - ver alarma *\n"); |
Susanaranjo | 0:1c2ae7e04432 | 144 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 145 | |
Susanaranjo | 0:1c2ae7e04432 | 146 | loop2: readptr = 0; |
Susanaranjo | 0:1c2ae7e04432 | 147 | |
Susanaranjo | 0:1c2ae7e04432 | 148 | //Obtengo la cadena de caracteres correspondiente a la orden |
Susanaranjo | 0:1c2ae7e04432 | 149 | |
Susanaranjo | 0:1c2ae7e04432 | 150 | while( (c = pc.getc()) != '\n') { |
Susanaranjo | 0:1c2ae7e04432 | 151 | buffer[readptr++] = c; } |
Susanaranjo | 0:1c2ae7e04432 | 152 | buffer[readptr++] = 0; |
Susanaranjo | 0:1c2ae7e04432 | 153 | |
Susanaranjo | 0:1c2ae7e04432 | 154 | if (strncmp(buffer, "alarma", 6) == 0) { |
Susanaranjo | 0:1c2ae7e04432 | 155 | |
Susanaranjo | 0:1c2ae7e04432 | 156 | lcd.cls(); |
Susanaranjo | 0:1c2ae7e04432 | 157 | lcd.printf("Configurando"); |
Susanaranjo | 0:1c2ae7e04432 | 158 | lcd.locate(0,1); |
Susanaranjo | 0:1c2ae7e04432 | 159 | lcd.printf("alarma"); |
Susanaranjo | 0:1c2ae7e04432 | 160 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 161 | pc.printf("* Nueva alarma *\n"); |
Susanaranjo | 0:1c2ae7e04432 | 162 | pc.printf("Fecha de la alarma (0..31)"); |
Susanaranjo | 0:1c2ae7e04432 | 163 | pc.scanf("%d", &fecha); |
Susanaranjo | 0:1c2ae7e04432 | 164 | pc.printf("Mes de la alarma (1..12)"); |
Susanaranjo | 0:1c2ae7e04432 | 165 | pc.scanf("%d", &mes); |
Susanaranjo | 0:1c2ae7e04432 | 166 | pc.printf("Anno de la alarma"); |
Susanaranjo | 0:1c2ae7e04432 | 167 | pc.scanf("%d", &anno); |
Susanaranjo | 0:1c2ae7e04432 | 168 | pc.printf("Hora de la alarma (0..23)"); |
Susanaranjo | 0:1c2ae7e04432 | 169 | pc.scanf("%d", &hora); |
Susanaranjo | 0:1c2ae7e04432 | 170 | pc.printf("Minutos de la alarma (0..59)"); |
Susanaranjo | 0:1c2ae7e04432 | 171 | pc.scanf("%d",&minutos); |
Susanaranjo | 0:1c2ae7e04432 | 172 | pc.printf("Segundos de la alarma (0..59)"); |
Susanaranjo | 0:1c2ae7e04432 | 173 | pc.scanf("%d", &seg); |
Susanaranjo | 0:1c2ae7e04432 | 174 | pc.printf("* Fin de la configuracion *\n"); |
Susanaranjo | 0:1c2ae7e04432 | 175 | pc.printf("*************************************\n"); |
Susanaranjo | 0:1c2ae7e04432 | 176 | lcd.cls(); |
Susanaranjo | 0:1c2ae7e04432 | 177 | lcd.printf("Fin de la configuracion"); |
Susanaranjo | 0:1c2ae7e04432 | 178 | wait_ms(500); |
Susanaranjo | 0:1c2ae7e04432 | 179 | goto loop1; |
Susanaranjo | 0:1c2ae7e04432 | 180 | |
Susanaranjo | 0:1c2ae7e04432 | 181 | } else if (strncmp(buffer, "ver", 3) == 0) { |
Susanaranjo | 0:1c2ae7e04432 | 182 | |
Susanaranjo | 0:1c2ae7e04432 | 183 | if(anno==0){ |
Susanaranjo | 0:1c2ae7e04432 | 184 | pc.printf("No hay alarma\n"); |
Susanaranjo | 0:1c2ae7e04432 | 185 | lcd.cls(); |
Susanaranjo | 0:1c2ae7e04432 | 186 | lcd.printf("No hay alarma"); |
Susanaranjo | 0:1c2ae7e04432 | 187 | wait_ms(500); |
Susanaranjo | 0:1c2ae7e04432 | 188 | }else{ |
Susanaranjo | 0:1c2ae7e04432 | 189 | pc.printf("Hora de la alarma : %02d:%02d:%02d\n", hora, minutos, seg); |
Susanaranjo | 0:1c2ae7e04432 | 190 | pc.printf("Fecha de la alarma : %02d/%02d/%04d\n", fecha, mes, anno); |
Susanaranjo | 0:1c2ae7e04432 | 191 | lcd.cls(); |
Susanaranjo | 0:1c2ae7e04432 | 192 | lcd.printf("%02d:%02d:%02d", hora, minutos, seg); |
Susanaranjo | 0:1c2ae7e04432 | 193 | lcd.locate(0,1); |
Susanaranjo | 0:1c2ae7e04432 | 194 | lcd.printf("%02d:%02d:%02d", fecha, mes, anno); |
Susanaranjo | 0:1c2ae7e04432 | 195 | wait(2);} |
Susanaranjo | 0:1c2ae7e04432 | 196 | |
Susanaranjo | 0:1c2ae7e04432 | 197 | goto loop1; |
Susanaranjo | 0:1c2ae7e04432 | 198 | |
Susanaranjo | 0:1c2ae7e04432 | 199 | } else { |
Susanaranjo | 0:1c2ae7e04432 | 200 | goto loop2; |
Susanaranjo | 0:1c2ae7e04432 | 201 | } |
Susanaranjo | 0:1c2ae7e04432 | 202 | |
Susanaranjo | 0:1c2ae7e04432 | 203 | |
Susanaranjo | 0:1c2ae7e04432 | 204 | } else { |
Susanaranjo | 0:1c2ae7e04432 | 205 | //Actualizo la hora y la fecha |
Susanaranjo | 0:1c2ae7e04432 | 206 | lcd.cls(); |
Susanaranjo | 0:1c2ae7e04432 | 207 | lcd.locate(0,0); |
Susanaranjo | 0:1c2ae7e04432 | 208 | lcd.printf("%02d:%02d:%02d", tm.hour, tm.min, tm.sec); |
Susanaranjo | 0:1c2ae7e04432 | 209 | lcd.locate(0,1); |
Susanaranjo | 0:1c2ae7e04432 | 210 | lcd.printf("%s/%02d/%02d/%4d", rtc.weekdayToString(tm.wday), tm.date, tm.mon, tm.year); |
Susanaranjo | 0:1c2ae7e04432 | 211 | wait_ms(200); |
Susanaranjo | 0:1c2ae7e04432 | 212 | goto loop1; |
Susanaranjo | 0:1c2ae7e04432 | 213 | } |
Susanaranjo | 0:1c2ae7e04432 | 214 | } |
Susanaranjo | 0:1c2ae7e04432 | 215 | |
Susanaranjo | 0:1c2ae7e04432 | 216 | |
Susanaranjo | 0:1c2ae7e04432 | 217 | |
Susanaranjo | 0:1c2ae7e04432 | 218 | } |
Susanaranjo | 0:1c2ae7e04432 | 219 | } |