#include <delays.h>
#include <i2c.h>
#include <stdlib.h>
unsigned char sec, min, hr, week_day, day, mn, year;
unsigned char last_date;
char tnum[4],txt;
void readDS1307(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year)
{
StartI2C2();
WriteI2C2(0xD0);
WriteI2C2(0x00);
RestartI2C2();
WriteI2C2(0xD1);
*sec = ReadI2C2();
AckI2C2();
*min = ReadI2C2();
AckI2C2();
*hr = ReadI2C2();
AckI2C2();
*week_day = ReadI2C2();
AckI2C2();
*day = ReadI2C2();
AckI2C2();
*mn = ReadI2C2();
AckI2C2();
*year = ReadI2C2();
NotAckI2C2();
StopI2C2();
}
void writeDS1307()
{
StartI2C2();
WriteI2C2(0xD0);
WriteI2C2(0x00);
WriteI2C2(0x00);// sec
WriteI2C2(0x01);//minutes
WriteI2C2(0x01);// hours
WriteI2C2(0x01);//week_day
WriteI2C2(0x15);//date
WriteI2C2(0x12);//month
WriteI2C2(0x13);//year
WriteI2C2(0x10); // REG 7 - Enable squarewave output pin1hz
StopI2C2();
}
// -------------------- Formats date and time
void Transform_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year)
{
*sec = ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
*min = ((*min & 0x70) >> 4)*10 + (*min & 0x0F);
*hr = ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
*week_day =(*week_day & 0x07);
*day = ((*day & 0x30) >> 4)*10 + (*day & 0x0F);
*mn = ((*mn & 0x70) >> 4)*10 + (*mn & 0x0F);
*year =((*year & 0xF0) >> 4)*10 + (*year & 0x0F);
}
void __init(void){
ANSELA = 0;
ANSELB = 0;
ANSELD = 0;
ANSELC = 0;
ANSELE = 0;
ADCON0 = 0;
OpenI2C2(MASTER,SLEW_OFF);
Delay10KTCYx(25);
SSP2ADD=39; //set i2c clock
StartI2C2();
IdleI2C2();
Delay10KTCYx(25);
// writeDS1307();
}
void main(void)
{
__init();
while(1){
readDS1307(&sec, &min, &hr, &week_day, &day, &mn, &year);
Transform_Time(&sec, &min, &hr, &week_day, &day, &mn, &year);
Delay10KTCYx(25);
}
}