//////////////////////////////////////////////////////////////////////////
#include <NewSoftSerial.h>
#include <Wire.h> // Esta es la libreria de comunicacion I2C
#define DS1307_I2C_ADDRESS 0x68
NewSoftSerial LCD(0,8); // serial LCD connected to pin #5
// 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
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 la hora del reloj
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 porque 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(9600);
/*
// la primera vez debemos poner en hora, active esta parte y luego vuelva a quitarla
second = 00;
minute = 37;
hour = 01;
dayOfWeek = 4;
dayOfMonth = 27;
month = 1;
year = 11;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
*/
}
void loop()
{
clearLCD();
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
goTo(0);
LCD.print("hora: ");
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);
goTo (18);
LCD.print("OK");
delay(1000); //Pausa durante 1 segundo
}
// LCD LCD COMMAND CODES
//Apagar LCD
void lcdoff()
{
LCD.print(0x7C, BYTE); //establecer brillo
LCD.print(0, BYTE);//cantidad para dejarla oscura 0
}
// Initiates a function command to the display
void serCommand() {
LCD.print(0xFE, BYTE);
}
// Resets the display, undoing any scroll and removing all text
void clearLCD() {
serCommand();
LCD.print(0x01, BYTE);
}
// Starts the cursor at the beginning of the first line (convienence method for goTo(0))
void selectLineOne() { //puts the cursor at line 0 char 0.
serCommand(); //command flag
LCD.print(128, BYTE); //position
}
// Starts the cursor at the beginning of the second line (convienence method for goTo(16))
void selectLineTwo() { //puts the cursor at line 0 char 0.
serCommand(); //command flag
LCD.print(192, BYTE); //position
}
// Sets the cursor to the given position
// line 1: 0-15, line 2: 16-31, 31+ defaults back to 0
void goTo(int position) {
if (position < 16) {
serCommand(); //command flag
LCD.print((position+128), BYTE);
} else if (position < 32) {
serCommand(); //command flag
LCD.print((position+48+128), BYTE);
} else {
goTo(0);
}
}