// include the library code:
#include <LiquidCrystal.h>
#include <Wire.h> //
#define DS1307_I2C_ADDRESS 0x68
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Convierte números normales decimales a BCD (binario decimal codificado)
byte decToBcd(byte val)
{
return ((val/10*16) + (val%10));
}
// Convierte BCD (binario decimal codificado) a números normales decimales
byte bcdToDec(byte val)
{
return ((val/16*10) + (val%16));
}
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(decToBcd(second));
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour));
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.endTransmission();
}
// Establece la fecha y el tiempo del ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Resetea el registro puntero
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// Alguno de estos necesitan enmascarar, ciertos bits son bits de control
*second = bcdToDec(Wire.receive()&0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive()&0x3f);
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
}
void setup()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
lcd.begin(20, 4);
///*
second = 00;
minute = 10;
hour = 22;
dayOfWeek = 7;
dayOfMonth = 15;
month = 5;
year = 11;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
//*/
}
void loop()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
lcd.setCursor (0,0);
if (dayOfMonth < 10) lcd.print("0");
lcd.print(dayOfMonth, DEC);
lcd.print("/");
if (month < 10) lcd.print("0");
lcd.print(month, DEC);
lcd.print("/");
if (year < 10) lcd.print("0");
lcd.print(year, DEC);
lcd.print(" ");
if (hour < 10) lcd.print("0");
lcd.print(hour, DEC);
lcd.print(":");
if (minute < 10) lcd.print("0");
lcd.print(minute, DEC);
lcd.print(":");
if (second < 10) lcd.print("0");
lcd.print(second, DEC);
lcd.setCursor(5,2);
switch (dayOfWeek)
{
case 1: lcd.println(" Lunes"); break;
case 2: lcd.println(" Martes"); break;
case 3: lcd.println(" Miercoles"); break;
case 4: lcd.println(" Jueves"); break;
case 5: lcd.println(" Viernes"); break;
case 6: lcd.println(" Sabado"); break;
case 7: lcd.println(" Domingo"); break;
}
delay(1000);
}