#include <18f4550.h>
#use delay(clock=1000000)
#use i2c(MASTER, SDA=PIN_b0, SCL=PIN_b1,slow)
//Registros DS1307
#define ds_seconds 0x00
#define ds_minutes 0x01
#define ds_hours 0x02
#define ds_day 0x03
#define ds_date 0x04
#define ds_month 0x05
#define ds_year 0x06
#define ds_control 0x07
//DS1307
//Vbat a tierra
#fuses XT,NOWDT,NOPBADEN //Fusibles de configuracion, OSC XT, y Watchdog Desactivado
#BYTE PORTD=0xF83
#BYTE PORTB=0xF81
#include "lcd.c"
#include "key.c"
#int_EXT2
void EXT2_isr(void){
teclado();
}
void main(void)
{
set_tris_b(0b11111111);
set_tris_d(0b00001111);
enable_interrupts(INT_EXT2); // Habilita la interrupción en RB0
enable_interrupts(GLOBAL); // Habilitación general de interrupción
ext_int_edge(L_TO_H); // Config. int. por flanco descendente. (L_TO_H) para ascendente
int16 retardo; //Cada cuando actualiza lectura en LCD
//Dato en registro ds, a mandar o leer
int seconds,minutes,hours,day,date,month,year,control;
boolean cambio_hora = true;
retardo=500; //ms a tardar entre lectura y lectura
//Justo cuando prende circuito, la fecha que establece es la siguiente
seconds= 0x40;
minutes= 0x59;
hours = 0x01; //modo 24 hrs
day = 0x01; //1 es domingo
date = 0x27;
month = 0x10;
year = 0x13;
control= 0x00; //OUT=0 SQWE=0 RS0-1=0 no hay onda cuadrada
//Establece fecha y hora
i2c_start(); //Inicia I2C
i2c_write(0xd0); //Direcciona DS1307 write
i2c_write(ds_seconds); //Indica que empiece en registro ds_seconds
//Aquí establece fecha y hora respecto a la puesta arriba
i2c_write(seconds);
i2c_write(minutes);
i2c_write(hours);
i2c_write(day);
i2c_write(date);
i2c_write(month);
i2c_write(year);
i2c_write(control);
i2c_stop(); //Ya puso la hora y fecha
//El RTC ya está marcando el tiempo y fecha
//Rutina LCD
lcd_init(); //Inicia LCD
lcd_putc("\f"); //Limpia LCD
lcd_putc("Real Time Clock\n"); //rtc
lcd_putc(" DS1307");
delay_ms(3000);
while(1)
{
delay_ms(retardo); //Usuario lee
//Obtiene datos de DS1307
i2c_start(); //Inicia I2C
i2c_write(0xd0); //Direcciona DS1307 write
i2c_write(ds_seconds); //Indica que ponga registro en ds_seconds
i2c_stop();
i2c_start();
i2c_write(0xd1); //Lee DS1307
seconds = i2c_read();
minutes = i2c_read();
hours = i2c_read();
day = i2c_read();
date = i2c_read();
month = i2c_read();
year = i2c_read(0); //no ack
i2c_stop();
if (day == 0x01 && month != 0x03 && month != 0x10 ){
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Domingo; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
else if (day == 0x01 && month == 0x03 && date < 0x25){
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Domingo; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
else if (day == 0x01 && month == 0x03 && date >= 0x25 && hours != 0x02){
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Domingo; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
else if (day == 0x01 && month == 0x03 && date >= 0x25 && hours == 0x02){
hours = 0x03;
//Representa datos en la LCD
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Domingo; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
else if (day == 0x01 && month == 0x10 && date < 0x25){
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Domingo; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
else if (day == 0x01 && month == 0x10 && date >= 0x25 && hours != 0x02){
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Domingo; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
else if (day == 0x01 && month == 0x10 && date >= 0x25 && hours == 0x02){
if (cambio_hora){
hours = 0x01;
}
cambio_hora=!cambio_hora;
//Representa datos en la LCD
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Domingo; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
else if (day == 0x02){
//Representa datos en la LCD
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Lunes; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
else if (day == 0x03){
//Representa datos en la LCD
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Martes; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
else if (day == 0x04){
//Representa datos en la LCD
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Miércoles; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
else if (day == 0x05){
//Representa datos en la LCD
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Jueves; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
else if (day == 0x06){
//Representa datos en la LCD
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Viernes; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
else if (day == 0x07){
//Representa datos en la LCD
printf(lcd_putc,"\f%2x-%2x-20%2x\n",date,month,year); //dd-mm-year
printf(lcd_putc,"Sábado; %2x:%2x:%2x",hours,minutes,seconds); //23:59:59
}
}
}