![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Reloj despertador
Dependencies: PinDetect QEI RTC-DS1307 TextLCD mbed
main.cpp@0:29267341170e, 2016-05-24 (annotated)
- Committer:
- satelite
- Date:
- Tue May 24 14:02:08 2016 +0000
- Revision:
- 0:29267341170e
Reloj despertador con snooze.; El sistema esta conformado por la tarjeta kl25z, un m?dulo con el RTC DS-1307, un encoder con boton central, un boton, un LCD 2x16 y un buzzer.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
satelite | 0:29267341170e | 1 | /*Reloj despertador con módulo DS1307 |
satelite | 0:29267341170e | 2 | */ |
satelite | 0:29267341170e | 3 | |
satelite | 0:29267341170e | 4 | #include "mbed.h" |
satelite | 0:29267341170e | 5 | #include "PinDetect.h" |
satelite | 0:29267341170e | 6 | #include "QEI.h" |
satelite | 0:29267341170e | 7 | #include "Rtc_Ds1307.h" |
satelite | 0:29267341170e | 8 | #include "TextLCD.h" |
satelite | 0:29267341170e | 9 | |
satelite | 0:29267341170e | 10 | // Configuracion puertos del microcontrolador |
satelite | 0:29267341170e | 11 | Rtc_Ds1307 rtc(PTE0, PTE1); // pines para la comunicacion I2C, SDA, SCL |
satelite | 0:29267341170e | 12 | Serial pc(USBTX, USBRX, "pc"); // puerto serie (USB) |
satelite | 0:29267341170e | 13 | QEI wheel(PTD5, PTD0, NC, 100); // pines para el encoder |
satelite | 0:29267341170e | 14 | PinDetect Boton(PTA13, PullUp); // boton encoder |
satelite | 0:29267341170e | 15 | PinDetect BotonA(PTE20, PullUp); // boton apagar alarma |
satelite | 0:29267341170e | 16 | DigitalOut led1(LED_RED); // led rojo |
satelite | 0:29267341170e | 17 | DigitalOut led2(LED_GREEN); // led verde |
satelite | 0:29267341170e | 18 | DigitalOut led3(LED_BLUE); // led azul |
satelite | 0:29267341170e | 19 | DigitalOut buzzer(PTD3); // alarma |
satelite | 0:29267341170e | 20 | TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // RS, E, D4, D5, D6, D7 |
satelite | 0:29267341170e | 21 | |
satelite | 0:29267341170e | 22 | // Declaracion de variables globales |
satelite | 0:29267341170e | 23 | int estado = 0, config = 0, dia = 1, mes = 1, ano = 2016, hora = 0, min = 0, configR = 0, cont = 1, set = 0, alarma = 0; |
satelite | 0:29267341170e | 24 | |
satelite | 0:29267341170e | 25 | // Rutina presion boton |
satelite | 0:29267341170e | 26 | void keyPressed(void) |
satelite | 0:29267341170e | 27 | { |
satelite | 0:29267341170e | 28 | if(estado == 0 && alarma == 1) { |
satelite | 0:29267341170e | 29 | min++; |
satelite | 0:29267341170e | 30 | } |
satelite | 0:29267341170e | 31 | if(estado == 1 && set == 1) { |
satelite | 0:29267341170e | 32 | config++; |
satelite | 0:29267341170e | 33 | } |
satelite | 0:29267341170e | 34 | if(estado == 2 && set == 1) { |
satelite | 0:29267341170e | 35 | configR++; |
satelite | 0:29267341170e | 36 | cont = 1; |
satelite | 0:29267341170e | 37 | } |
satelite | 0:29267341170e | 38 | } |
satelite | 0:29267341170e | 39 | |
satelite | 0:29267341170e | 40 | // Rutina presion sostenida boton |
satelite | 0:29267341170e | 41 | void keyPressedHeld(void) |
satelite | 0:29267341170e | 42 | { |
satelite | 0:29267341170e | 43 | //wait(0.5); |
satelite | 0:29267341170e | 44 | lcd.locate(0, 0); |
satelite | 0:29267341170e | 45 | lcd.printf(" "); |
satelite | 0:29267341170e | 46 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 47 | lcd.printf(" "); |
satelite | 0:29267341170e | 48 | estado++; |
satelite | 0:29267341170e | 49 | //lcd.cls(); |
satelite | 0:29267341170e | 50 | if(estado > 2) { |
satelite | 0:29267341170e | 51 | estado = 0; |
satelite | 0:29267341170e | 52 | } |
satelite | 0:29267341170e | 53 | } |
satelite | 0:29267341170e | 54 | |
satelite | 0:29267341170e | 55 | // Rutina para apagar la alarma |
satelite | 0:29267341170e | 56 | void apagarAlarma(void) |
satelite | 0:29267341170e | 57 | { |
satelite | 0:29267341170e | 58 | min--; |
satelite | 0:29267341170e | 59 | } |
satelite | 0:29267341170e | 60 | // Rutina principal |
satelite | 0:29267341170e | 61 | int main() |
satelite | 0:29267341170e | 62 | { |
satelite | 0:29267341170e | 63 | // Configuracion de inicio y bienvenida |
satelite | 0:29267341170e | 64 | Rtc_Ds1307::Time_rtc tm = {}; |
satelite | 0:29267341170e | 65 | Boton.setAssertValue(0); |
satelite | 0:29267341170e | 66 | Boton.attach_asserted(&keyPressed); |
satelite | 0:29267341170e | 67 | Boton.attach_asserted_held(&keyPressedHeld); |
satelite | 0:29267341170e | 68 | Boton.setSamplesTillHeld(200); |
satelite | 0:29267341170e | 69 | Boton.setSampleFrequency(); |
satelite | 0:29267341170e | 70 | BotonA.setAssertValue(0); |
satelite | 0:29267341170e | 71 | BotonA.attach_asserted_held(&apagarAlarma); |
satelite | 0:29267341170e | 72 | BotonA.setSamplesTillHeld(200); |
satelite | 0:29267341170e | 73 | BotonA.setSampleFrequency(); |
satelite | 0:29267341170e | 74 | led1 = 1; |
satelite | 0:29267341170e | 75 | led2 = 1; |
satelite | 0:29267341170e | 76 | led3 = 1; |
satelite | 0:29267341170e | 77 | buzzer = 0; |
satelite | 0:29267341170e | 78 | lcd.printf(" Reloj"); |
satelite | 0:29267341170e | 79 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 80 | lcd.printf(" despertador"); |
satelite | 0:29267341170e | 81 | wait(3); |
satelite | 0:29267341170e | 82 | lcd.locate(0, 0); |
satelite | 0:29267341170e | 83 | lcd.printf(" "); |
satelite | 0:29267341170e | 84 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 85 | lcd.printf(" "); |
satelite | 0:29267341170e | 86 | wait(0.5); |
satelite | 0:29267341170e | 87 | |
satelite | 0:29267341170e | 88 | // Ciclo infinito |
satelite | 0:29267341170e | 89 | while(1) { |
satelite | 0:29267341170e | 90 | // Reloj en funcionamiento |
satelite | 0:29267341170e | 91 | while(estado == 0) { |
satelite | 0:29267341170e | 92 | led1 = 1; |
satelite | 0:29267341170e | 93 | led2 = 0; |
satelite | 0:29267341170e | 94 | led3 = 1; |
satelite | 0:29267341170e | 95 | rtc.getTime(tm); |
satelite | 0:29267341170e | 96 | lcd.locate(0, 0); |
satelite | 0:29267341170e | 97 | lcd.printf(" %02d:%02d:%02d ", tm.hour, tm.min, tm.sec); |
satelite | 0:29267341170e | 98 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 99 | lcd.printf(" %s %02d/%02d/%04d", rtc.weekdayToString(tm.wday), tm.date, tm.mon, tm.year); |
satelite | 0:29267341170e | 100 | if(tm.hour == hora && tm.min == min && tm.mon == mes && tm.date == dia && tm.year == ano) { |
satelite | 0:29267341170e | 101 | buzzer = !buzzer; |
satelite | 0:29267341170e | 102 | alarma = 1; |
satelite | 0:29267341170e | 103 | } else { |
satelite | 0:29267341170e | 104 | buzzer = 0; |
satelite | 0:29267341170e | 105 | alarma = 0; |
satelite | 0:29267341170e | 106 | } |
satelite | 0:29267341170e | 107 | if(tm.hour <= hora && tm.min <= min && tm.mon <= mes && tm.date <= dia && tm.year <= ano) { |
satelite | 0:29267341170e | 108 | lcd.locate(15, 0); |
satelite | 0:29267341170e | 109 | lcd.printf("@"); |
satelite | 0:29267341170e | 110 | } else { |
satelite | 0:29267341170e | 111 | lcd.locate(15, 0); |
satelite | 0:29267341170e | 112 | lcd.printf(" "); |
satelite | 0:29267341170e | 113 | } |
satelite | 0:29267341170e | 114 | wait(1); |
satelite | 0:29267341170e | 115 | } |
satelite | 0:29267341170e | 116 | // Configuracion de la alarma |
satelite | 0:29267341170e | 117 | while(estado == 1) { |
satelite | 0:29267341170e | 118 | led1 = 1; |
satelite | 0:29267341170e | 119 | led2 = 1; |
satelite | 0:29267341170e | 120 | led3 = 0; |
satelite | 0:29267341170e | 121 | wait(0.2); |
satelite | 0:29267341170e | 122 | if(config == 0) { |
satelite | 0:29267341170e | 123 | lcd.locate(0, 0); |
satelite | 0:29267341170e | 124 | lcd.printf(" Establecer"); |
satelite | 0:29267341170e | 125 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 126 | lcd.printf("alarma: "); |
satelite | 0:29267341170e | 127 | set = set + wheel.getPulses(); |
satelite | 0:29267341170e | 128 | wheel.reset(); |
satelite | 0:29267341170e | 129 | if(set > 1) { |
satelite | 0:29267341170e | 130 | set = 0; |
satelite | 0:29267341170e | 131 | } |
satelite | 0:29267341170e | 132 | if(set < 0) { |
satelite | 0:29267341170e | 133 | set = 1; |
satelite | 0:29267341170e | 134 | } |
satelite | 0:29267341170e | 135 | lcd.locate(8, 1); |
satelite | 0:29267341170e | 136 | if(set == 0) { |
satelite | 0:29267341170e | 137 | lcd.printf("NO "); |
satelite | 0:29267341170e | 138 | } else if(set == 1) { |
satelite | 0:29267341170e | 139 | lcd.printf("SI "); |
satelite | 0:29267341170e | 140 | } |
satelite | 0:29267341170e | 141 | } |
satelite | 0:29267341170e | 142 | if(config == 1) { |
satelite | 0:29267341170e | 143 | dia = dia + wheel.getPulses(); |
satelite | 0:29267341170e | 144 | wheel.reset(); |
satelite | 0:29267341170e | 145 | if(dia > 31) { |
satelite | 0:29267341170e | 146 | dia = 1; |
satelite | 0:29267341170e | 147 | } |
satelite | 0:29267341170e | 148 | if(dia < 1) { |
satelite | 0:29267341170e | 149 | dia = 31; |
satelite | 0:29267341170e | 150 | } |
satelite | 0:29267341170e | 151 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 152 | lcd.printf("Dia: %02d ", dia); |
satelite | 0:29267341170e | 153 | } |
satelite | 0:29267341170e | 154 | if(config == 2) { |
satelite | 0:29267341170e | 155 | mes = mes + wheel.getPulses(); |
satelite | 0:29267341170e | 156 | wheel.reset(); |
satelite | 0:29267341170e | 157 | if(mes > 12) { |
satelite | 0:29267341170e | 158 | mes = 1; |
satelite | 0:29267341170e | 159 | } |
satelite | 0:29267341170e | 160 | if(mes < 1) { |
satelite | 0:29267341170e | 161 | mes = 12; |
satelite | 0:29267341170e | 162 | } |
satelite | 0:29267341170e | 163 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 164 | lcd.printf("Mes: %02d ", mes); |
satelite | 0:29267341170e | 165 | } |
satelite | 0:29267341170e | 166 | if(config == 3) { |
satelite | 0:29267341170e | 167 | ano = ano + wheel.getPulses(); |
satelite | 0:29267341170e | 168 | wheel.reset(); |
satelite | 0:29267341170e | 169 | if(ano > 3000) { |
satelite | 0:29267341170e | 170 | ano = 2016; |
satelite | 0:29267341170e | 171 | } |
satelite | 0:29267341170e | 172 | if(ano < 2016) { |
satelite | 0:29267341170e | 173 | ano = 3000; |
satelite | 0:29267341170e | 174 | } |
satelite | 0:29267341170e | 175 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 176 | lcd.printf("Anno: %02d ", ano); |
satelite | 0:29267341170e | 177 | } |
satelite | 0:29267341170e | 178 | if(config == 4) { |
satelite | 0:29267341170e | 179 | hora = hora + wheel.getPulses(); |
satelite | 0:29267341170e | 180 | wheel.reset(); |
satelite | 0:29267341170e | 181 | if(hora > 23) { |
satelite | 0:29267341170e | 182 | hora = 0; |
satelite | 0:29267341170e | 183 | } |
satelite | 0:29267341170e | 184 | if(hora < 0) { |
satelite | 0:29267341170e | 185 | hora = 23; |
satelite | 0:29267341170e | 186 | } |
satelite | 0:29267341170e | 187 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 188 | lcd.printf("Hora: %02d ", hora); |
satelite | 0:29267341170e | 189 | } |
satelite | 0:29267341170e | 190 | if(config == 5) { |
satelite | 0:29267341170e | 191 | min = min + wheel.getPulses(); |
satelite | 0:29267341170e | 192 | wheel.reset(); |
satelite | 0:29267341170e | 193 | if(min > 59) { |
satelite | 0:29267341170e | 194 | min = 0; |
satelite | 0:29267341170e | 195 | } |
satelite | 0:29267341170e | 196 | if(min < 0) { |
satelite | 0:29267341170e | 197 | min = 59; |
satelite | 0:29267341170e | 198 | } |
satelite | 0:29267341170e | 199 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 200 | lcd.printf("Minutos: %02d ", min); |
satelite | 0:29267341170e | 201 | } |
satelite | 0:29267341170e | 202 | if(config == 6) { |
satelite | 0:29267341170e | 203 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 204 | lcd.printf(" Establecida "); |
satelite | 0:29267341170e | 205 | estado = 0; |
satelite | 0:29267341170e | 206 | config = 0; |
satelite | 0:29267341170e | 207 | wait(2); |
satelite | 0:29267341170e | 208 | } |
satelite | 0:29267341170e | 209 | } |
satelite | 0:29267341170e | 210 | // Configuracion del reloj |
satelite | 0:29267341170e | 211 | while(estado == 2) { |
satelite | 0:29267341170e | 212 | led1 = 0; |
satelite | 0:29267341170e | 213 | led2 = 1; |
satelite | 0:29267341170e | 214 | led3 = 1; |
satelite | 0:29267341170e | 215 | wait(0.2); |
satelite | 0:29267341170e | 216 | if(configR == 0) { |
satelite | 0:29267341170e | 217 | lcd.locate(0, 0); |
satelite | 0:29267341170e | 218 | lcd.printf("Configurar reloj"); |
satelite | 0:29267341170e | 219 | set = set + wheel.getPulses(); |
satelite | 0:29267341170e | 220 | wheel.reset(); |
satelite | 0:29267341170e | 221 | if(set > 1) { |
satelite | 0:29267341170e | 222 | set = 0; |
satelite | 0:29267341170e | 223 | } |
satelite | 0:29267341170e | 224 | if(set < 0) { |
satelite | 0:29267341170e | 225 | set = 1; |
satelite | 0:29267341170e | 226 | } |
satelite | 0:29267341170e | 227 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 228 | if(set == 0) { |
satelite | 0:29267341170e | 229 | lcd.printf(" NO "); |
satelite | 0:29267341170e | 230 | } else if(set == 1) { |
satelite | 0:29267341170e | 231 | lcd.printf(" SI "); |
satelite | 0:29267341170e | 232 | } |
satelite | 0:29267341170e | 233 | } |
satelite | 0:29267341170e | 234 | if(configR == 1) { |
satelite | 0:29267341170e | 235 | cont = cont + wheel.getPulses(); |
satelite | 0:29267341170e | 236 | wheel.reset(); |
satelite | 0:29267341170e | 237 | if(cont > 31) { |
satelite | 0:29267341170e | 238 | cont = 1; |
satelite | 0:29267341170e | 239 | } |
satelite | 0:29267341170e | 240 | if(cont < 1) { |
satelite | 0:29267341170e | 241 | cont = 31; |
satelite | 0:29267341170e | 242 | } |
satelite | 0:29267341170e | 243 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 244 | lcd.printf("Dia: %02d ", cont); |
satelite | 0:29267341170e | 245 | tm.date = cont; |
satelite | 0:29267341170e | 246 | } |
satelite | 0:29267341170e | 247 | if(configR == 2) { |
satelite | 0:29267341170e | 248 | cont = cont + wheel.getPulses(); |
satelite | 0:29267341170e | 249 | wheel.reset(); |
satelite | 0:29267341170e | 250 | if(cont > 12) { |
satelite | 0:29267341170e | 251 | cont = 1; |
satelite | 0:29267341170e | 252 | } |
satelite | 0:29267341170e | 253 | if(cont < 1) { |
satelite | 0:29267341170e | 254 | cont = 12; |
satelite | 0:29267341170e | 255 | } |
satelite | 0:29267341170e | 256 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 257 | lcd.printf("Mes: %02d ", cont); |
satelite | 0:29267341170e | 258 | tm.mon = cont; |
satelite | 0:29267341170e | 259 | } |
satelite | 0:29267341170e | 260 | if(configR == 3) { |
satelite | 0:29267341170e | 261 | cont = cont + wheel.getPulses(); |
satelite | 0:29267341170e | 262 | wheel.reset(); |
satelite | 0:29267341170e | 263 | if(cont > 3000) { |
satelite | 0:29267341170e | 264 | cont = 2016; |
satelite | 0:29267341170e | 265 | } |
satelite | 0:29267341170e | 266 | if(cont < 2016) { |
satelite | 0:29267341170e | 267 | cont = 2016; |
satelite | 0:29267341170e | 268 | } |
satelite | 0:29267341170e | 269 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 270 | lcd.printf("Anno: %02d ", cont); |
satelite | 0:29267341170e | 271 | tm.year = cont; |
satelite | 0:29267341170e | 272 | } |
satelite | 0:29267341170e | 273 | if(configR == 4) { |
satelite | 0:29267341170e | 274 | cont = cont + wheel.getPulses(); |
satelite | 0:29267341170e | 275 | wheel.reset(); |
satelite | 0:29267341170e | 276 | if(cont > 7) { |
satelite | 0:29267341170e | 277 | cont = 1; |
satelite | 0:29267341170e | 278 | } |
satelite | 0:29267341170e | 279 | if(cont < 1) { |
satelite | 0:29267341170e | 280 | cont = 7; |
satelite | 0:29267341170e | 281 | } |
satelite | 0:29267341170e | 282 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 283 | lcd.printf("Dia semana: %1d", cont); |
satelite | 0:29267341170e | 284 | tm.wday = cont; |
satelite | 0:29267341170e | 285 | } |
satelite | 0:29267341170e | 286 | if(configR == 5) { |
satelite | 0:29267341170e | 287 | cont = cont + wheel.getPulses(); |
satelite | 0:29267341170e | 288 | wheel.reset(); |
satelite | 0:29267341170e | 289 | if(cont > 23) { |
satelite | 0:29267341170e | 290 | cont = 0; |
satelite | 0:29267341170e | 291 | } |
satelite | 0:29267341170e | 292 | if(cont < 0) { |
satelite | 0:29267341170e | 293 | cont = 23; |
satelite | 0:29267341170e | 294 | } |
satelite | 0:29267341170e | 295 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 296 | lcd.printf("Hora: %02d ", cont); |
satelite | 0:29267341170e | 297 | tm.hour = cont; |
satelite | 0:29267341170e | 298 | } |
satelite | 0:29267341170e | 299 | if(configR == 6) { |
satelite | 0:29267341170e | 300 | cont = cont + wheel.getPulses(); |
satelite | 0:29267341170e | 301 | wheel.reset(); |
satelite | 0:29267341170e | 302 | if(cont > 59) { |
satelite | 0:29267341170e | 303 | cont = 0; |
satelite | 0:29267341170e | 304 | } |
satelite | 0:29267341170e | 305 | if(cont < 0) { |
satelite | 0:29267341170e | 306 | cont = 59; |
satelite | 0:29267341170e | 307 | } |
satelite | 0:29267341170e | 308 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 309 | lcd.printf("Minutos: %02d ", cont); |
satelite | 0:29267341170e | 310 | tm.min = cont; |
satelite | 0:29267341170e | 311 | } |
satelite | 0:29267341170e | 312 | if(configR == 7) { |
satelite | 0:29267341170e | 313 | cont = cont + wheel.getPulses(); |
satelite | 0:29267341170e | 314 | wheel.reset(); |
satelite | 0:29267341170e | 315 | if(cont > 59) { |
satelite | 0:29267341170e | 316 | cont = 0; |
satelite | 0:29267341170e | 317 | } |
satelite | 0:29267341170e | 318 | if(cont < 0) { |
satelite | 0:29267341170e | 319 | cont = 59; |
satelite | 0:29267341170e | 320 | } |
satelite | 0:29267341170e | 321 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 322 | lcd.printf("Segundos: %02d ", cont); |
satelite | 0:29267341170e | 323 | tm.sec = cont; |
satelite | 0:29267341170e | 324 | } |
satelite | 0:29267341170e | 325 | if(configR == 8) { |
satelite | 0:29267341170e | 326 | lcd.locate(0, 1); |
satelite | 0:29267341170e | 327 | lcd.printf(" Establecido "); |
satelite | 0:29267341170e | 328 | rtc.setTime(tm, false, false); |
satelite | 0:29267341170e | 329 | rtc.startClock(); |
satelite | 0:29267341170e | 330 | estado = 0; |
satelite | 0:29267341170e | 331 | configR = 0; |
satelite | 0:29267341170e | 332 | wait(2); |
satelite | 0:29267341170e | 333 | } |
satelite | 0:29267341170e | 334 | } |
satelite | 0:29267341170e | 335 | } |
satelite | 0:29267341170e | 336 | } |