Arduino library to check the date and temperature of the integrated DS3231 via I2C
UPDATED: Also visit the new library to manage date and time with the DS3231 RTC module and Arduino with improvements such as seasonal time.
The operation of IC de real time clocks (RTC) most popular that are controlled using the bus I2C It is usually very similar. Besides, the Wire library de Arduino greatly simplifies communications with devices Two-wire Serial Interface (TWI), I2C, specific.
Broadly speaking, the process consists of
-
Initiate communications as slave or master (set by default) with the function Wire.begin(address). If "address" is omitted, communications begin with the μC the teacher of bus I2C.
-
Activate communication I2C with the device through the memory address where it is located, using the command Wire.beginTransmission(address).
-
Write an order in the bus I2C to tell the device the operation you want it to perform, using Wire.write(command), in which "order" is the operation code.
-
Disable communications to release the bus I2C with function Wire.endTransmission().
-
Request the device to send a certain amount of data that corresponds to the operation that has been requested (in this case, the date and time) with the function Wire.requestFrom(address,amount).
-
Wait for the data requested with the function to be available for reading Wire.available(), which returns the number of data that has already been received and can be read.
-
Read the data sent by the device (the real time clock, in this case) using the function Wire.read() as many times as bytes indicated Wire.available() that are available.
-
Normally the data is sent in very compact formats so it is very likely that it will be necessary to interpret the data received in a way that corresponds to the representation of the data made in the program that uses the device.
Regarding the DS3231 (and compatible ones in the same series, such as the DS3232) and the interpretation of the data, according to the specifications of the integrated, for example, the values of the different digits that represent the time are represented in binary coded decimal (BCD) which will be more convenient to express as a decimal value (a byte) to use in Arduino
On the same line, the temperature is expressed as a byte in two's complement for the integer part and two bits for the step, with a resolution of a quarter of a degree, of the decimal part. These and other aspects of data representation on the clock have been discussed exhaustively in the library code below. DS3231
To check the temperature with this library, just use the method read_temperature() Object DS3231 instantiated at the beginning. To read the date and time, they are first loaded and then requested in one of the formats (compact, human...) available for different uses, documented in the header document of the code library below.
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
|
//DS3231.h
#if defined(ARDUINO) && ARDUINO>=100
#include “Arduino.h”
#else
#include “WProgram.h”
#endif
#include <Wire.h>
#define TEMPERATURA_MAXIMA_DS3231 85.0 // Máxima temperatura que se puede medir con un DS3231 (70 grados en la versión comercial / no-industrial)
#define TEMPERATURA_MINIMA_DS3231 -40.0 // Mínima temperatura que se puede medir con un DS3231 (0 grados en la versión comercial / no-industrial)
#define DIRECCION_DS3231 B1101000 // Según datasheet
#define TIMEOUT_I2C_DS3231 200 // Máximo tiempo de espera del bus I2C del DS3231
#define NUMERO_ELEMENTOS_FECHA 7 // Número de elementos (un byte por elemento) que tiene la matriz con los datos de la fecha
#define NUMERO_BYTES_TEMPERATURA 2 // Número de bytes con los que se representa la temperatura (uno para la parte entera y el signo y otro para la parte decimal representada con una resolución de 0.25 grados)
#define RESOLUCION_DECIMALES_DS3231 0.25 // Grados de cada paso de la parte decimal
#define ROTACION_DECIMALES 6 // Rotación necesaria hasta llegar a los bits que contienen la parte que representa los decimales de la temperatura (rotar 6 corresponde a atender a los bits 7 y 8)
#define MASCARA_DECIMALES B11000000 // Máscara para eliminar con una operación and la parte no significativa. En el caso el DS3231 no hace nada ya que al rotar queda sólo la parte relevante.
class DS3231
{
private:
char valor_fecha_hora_DS3231[7]; // Matriz de valores numéricos (7 char) de la fecha y la hora. El índice 0 representa los segundos, el 1 los minutos, el 2 las horas (en formato de 24), el 3 el día de la semana empezando en el domingo que es 1, el 4 el día del mes, el 5 el número del mes y el 6 los dos últimos dígitos del año
char hora_humana_DS3231[11]; // Hora en el formato hh:mm:ss siendo hh la hora (en formato de 24) representada con 2 dígitos, mm los minutos con 2 dígitos y ss los segundos con 2 dígitos
char fecha_humana_DS3231[11]; // Fecha en formato DD/MM/AAAA siendo DD el día representado con 2 dígitos, MM el mes con 2 dígitos y AAAA el año con 4 dígitos
char fecha_hora_MySQL_DS3231[20]; // Fecha en formato AAAA-MM-DD hh:mm:ss (estilo MySQL) siendo AAAA el año representado con 4 dígitos, MM el mes con 2 dígitos, DD el día con 2 dígitos, hh la hora (en formato de 24) con 2 dígitos, mm los minutos con 2 dígitos y ss los segundos con 2 dígitos
char fecha_hora_compacta_DS3231[13]; // Fecha en formato compacto (como la anterior pero sin adornos y dos dígitos para el año) para escribir en log y en bases de datos
char bcd_a_decimal(char bcd); // Convertir de BCD a decimal
char decimal_a_bcd(char decimal); // Convertir de decimal a BCD
protected:
public:
DS3231();
~DS3231();
void cargar_fecha_hora();
void grabar_fecha_hora(char *fecha);
char *valor_fecha_hora();
char *hora_humana(); // Hora en el formato hh:mm:ss siendo hh la hora (en formato de 24) representada con 2 dígitos, mm los minutos con 2 dígitos y ss los segundos con 2 dígitos
unsigned int reloj_4_digitos_7_segmentos(); // La hora tal como la esperan la mayoría de relojes de cuatro dígitos LED de 7 segmentos
char *fecha_humana(); // Fecha en formato DD/MM/AAAA siendo DD el día representado con 2 dígitos, MM el mes con 2 dígitos y AAAA el año con 4 dígitos
char numero_dia_semana(); // Eso y empezando en domingo que es el 1
char *fecha_hora_MySQL(); // Fecha en formato AAAA-MM-DD hh:mm:ss (estilo MySQL) siendo AAAA el año representado con 4 dígitos, MM el mes con 2 dígitos, DD el día con 2 dígitos, hh la hora (en formato de 24) con 2 dígitos, mm los minutos con 2 dígitos y ss los segundos con 2 dígitos
char *fecha_hora_compacta(); // Fecha en formato compacto (como la anterior pero sin adornos y dos dígitos para el año) para escribir en log y en bases de datos
double leer_temperatura();
double temperatura_minima();
double temperatura_maxima();
};
|
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
|
//DS3231.cpp
#include “DS3231.h”
DS3231::DS3231()
{
//Wire.begin(); // Dependiendo de la versión del IDE puede ser maestro por defecto o no y habrá que activar en la librería o en la aplicación que la use (en el ejemplo se activa desde la aplicación, lo que permite un uso más genérico)
}
DS3231::~DS3231()
{
}
void DS3231::cargar_fecha_hora()
{
unsigned long timeout_i2c;
byte contador;
Wire.beginTransmission(DIRECCION_DS3231); // Comunicar con el DS3231 en la dirección correspondiente
Wire.write(0x00); // pedir registros desde la primera dirección
Wire.endTransmission(); // Liberar el bus I2C
Wire.requestFrom(DIRECCION_DS3231,NUMERO_ELEMENTOS_FECHA); // esperar NUMERO_ELEMENTOS_FECHA bytes
timeout_i2c=millis()+TIMEOUT_I2C_DS3231;
while(Wire.available()<NUMERO_ELEMENTOS_FECHA&&millis()<timeout_i2c){} // Esperar a que lleguen los datos o a que pase el tiempo mínimo de espera // Para usar sin espera: if(Wire.available())
for(contador=0;contador<NUMERO_ELEMENTOS_FECHA;contador++)
{
valor_fecha_hora_DS3231[contador]= Wire.read(); // Leer todos los datos sin discriminar aunque luego tendrán distinto tratamiento
}
valor_fecha_hora_DS3231[0]=bcd_a_decimal(valor_fecha_hora_DS3231[0]); // segundos en BCD
valor_fecha_hora_DS3231[1]=bcd_a_decimal(valor_fecha_hora_DS3231[1]); // minutos en BCD
valor_fecha_hora_DS3231[2]=((valor_fecha_hora_DS3231[2]&B00110000)>>4)*10+(valor_fecha_hora_DS3231[2]&B00001111); // BCD en modo de 24 horas
valor_fecha_hora_DS3231[3]=valor_fecha_hora_DS3231[3]&B00000111; // Número de día de la semana empezando en 1 que es domingo
valor_fecha_hora_DS3231[4]=((valor_fecha_hora_DS3231[4]&B00110000)>>4)*10+(valor_fecha_hora_DS3231[4]&B00001111); // Número del día del mes
valor_fecha_hora_DS3231[5]=((valor_fecha_hora_DS3231[5]&B00010000)>>4)*10+(valor_fecha_hora_DS3231[5]&B00001111); // Número de mes (sin MSB)
valor_fecha_hora_DS3231[6]=bcd_a_decimal(valor_fecha_hora_DS3231[6]); // Año en BCD (dos últimos dígitos)
}
char *DS3231::valor_fecha_hora()
{
//cargar_fecha_hora();
return valor_fecha_hora_DS3231;
}
char *DS3231::hora_humana()
{
//cargar_fecha_hora();
sprintf
(
hora_humana_DS3231,
“%02d:%02d:%02d”,
valor_fecha_hora_DS3231[2],
valor_fecha_hora_DS3231[1],
valor_fecha_hora_DS3231[0]
);
return hora_humana_DS3231;
}
unsigned int DS3231::reloj_4_digitos_7_segmentos()
{
//cargar_fecha_hora();
return (int)valor_fecha_hora_DS3231[2]*100+(int)valor_fecha_hora_DS3231[1];
}
char *DS3231::fecha_humana()
{
//cargar_fecha_hora();
sprintf
(
fecha_humana_DS3231,
“%02d/%02d/20%02d”, // Formato de fecha y hora estilo español ¡Olé!
valor_fecha_hora_DS3231[4],
valor_fecha_hora_DS3231[5],
valor_fecha_hora_DS3231[6]
);
return fecha_humana_DS3231;
}
char DS3231::numero_dia_semana()
{
//cargar_fecha_hora();
return valor_fecha_hora_DS3231[3];
}
char *DS3231::fecha_hora_compacta()
{
//cargar_fecha_hora();
sprintf
(
fecha_hora_compacta_DS3231,
“%02d%02d%02d%02d%02d%02d”, // Formato de fecha y hora compacta para log y base de datos
valor_fecha_hora_DS3231[6],
valor_fecha_hora_DS3231[5],
valor_fecha_hora_DS3231[4],
valor_fecha_hora_DS3231[2],
valor_fecha_hora_DS3231[1],
valor_fecha_hora_DS3231[0]
);
return fecha_hora_compacta_DS3231;
}
char *DS3231::fecha_hora_MySQL()
{
//cargar_fecha_hora();
sprintf
(
fecha_hora_MySQL_DS3231,
“20%02d-%02d-%02d %02d:%02d:%02d”, // Formato de fecha y hora estilo MySQL
valor_fecha_hora_DS3231[6],
valor_fecha_hora_DS3231[5],
valor_fecha_hora_DS3231[4],
valor_fecha_hora_DS3231[2],
valor_fecha_hora_DS3231[1],
valor_fecha_hora_DS3231[0]
);
return fecha_hora_MySQL_DS3231;
}
void DS3231::grabar_fecha_hora(char *fecha)
{
byte contador;
Wire.beginTransmission(DIRECCION_DS3231); // Comunicar con el DS3231 en la dirección correspondiente
Wire.write(0x00); // Empezar el envío en la primera dirección
for(contador=0;contador<NUMERO_ELEMENTOS_FECHA;contador++)
{
Wire.write(decimal_a_bcd(fecha[contador])); // Escribir cada valor expresándolo en BCD
}
Wire.endTransmission(); // Liberar el bus I2C
}
double DS3231::leer_temperatura()
{
byte msb; // El byte más significativo contiene la parte entera de la temperatura (en complemento a 2 para poder representar temperaturas bajo cero)
byte lsb; // El byte menos significatico contiene la parte decimal con una resolución de un cuarto de grado
float temperatura=TEMPERATURA_MAXIMA_DS3231+1.0; // Un número mayor que el máximo como aviso de que algo va mal
boolean negativo=false; // Inicialmente se considera postivo
unsigned long timeout_i2c;
Wire.beginTransmission(DIRECCION_DS3231); // Preparar el dispositivo
Wire.write(0x11); // Solicitar temperatura (empieza en 11h y termina en 12h)
Wire.endTransmission();
Wire.requestFrom(DIRECCION_DS3231,NUMERO_BYTES_TEMPERATURA); // Esperar temperatura: pedir dos bytes en la dirección del integrado
timeout_i2c=millis()+TIMEOUT_I2C_DS3231;
while(Wire.available()<NUMERO_BYTES_TEMPERATURA&&millis()<timeout_i2c){}// Esperar a que lleguen los datos o pase el tiempo de espera máximo // Para usar sin espera: if(Wire.available())
msb=Wire.read(); // parte entera con signo en complemento a dos
lsb=Wire.read(); // parte fraccional con resolución de 0.25 grados
negativo=msb>B01111111; // Es negativo si el primer dígito es uno
temperatura=msb&B01111111; // revertir complemento a dos
temperatura+=((lsb&MASCARA_DECIMALES)>>ROTACION_DECIMALES)*RESOLUCION_DECIMALES_DS3231; // atender sólo a los bits que contienen la parte decimal (7 y 8), multiplicar por el paso de la resolución y sumar a la parte entera de la temperatura
if(negativo)
{
temperatura*=–1; // Cambiar el signo
}
return temperatura;
}
double DS3231::temperatura_minima()
{
return TEMPERATURA_MINIMA_DS3231;
}
double DS3231::temperatura_maxima()
{
return TEMPERATURA_MAXIMA_DS3231;
}
char DS3231::bcd_a_decimal(char bcd) // Convertir de BCD a decimal
{
return ((bcd&B11110000)>>4)*10+(bcd&B00001111);
}
char DS3231::decimal_a_bcd(char decimal) // Convertir de decimal a BCD
{
return decimal/10*16+(decimal%10);
}
|
The following is example code to show how to use the library. As mentioned above, the temperature is simply read with the function
of the class object but, to ignore errors in the reading, two constants are used that store, respectively, the maximum and minimum temperature of the device according to the data sheet and which are read with the corresponding functions.The temperature reading is carried out in two stages: first the value is loaded, so that different uses of the date or time will be consistent (they will not show a higher value in unfavorable cases) and secondly it is used according to the format that is needed. The example program (which is not very practical, although it explains all the possibilities) shows all the available formats
-
The function tank (bytes) containing the seven numerical values that represent the date and time on a clock DS3231 converted to decimal (they are in BCD on the device)
which returns a pointer to an array -
Using the function
A value is obtained that corresponds to the number of the day of the week starting on Sunday. To display it as text, an array is used and one is subtracted to start at index zero, Sunday. -
To consult the date in a "local" (Spanish) format, use the function
, which returns a pointer to a string in which the date is represented in DD/MM/YYYY format, where DD is the day represented with 2 digits, MM is the month with 2 digits and YYYY is the year with 4 digits. -
The function
returns the time in the format hh:mm:ss, with hh being the hour (in 24 format) represented with 2 digits, mm being the minutes with 2 digits and ss being the seconds with 2 digits. -
To easily use the date and time on log files the function has been programmed , which delivers the value of the date and time in the format YYMMDDhhmmss with AA being the year represented with the last 2 digits, MM the month with 2 digits, DD the day with 2 digits, hh the hour (in 24 format) with 2 digits, mm the minutes with 2 digits and ss the seconds with 2 digits. This format, even though it is text, takes up little space and allows for very simple alphabetical ordering.
-
The function MySQL (or the new and freer MariaDB) YYYY-MM-DD hh:mm:ss, where YYYY is the year represented with 4 digits, MM is the month with 2 digits, DD is the day with 2 digits, hh is the hour (in 24 format) with 2 digits, mm is the minutes with 2 digits and seconds with 2 digits.
serves to present the date and time in the format used by the database manager
Although there are many formats with which to represent the date and time, the one you need may not be there, but surely based on one of the existing ones and using it as an example, it will be easy to add a new method according to other specifications. Please, if you add new functions, share the code (release it!) and explain to us how it works, so we can make the library better little by little. Thank you!
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
|
#define INTERVALO_MEDICION 100000 // Medir temperatura cada 100 segundos (se renueva internamente en el DS3231 cada 64 segundos)
#define ESPERA_ERROR 1000 // Tiempo de espera antes de volver a medir si se ha producido un error
#define ELEMENTOS_MATRIZ_FECHA 7
#include “DS3231.h”
#include <Wire.h>
char buffer_fecha[ELEMENTOS_MATRIZ_FECHA];
char *puntero_fecha;
float temperatura;
unsigned long cronometro;
byte contador;
String dia_semana[]={“lunes”,“martes”,“miércoles”,“jueves”,“viernes”,“sábado”,“domingo”};
DS3231 reloj;
void setup()
{
Serial.begin(9600);
Wire.begin(); // Inicializar Wire sólo si no se hace dentro del constructor (de la librería) Este método, hacerlo en la aplicación, supone que se usa Wire para comunicar con otros dispositivos, no sólo con el DS3231
cronometro=0; // para que empiece inmediatamente
}
void loop()
{
if(millis()>cronometro)
{
temperatura=reloj.leer_temperatura();
if(temperatura>reloj.temperatura_maxima()||temperatura<reloj.temperatura_minima())
{
cronometro=millis()+ESPERA_ERROR;
}
else
{
cronometro=millis()+INTERVALO_MEDICION;
reloj.cargar_fecha_hora();
puntero_fecha=reloj.valor_fecha_hora();
for(contador=0;contador<ELEMENTOS_MATRIZ_FECHA;contador++)
{
buffer_fecha[contador]=*(puntero_fecha+contador);
Serial.println(“Contenido de la posición “+String(contador,DEC)+” del buffer de la fecha -> “+String(int(buffer_fecha[contador]),DEC));
}
Serial.print(“El día “);
Serial.print(reloj.fecha_humana());
Serial.print(“, “);
Serial.print(dia_semana[reloj.numero_dia_semana()–1]);
Serial.print(“, a las “);
Serial.println(reloj.hora_humana());
Serial.print(“(“);
Serial.print(reloj.reloj_4_digitos_7_segmentos());
Serial.print(” en un reloj de 4 dígitos y “);
Serial.print(reloj.fecha_hora_MySQL());
Serial.print(” según MySQL o “);
Serial.print(reloj.fecha_hora_compacta());
Serial.println(” abreviadamente)”);
Serial.print(“la temperatura era de “);
Serial.print(temperatura); // Mostrar la temperatura
Serial.println(” grados centígrados”);
}
}
}
|
The output of the above example program could be something like what is shown in the following image: a list of 7 values (seconds, minutes, hour, day of the week, day of the month, month and year) the date and the time expressed in a "human" way (according to the Spanish style) the time as a whole number in the four-digit clock format, the date and time in database format MySQL, date and time in compact format (for logs) and the internal temperature of the DS3231.
Post Comment