![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
trabajo de creacion de un despertador por i2c configurado por un encoder
Dependencies: DebouncedIn QEI RTC-DS1307 mbed
Fork of I2C by
main.cpp@5:d38b972a1813, 2015-11-23 (annotated)
- Committer:
- GermanD
- Date:
- Mon Nov 23 14:44:24 2015 +0000
- Revision:
- 5:d38b972a1813
- Parent:
- 4:fa48e78e9bba
Tarea para configurar una alarma despertadora en el mes, a?o y hora que elijamos
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leihen | 0:431183c5b136 | 1 | #include "mbed.h" |
leihen | 0:431183c5b136 | 2 | #include "Rtc_Ds1307.h" |
demo71 | 3:9e5e0b8d0e2a | 3 | #include "DebouncedIn.h" |
demo71 | 3:9e5e0b8d0e2a | 4 | #include "QEI.h" |
GermanD | 4:fa48e78e9bba | 5 | |
GermanD | 4:fa48e78e9bba | 6 | Rtc_Ds1307 rtc(PTE0,PTE1); //Puertos para el modulo |
GermanD | 4:fa48e78e9bba | 7 | Serial pc(USBTX, USBRX, "pc"); //Puertos para el PC |
GermanD | 4:fa48e78e9bba | 8 | QEI wheel (PTD5, PTD0, NC, 100); //Configuracion encoder |
GermanD | 4:fa48e78e9bba | 9 | DebouncedIn Boton(PTA13); //Boton Encoder |
GermanD | 5:d38b972a1813 | 10 | DigitalOut led1(PTD2); //Salida |
demo71 | 3:9e5e0b8d0e2a | 11 | |
GermanD | 4:fa48e78e9bba | 12 | int Dia=0; |
GermanD | 4:fa48e78e9bba | 13 | int Mes=0; |
GermanD | 4:fa48e78e9bba | 14 | int Anio=2015; |
GermanD | 4:fa48e78e9bba | 15 | int Horas=0; |
GermanD | 4:fa48e78e9bba | 16 | int Minutos=0; |
GermanD | 4:fa48e78e9bba | 17 | int Segundos=0; |
GermanD | 4:fa48e78e9bba | 18 | |
leihen | 0:431183c5b136 | 19 | char buffer[128]; |
leihen | 0:431183c5b136 | 20 | int readptr = 0; |
GermanD | 4:fa48e78e9bba | 21 | |
GermanD | 4:fa48e78e9bba | 22 | int main() |
GermanD | 4:fa48e78e9bba | 23 | { |
leihen | 0:431183c5b136 | 24 | char c; |
leihen | 2:3be003301107 | 25 | Rtc_Ds1307::Time_rtc tm = {}; |
leihen | 0:431183c5b136 | 26 | |
GermanD | 4:fa48e78e9bba | 27 | while(1) |
GermanD | 4:fa48e78e9bba | 28 | { |
GermanD | 4:fa48e78e9bba | 29 | pc.printf("*************************************\n"); |
GermanD | 4:fa48e78e9bba | 30 | pc.printf("* Menu for RTC Test : *\n"); |
GermanD | 5:d38b972a1813 | 31 | pc.printf("* leer - reads the clock *\n"); |
GermanD | 5:d38b972a1813 | 32 | pc.printf("* empezar - start the clock *\n"); |
GermanD | 5:d38b972a1813 | 33 | pc.printf("* parar - stop the clock *\n"); |
GermanD | 5:d38b972a1813 | 34 | pc.printf("* configurar - write the clock *\n"); |
GermanD | 4:fa48e78e9bba | 35 | pc.printf("* ena - enable Square wave output *\n"); |
GermanD | 4:fa48e78e9bba | 36 | pc.printf("* dis - disable square wave outp. *\n"); |
GermanD | 4:fa48e78e9bba | 37 | pc.printf("*************************************\n"); |
demo71 | 3:9e5e0b8d0e2a | 38 | |
GermanD | 4:fa48e78e9bba | 39 | while( (c = pc.getc()) != '\n') |
GermanD | 4:fa48e78e9bba | 40 | { |
GermanD | 4:fa48e78e9bba | 41 | buffer[readptr++] = c; |
leihen | 0:431183c5b136 | 42 | } |
leihen | 0:431183c5b136 | 43 | buffer[readptr++] = 0; |
GermanD | 5:d38b972a1813 | 44 | |
GermanD | 5:d38b972a1813 | 45 | // para leer los valores que tiene el reloj |
GermanD | 5:d38b972a1813 | 46 | |
GermanD | 5:d38b972a1813 | 47 | if (strncmp(buffer, "leer", 4) == 0) |
GermanD | 4:fa48e78e9bba | 48 | { |
leihen | 0:431183c5b136 | 49 | pc.printf("Performing read operation\n"); |
GermanD | 4:fa48e78e9bba | 50 | if (rtc.getTime(tm) ) |
GermanD | 4:fa48e78e9bba | 51 | { |
GermanD | 5:d38b972a1813 | 52 | pc.printf("La hora es: %02d:%02d:%02d\n", tm.hour, tm.min, tm.sec); // coloca en pantalla la hora actual que hay almacenada |
GermanD | 5:d38b972a1813 | 53 | pc.printf("La fecha es: %s, %02d/%02d/%04d\n", rtc.weekdayToString(tm.wday), tm.mon, tm.date, tm.year); // coloca en pantalla la fecha actual que hay almacenada |
GermanD | 4:fa48e78e9bba | 54 | } |
leihen | 0:431183c5b136 | 55 | |
GermanD | 4:fa48e78e9bba | 56 | } |
GermanD | 5:d38b972a1813 | 57 | else if (strncmp(buffer, "configurar", 5) == 0) // se empieza por configurar el reloj interno |
GermanD | 4:fa48e78e9bba | 58 | pc.printf("Dia (0..31)"); |
GermanD | 5:d38b972a1813 | 59 | pc.scanf("%d", &tm.date); // coloca lo que uno le ingrese como dia |
GermanD | 4:fa48e78e9bba | 60 | pc.printf("Mes (1..12)"); |
GermanD | 5:d38b972a1813 | 61 | pc.scanf("%d", &tm.mon); // coloca lo que uno le ingrese como mes |
GermanD | 5:d38b972a1813 | 62 | pc.printf("Anio"); |
GermanD | 5:d38b972a1813 | 63 | pc.scanf("%d", &tm.year); // coloca lo que uno le ingrese como año |
GermanD | 4:fa48e78e9bba | 64 | pc.printf("Hora (0..23)"); |
GermanD | 5:d38b972a1813 | 65 | pc.scanf("%d", &tm.hour); // coloca lo que uno le ingrese como hora |
GermanD | 4:fa48e78e9bba | 66 | pc.printf("Minutos (0..59)"); |
GermanD | 5:d38b972a1813 | 67 | pc.scanf("%d", &tm.min); // coloca lo que uno le ingrese como minutos |
GermanD | 4:fa48e78e9bba | 68 | pc.printf("Segundos (0..59)"); |
GermanD | 5:d38b972a1813 | 69 | pc.scanf("%d", &tm.sec); // coloca lo que uno le ingrese como segundos |
leihen | 0:431183c5b136 | 70 | pc.printf("Performing write operation\n"); |
leihen | 1:6dbe51fe0737 | 71 | |
leihen | 1:6dbe51fe0737 | 72 | while(pc.readable()) |
GermanD | 4:fa48e78e9bba | 73 | pc.getc(); |
leihen | 1:6dbe51fe0737 | 74 | rtc.setTime(tm, false, false); |
GermanD | 4:fa48e78e9bba | 75 | } |
GermanD | 5:d38b972a1813 | 76 | |
GermanD | 5:d38b972a1813 | 77 | // para empezar a correr el reloj que se programó arriba |
GermanD | 5:d38b972a1813 | 78 | |
GermanD | 5:d38b972a1813 | 79 | else if (strncmp(buffer, "empezar", 5) == 0) |
GermanD | 4:fa48e78e9bba | 80 | { |
demo71 | 3:9e5e0b8d0e2a | 81 | readptr = 0; |
demo71 | 3:9e5e0b8d0e2a | 82 | pc.printf("Dispositivo Iniciado\n"); |
leihen | 1:6dbe51fe0737 | 83 | rtc.startClock(); |
GermanD | 5:d38b972a1813 | 84 | goto alarma; |
GermanD | 4:fa48e78e9bba | 85 | |
leihen | 0:431183c5b136 | 86 | } |
GermanD | 5:d38b972a1813 | 87 | else if (strncmp(buffer, "parar", 4) == 0) // para parar el reloj |
GermanD | 4:fa48e78e9bba | 88 | { |
leihen | 0:431183c5b136 | 89 | pc.printf("Performing stop operation\n"); |
leihen | 1:6dbe51fe0737 | 90 | rtc.stopClock(); |
leihen | 1:6dbe51fe0737 | 91 | } |
GermanD | 4:fa48e78e9bba | 92 | else if (strncmp(buffer, "ena", 3) == 0) |
GermanD | 4:fa48e78e9bba | 93 | { |
leihen | 1:6dbe51fe0737 | 94 | int rs; |
leihen | 1:6dbe51fe0737 | 95 | pc.printf("Please specify the frequency : [0 = 1Hz, 1 = 4.096kHz, 2 = 8.192kHz, 3 = 32.768kHz] "); |
leihen | 1:6dbe51fe0737 | 96 | scanf("%d", &rs); |
leihen | 1:6dbe51fe0737 | 97 | pc.printf("Enabling the output with %d option\n", rs); |
leihen | 2:3be003301107 | 98 | rtc.setSquareWaveOutput(true, (Rtc_Ds1307::SqwRateSelect_t)rs); |
leihen | 1:6dbe51fe0737 | 99 | } |
GermanD | 4:fa48e78e9bba | 100 | else if (strncmp(buffer, "dis", 3) == 0) |
GermanD | 4:fa48e78e9bba | 101 | { |
leihen | 1:6dbe51fe0737 | 102 | pc.printf("Disableing square wave output\n"); |
leihen | 2:3be003301107 | 103 | rtc.setSquareWaveOutput(false, Rtc_Ds1307::RS1Hz); |
leihen | 0:431183c5b136 | 104 | } |
GermanD | 4:fa48e78e9bba | 105 | else |
GermanD | 4:fa48e78e9bba | 106 | { |
leihen | 0:431183c5b136 | 107 | pc.printf("syntax error\n"); |
leihen | 0:431183c5b136 | 108 | } |
leihen | 0:431183c5b136 | 109 | readptr = 0; |
demo71 | 3:9e5e0b8d0e2a | 110 | } |
GermanD | 5:d38b972a1813 | 111 | |
GermanD | 5:d38b972a1813 | 112 | //se inicia a configurar la alarma |
GermanD | 5:d38b972a1813 | 113 | |
GermanD | 5:d38b972a1813 | 114 | alarma: |
GermanD | 4:fa48e78e9bba | 115 | pc.printf("Configuracion de la alarma\n"); |
demo71 | 3:9e5e0b8d0e2a | 116 | |
demo71 | 3:9e5e0b8d0e2a | 117 | |
GermanD | 5:d38b972a1813 | 118 | Dia: //empieza con el dia |
GermanD | 4:fa48e78e9bba | 119 | while(1) |
GermanD | 4:fa48e78e9bba | 120 | { |
GermanD | 4:fa48e78e9bba | 121 | Dia=Dia+wheel.getPulses(); //con el encoder se aumenta el dia |
demo71 | 3:9e5e0b8d0e2a | 122 | wheel.reset(); |
demo71 | 3:9e5e0b8d0e2a | 123 | |
GermanD | 4:fa48e78e9bba | 124 | if(Dia>=31) |
GermanD | 4:fa48e78e9bba | 125 | { //como el dia no tiene mas de 31 dias, se mantiene en 31 al ingresar un valor diferente |
GermanD | 4:fa48e78e9bba | 126 | Dia=31; |
demo71 | 3:9e5e0b8d0e2a | 127 | } |
GermanD | 4:fa48e78e9bba | 128 | else if (Dia<=0) |
GermanD | 5:d38b972a1813 | 129 | { //no se pueden ingresar numeros negativos |
GermanD | 4:fa48e78e9bba | 130 | Dia=0; |
demo71 | 3:9e5e0b8d0e2a | 131 | } |
GermanD | 5:d38b972a1813 | 132 | pc.printf("Dia= %d\n",Dia); //muestra el valor |
demo71 | 3:9e5e0b8d0e2a | 133 | |
demo71 | 3:9e5e0b8d0e2a | 134 | |
GermanD | 4:fa48e78e9bba | 135 | if(Boton.falling()) |
GermanD | 5:d38b972a1813 | 136 | { //presionando el encoder cambiamos al configurar el mes |
GermanD | 5:d38b972a1813 | 137 | goto Mes; |
demo71 | 3:9e5e0b8d0e2a | 138 | } |
demo71 | 3:9e5e0b8d0e2a | 139 | } |
demo71 | 3:9e5e0b8d0e2a | 140 | |
GermanD | 5:d38b972a1813 | 141 | Mes: //Configuracion del Mes para la alarma |
GermanD | 4:fa48e78e9bba | 142 | |
GermanD | 4:fa48e78e9bba | 143 | while(1) |
GermanD | 4:fa48e78e9bba | 144 | { |
demo71 | 3:9e5e0b8d0e2a | 145 | |
GermanD | 4:fa48e78e9bba | 146 | Mes=Mes+wheel.getPulses(); //con el encoder aumentamos el mes |
GermanD | 4:fa48e78e9bba | 147 | wheel.reset(); |
GermanD | 4:fa48e78e9bba | 148 | |
GermanD | 4:fa48e78e9bba | 149 | if(Mes>=12) |
GermanD | 4:fa48e78e9bba | 150 | { //maximo a 12 meses si es mayor, matiene a 12 |
GermanD | 4:fa48e78e9bba | 151 | Mes=12; |
demo71 | 3:9e5e0b8d0e2a | 152 | } |
GermanD | 4:fa48e78e9bba | 153 | else if (Mes<=0) |
GermanD | 5:d38b972a1813 | 154 | { //se mantiene en 0 |
GermanD | 4:fa48e78e9bba | 155 | Mes=0; |
demo71 | 3:9e5e0b8d0e2a | 156 | } |
GermanD | 4:fa48e78e9bba | 157 | pc.printf("Mes= %d\n ",Mes); //muestra el mes |
demo71 | 3:9e5e0b8d0e2a | 158 | |
demo71 | 3:9e5e0b8d0e2a | 159 | |
GermanD | 4:fa48e78e9bba | 160 | if(Boton.falling()) |
GermanD | 4:fa48e78e9bba | 161 | { |
GermanD | 5:d38b972a1813 | 162 | goto Anio; |
demo71 | 3:9e5e0b8d0e2a | 163 | } |
GermanD | 4:fa48e78e9bba | 164 | |
demo71 | 3:9e5e0b8d0e2a | 165 | |
demo71 | 3:9e5e0b8d0e2a | 166 | } |
GermanD | 5:d38b972a1813 | 167 | |
GermanD | 5:d38b972a1813 | 168 | Anio: |
GermanD | 4:fa48e78e9bba | 169 | while(1) |
GermanD | 4:fa48e78e9bba | 170 | { |
GermanD | 4:fa48e78e9bba | 171 | Anio=Anio+wheel.getPulses(); |
demo71 | 3:9e5e0b8d0e2a | 172 | wheel.reset(); |
GermanD | 4:fa48e78e9bba | 173 | |
demo71 | 3:9e5e0b8d0e2a | 174 | |
GermanD | 5:d38b972a1813 | 175 | if(Anio>=3000) //se mantiene hasta el año 3000 si se ingresa un año mas grande |
GermanD | 4:fa48e78e9bba | 176 | { |
GermanD | 4:fa48e78e9bba | 177 | Anio=3000; |
demo71 | 3:9e5e0b8d0e2a | 178 | } |
GermanD | 4:fa48e78e9bba | 179 | else if (Anio<=2015){ //como estamos en el 2015 una alarma no puede sonar antes de esa fecha |
GermanD | 4:fa48e78e9bba | 180 | Anio=2015; |
demo71 | 3:9e5e0b8d0e2a | 181 | } |
GermanD | 5:d38b972a1813 | 182 | pc.printf("Anio= %d\n",Anio); |
demo71 | 3:9e5e0b8d0e2a | 183 | |
demo71 | 3:9e5e0b8d0e2a | 184 | |
GermanD | 5:d38b972a1813 | 185 | if(Boton.falling()) //presionando el encoder cambiamos a configurar la hora |
GermanD | 4:fa48e78e9bba | 186 | { |
GermanD | 5:d38b972a1813 | 187 | goto Horas; |
leihen | 0:431183c5b136 | 188 | } |
GermanD | 4:fa48e78e9bba | 189 | |
demo71 | 3:9e5e0b8d0e2a | 190 | } |
GermanD | 5:d38b972a1813 | 191 | Horas: |
GermanD | 4:fa48e78e9bba | 192 | while(1) |
GermanD | 4:fa48e78e9bba | 193 | { |
GermanD | 4:fa48e78e9bba | 194 | |
GermanD | 4:fa48e78e9bba | 195 | Horas=Horas+wheel.getPulses(); |
demo71 | 3:9e5e0b8d0e2a | 196 | wheel.reset(); |
GermanD | 4:fa48e78e9bba | 197 | |
demo71 | 3:9e5e0b8d0e2a | 198 | |
GermanD | 4:fa48e78e9bba | 199 | if(Horas>=23) |
GermanD | 4:fa48e78e9bba | 200 | { |
GermanD | 4:fa48e78e9bba | 201 | Horas=23; |
demo71 | 3:9e5e0b8d0e2a | 202 | } |
GermanD | 4:fa48e78e9bba | 203 | else if (Horas<=0) |
GermanD | 4:fa48e78e9bba | 204 | { |
GermanD | 4:fa48e78e9bba | 205 | Horas=0; |
demo71 | 3:9e5e0b8d0e2a | 206 | } |
GermanD | 4:fa48e78e9bba | 207 | pc.printf(" Hora=%d\n ",Horas); |
demo71 | 3:9e5e0b8d0e2a | 208 | |
demo71 | 3:9e5e0b8d0e2a | 209 | |
GermanD | 5:d38b972a1813 | 210 | if(Boton.falling()){ //presionando el encoder cambiamos a configurar los minutos |
GermanD | 5:d38b972a1813 | 211 | goto Minutos; |
demo71 | 3:9e5e0b8d0e2a | 212 | } |
GermanD | 4:fa48e78e9bba | 213 | |
demo71 | 3:9e5e0b8d0e2a | 214 | |
demo71 | 3:9e5e0b8d0e2a | 215 | } |
GermanD | 5:d38b972a1813 | 216 | Minutos: |
GermanD | 4:fa48e78e9bba | 217 | while(1) |
GermanD | 4:fa48e78e9bba | 218 | { |
demo71 | 3:9e5e0b8d0e2a | 219 | |
GermanD | 4:fa48e78e9bba | 220 | Minutos=Minutos+wheel.getPulses(); |
demo71 | 3:9e5e0b8d0e2a | 221 | wheel.reset(); |
GermanD | 4:fa48e78e9bba | 222 | |
demo71 | 3:9e5e0b8d0e2a | 223 | |
GermanD | 4:fa48e78e9bba | 224 | if(Minutos>=59) |
GermanD | 4:fa48e78e9bba | 225 | { |
GermanD | 4:fa48e78e9bba | 226 | Minutos=59; |
demo71 | 3:9e5e0b8d0e2a | 227 | } |
GermanD | 4:fa48e78e9bba | 228 | else if (Horas<=0) |
GermanD | 4:fa48e78e9bba | 229 | { |
GermanD | 4:fa48e78e9bba | 230 | Minutos=0; |
demo71 | 3:9e5e0b8d0e2a | 231 | } |
GermanD | 5:d38b972a1813 | 232 | pc.printf(" Minuto=%d\n ",Minutos); |
demo71 | 3:9e5e0b8d0e2a | 233 | |
demo71 | 3:9e5e0b8d0e2a | 234 | |
GermanD | 4:fa48e78e9bba | 235 | if(Boton.falling()) |
GermanD | 4:fa48e78e9bba | 236 | { |
GermanD | 5:d38b972a1813 | 237 | goto comp; |
demo71 | 3:9e5e0b8d0e2a | 238 | } |
GermanD | 4:fa48e78e9bba | 239 | |
demo71 | 3:9e5e0b8d0e2a | 240 | } |
demo71 | 3:9e5e0b8d0e2a | 241 | |
GermanD | 5:d38b972a1813 | 242 | comp: // para que muestre cuándo quedó configurada la alarma |
demo71 | 3:9e5e0b8d0e2a | 243 | |
GermanD | 4:fa48e78e9bba | 244 | pc.printf("La alarma se programara el: \n"); |
GermanD | 5:d38b972a1813 | 245 | pc.printf(" Dia=%d Mes=%d Anio=%d Hora=%d Minuto=%d Segundo=%d\n ",Dia,Mes,Anio,Horas,Minutos,Segundos); |
GermanD | 5:d38b972a1813 | 246 | |
GermanD | 5:d38b972a1813 | 247 | wait (3); |
demo71 | 3:9e5e0b8d0e2a | 248 | |
GermanD | 4:fa48e78e9bba | 249 | while(1) |
GermanD | 4:fa48e78e9bba | 250 | { |
demo71 | 3:9e5e0b8d0e2a | 251 | rtc.getTime(tm); |
GermanD | 5:d38b972a1813 | 252 | pc.printf("Hora actual: %02d:%02d:%02d\n", tm.hour, tm.min, tm.sec); |
GermanD | 5:d38b972a1813 | 253 | pc.printf("Fecha actual: %02d/%02d/%04d\n", tm.mon, tm.date, tm.year); |
GermanD | 4:fa48e78e9bba | 254 | if(tm.hour>=Horas && tm.min>=Minutos &&tm.sec>=Segundos && tm.mon>=Mes && tm.date>=Dia && tm.year>=Anio){ |
GermanD | 4:fa48e78e9bba | 255 | led1=0; |
GermanD | 5:d38b972a1813 | 256 | // pc.printf("La alarma se ha activado: \n"); |
demo71 | 3:9e5e0b8d0e2a | 257 | } |
demo71 | 3:9e5e0b8d0e2a | 258 | else { |
GermanD | 4:fa48e78e9bba | 259 | led1=1; |
demo71 | 3:9e5e0b8d0e2a | 260 | } |
demo71 | 3:9e5e0b8d0e2a | 261 | wait(1); |
GermanD | 4:fa48e78e9bba | 262 | } |
GermanD | 4:fa48e78e9bba | 263 | } |