2
sep
2012
Librería gDS1307 para reloj Dallas DS1307
Después de probar bastantes de las librerías disponibles para este reloj y de que no me convenciera ninguna por su forma de funcionar, decidí programar mi propia librería basándome en el código que use en mi primer RTC.
Ahora al estar todo en una librería es mucho mas cómodo de usar que antes y tiene los métodos necesarios para poner el reloj en hora u obtener sus datos.
Este es uno de los ejemplos incluidos en la librería:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/* Nombre gDS1307 Descripcion Ejemplo 1 URL http://giltesa.com/?p=11991 Licencia (CC) BY-NC-SA: giltesa.com Nombre del fichero gDS1307_miniTest.pde Fecha ultima modificacion 02/09/2012 Version: 1.0 */ #include <Wire.h> #include <gDS1307.h> gDS1307 rtc; void setup() { Serial.begin(9600); // Permite la configurar de la fecha y la hora. // Descomentar para usar. Despues volver a comentar y grabar de nuevo el codigo en el Arduino, si no cada vez que se reinicie se reconfigurara. // rtc.setFecha( DOMINGO, 2, SEPTIEMBRE, 2012 ); // rtc.setTiempo( 13, 30, 0 ); } void loop() { Tiempo t = rtc.getDatos(); // HORA Serial.print(t.horaPM, DEC); Serial.print(":"); Serial.print(t.minutos, DEC); Serial.print(":"); Serial.print(t.segundos, DEC); Serial.print(" del "); // FECHA Serial.print(t.diaMes, DEC); Serial.print("/"); Serial.print(t.mes, DEC); Serial.print("/"); Serial.println(t.anio, DEC); delay(1000); } |
Y esta la librería en si:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
/* Nombre gDS1307 Descripcion Ejemplo 1 URL http://giltesa.com/?p=11991 Licencia (CC) BY-NC-SA: giltesa.com Nombre del fichero gDS1307.h Fecha ultima modificacion 02/09/2012 Version 1.0 */ #ifndef GDS1307_H_ #define GDS1307_H_ // LIBRERIAS #include "WProgram.h" #include <../Wire/Wire.h> // CONSTANTES #define DS1307_I2C_ADDRESS 0x68 #define LUNES 1 #define MARTES 2 #define MIERCOLES 3 #define JUEVES 4 #define VIERNES 5 #define SABADO 6 #define DOMINGO 7 #define ENERO 1 #define FEBRERO 2 #define MARZO 3 #define ABRIL 4 #define MAYO 5 #define JUNIO 6 #define JULIO 7 #define AGOSTO 8 #define SEPTIEMBRE 9 #define OCTUBRE 10 #define NOVIEMBRE 11 #define DICIEMBRE 12 // LA CLASE TIEMPO class Tiempo { public: uint8_t segundos; // 0~59 uint8_t minutos; // 0~59 uint8_t horaAM; // 0~12 uint8_t horaPM; // 0~23 uint8_t diaSemana; // 1~07 uint8_t diaMes; // 1~31 uint8_t mes; // 1~12 uint16_t anio; // 2000~2099 Tiempo(); }; // LA CLASE gDS1307 class gDS1307 { public: gDS1307(); void setFecha( uint8_t diaSemana, uint8_t diaMes, uint8_t mes, uint16_t anio ); void setTiempo( uint8_t horaPM, uint8_t minutos, uint8_t segundos ); Tiempo getDatos(); private: uint8_t decToBcd(uint8_t val); uint8_t bcdToDec(uint8_t val); }; #endif /* GDS1307_H_ */ |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
/* Nombre gDS1307 Descripcion Ejemplo 1 URL http://giltesa.com/?p=11991 Licencia (CC) BY-NC-SA: giltesa.com Nombre del fichero gDS1307.cpp Fecha ultima modificacion 02/09/2012 Version 1.0 */ #include "WProgram.h" #include <../Wire/Wire.h> #include "gDS1307.h" /* * LA CLASE TIEMPO * Nos proporciona todas las propiedades necesarias para almacenar los parametros del RTC, * bien para ponerlo en hora o para obtener sus datos. * * El constructor pone a "0" todas las propiedades al instanciar un objeto. * */ Tiempo :: Tiempo() { segundos = 0; minutos = 0; horaAM = 0; horaPM = 0; diaSemana = 1; diaMes = 1; mes = 1; anio = 2012; } /* LA CLASE gDS1307 * Se encarga de poner en hora o de obtener los datos del reloj. * * El constructor inicia la comunicacion con el bus i2c * */ gDS1307 :: gDS1307() { Wire.begin(); } /* * LA CLASE gDS1307 - METODO setFecha * Permite ajustar la fecha del DS1307, necesita para ello 4 parametros: dia de la semana, dia del mes, mes y el año. * El dia de la semana y el mes pueden ser representados tanto en numero como por escrito en mayusculas y sin acentos. * Si alguno de esos parametros estuviera en un rango incorrecto prevalecera el que tenga configurado el DS1307 en ese momento. * */ void gDS1307 :: setFecha( uint8_t diaSemana, uint8_t diaMes, uint8_t mes, uint16_t anio ) { Tiempo t = this->getDatos(); Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.send(0); Wire.send(decToBcd(t.segundos)); Wire.send(decToBcd(t.minutos)); Wire.send(decToBcd(t.horaPM)); if(diaSemana >= 1 && diaSemana <= 7) Wire.send(decToBcd(diaSemana)); else Wire.send(decToBcd(t.diaSemana)); if(diaMes >= 1 && diaMes <= 31) Wire.send(decToBcd(diaMes)); else Wire.send(decToBcd(t.diaMes)); if(mes >= 1 && mes <= 12) Wire.send(decToBcd(mes)); else Wire.send(decToBcd(t.mes)); if(anio >= 0 && anio <= 99) Wire.send(decToBcd(anio)); else if( anio >= 2000 && anio <= 2099) Wire.send(decToBcd(anio-2000)); else Wire.send(decToBcd(t.anio)); Wire.endTransmission(); } /* * LA CLASE gDS1307 - METODO setTiempo * Permite ajustar la fecha del DS1307, necesita para ello 3 parametros: La hora en formato PM/24h, los minutos y los segundos. * Si alguno de esos parametros estuviera en un rango incorrecto prevalecera el que tenga configurado el DS1307 en ese momento. * */ void gDS1307 :: setTiempo( uint8_t horaPM, uint8_t minutos, uint8_t segundos ) { Tiempo t = this->getDatos(); Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.send(0); if(segundos >= 0 && segundos <= 59) Wire.send(decToBcd(segundos)); else Wire.send(decToBcd(t.segundos)); if(minutos >= 0 && minutos <= 59) Wire.send(decToBcd(minutos)); else Wire.send(decToBcd(t.minutos)); if(horaPM >= 0 && horaPM <=23) Wire.send(decToBcd(horaPM)); else Wire.send(decToBcd(t.horaPM)); Wire.send(decToBcd(t.diaSemana)); Wire.send(decToBcd(t.diaMes)); Wire.send(decToBcd(t.mes)); Wire.send(decToBcd(t.anio-2000)); Wire.endTransmission(); } /* * LA CLASE gDS1307 - METODO getDatos * Permite obtener todos los datos del DS1307, estos datos son devueltos por valor. * */ Tiempo gDS1307 :: getDatos() { Tiempo t; Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.send(0); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 7); t.segundos = bcdToDec(Wire.receive() & 0x7f); t.minutos = bcdToDec(Wire.receive()); t.horaPM = bcdToDec(Wire.receive() & 0x3f); if(t.horaPM > 12) t.horaAM = t.horaPM - 12; else t.horaAM = t.horaPM; t.diaSemana = bcdToDec(Wire.receive()); t.diaMes = bcdToDec(Wire.receive()); t.mes = bcdToDec(Wire.receive()); t.anio = bcdToDec(Wire.receive()) + 2000; return t; } // De Decimal a BCD uint8_t gDS1307 :: decToBcd(uint8_t val) { return( (val/10*16) + (val%10) ); } // De BCD a Decimal uint8_t gDS1307 :: bcdToDec(uint8_t val) { return( (val/16*10) + (val%16) ); } |
Descargar la librería (Para Arduino IDE 0022)