The DS1307 can be run in either 12-hour or 24-hour mode. Bit 6 of the hours register is defined as the 12-hour or 24-hour mode-select bit. When high, the 12-hour mode is selected. In the 12-hour mode, bit 5 is the AM/PM bit with logic high being PM. In the 24-hour mode, bit 5 is the second 10-hour bit (20 to 23 hours). The hours value must be re-entered whenever the 12/24-hour mode bit is changed.
When reading or writing the time and date registers, secondary (user) buffers are used to prevent errors when the internal registers update. When reading the time and date registers, the user buffers are synchronized to the internal registers on any I2C START. The time information is read from these secondary registers while the clock continues to run. This eliminates the need to re-read the registers in case the internal registers update during a read. The divider chain is reset whenever the seconds register is written. Write transfers occur on the I2C acknowledge from the DS1307. Once the divider chain is reset, to avoid rollover issues, the remaining time and date registers must be written within one second.
#include<reg51.h>
sbit set=P1^2;
sbit mov=P1^3;
sbit inc=P1^4;
sbit dec=P3^7;
sbit ent=P3^6;
void all_disp();
void time_set();
#define First_Line 0x80
#define Second_Line 0xc0
#define Curser_On 0x0f
#define Curser_Off 0x0c
#define Clear_Display 0x01
#define Data_Port P0
sbit Lcd_rs = P2^0;
sbit Lcd_rw = P2^1;
sbit Lcd_en = P2^2;
sbit sda_rtc =P2^5;
sbit scl_rtc =P2^4;
/*sbit Lcd_rs = P3^5;
sbit Lcd_rw = P3^6;
sbit Lcd_en = P3^7; */
void Lcd8_Init();
void Lcd8_Command(unsigned char);
void Lcd8_Write(unsigned char,unsigned char);
void Lcd8_Display(unsigned char,const unsigned char*,unsigned int);
void Lcd8_Decimal2(unsigned char,unsigned char);
void Lcd8_Decimal3(unsigned char,unsigned char);
void Lcd8_Decimal4(unsigned char,unsigned int);
void Delay(unsigned int);
void del();
#define DS1307_ID 0xD0
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02
#define DATE 0x04
#define MONTH 0x05
#define YEAR 0x06
DS1307_get(unsigned char);
void DS1307_settime(unsigned char, unsigned char, unsigned char);
void DS1307_setdate(unsigned char, unsigned char, unsigned char);
Send2lcd(unsigned char);
void Rtc_Write(unsigned char,unsigned char);
Rtc_Read(unsigned char);
void Rtc_rd_wr_sub();
void Rtc_Init();
void Rtc_Start();
void Rtc_Tx();
void Rtc_Rx();
void Rtc_Stop();
void Rtc_Ack();
unsigned int Rtc_add_wr,Rtc_add_rd;
unsigned char d_rtc,datain_rtc,in_rtc,temp_rtc,dat_rtc,flag_rtc;
unsigned char i,j,a[10],cur=0;
unsigned char sec,hour,min,date,month,year,sec1,hour1,min1,sec2, hour2,min2,hh,mm,ss,dd,mn,yy;
unsigned char count[6]={0x86,0x89,0x8c,0xc6,0xc9,0xcc};
unsigned char dec_hex(unsigned char tt)
{
if(tt>59)
tt+=36;
else if(tt>49)
tt+=30;
else if(tt>39)
tt+=24;
else if(tt>29)
tt+=18;
else if(tt>19)
tt+=12;
else if(tt>9)
tt+=6;
return(tt);
}
void all_disp()
{
sec = DS1307_get(SEC);
min = DS1307_get(MIN);
hour = DS1307_get(HOUR);
date = DS1307_get(DATE);
month = DS1307_get(MONTH);
year = DS1307_get(YEAR);
sec=Send2lcd(sec);
min=Send2lcd(min);
hour=Send2lcd(hour);
date=Send2lcd(date);
month=Send2lcd(month);
year=Send2lcd(year);
Lcd8_Decimal2(0x86,date);
Lcd8_Write(0x88,'-');
Lcd8_Decimal2(0x89,month);
Lcd8_Write(0x8b,'-');
Lcd8_Decimal2(0x8c,year);
Lcd8_Decimal2(0xc6,hour);
Lcd8_Write(0xc8,'.');
Lcd8_Decimal2(0xc9,min);
Lcd8_Write(0xcb,'.');
Lcd8_Decimal2(0xcc,sec);
}
void main()
{
Lcd8_Init();
Lcd8_Display(0x80,"RTC TESTING: ",16);
Lcd8_Display(0xc0,"ZAKARIYA: ",16);
Delay(65000);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(First_Line,"Date:",5);
Lcd8_Display(Second_Line,"Time:",5);
Rtc_Init();
//DS1307_setdate(0x04,0x11,0x11);
//DS1307_settime(0x16,0x00,0x00);
while(1)
{
all_disp();
if(!set)
time_set();
}
}
void time_set()
{
Lcd8_Display(0x80," TIME SET ",16);
Lcd8_Display(0xc0," MODE ",16);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(First_Line,"Date:",5);
Lcd8_Display(Second_Line,"Time:",5);
all_disp();
cur=0;
while(ent)
{
Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);
if(mov==0)
{
while(!mov);
cur++;
if(cur==6)
cur=0;
}
else if(count[cur]==0x86)
{
if(inc==0)
{
while(!inc);
date++;
if(date>=32)
date=1;
Lcd8_Decimal2(count[cur],date);
}
else if(dec==0)
{
while(!dec);
date--;
if(date==0xff)
date=31;
Lcd8_Decimal2(count[cur],date);
}
}
else if(count[cur]==0x89)
{
if(inc==0)
{
while(!inc);
month++;
if(month>=13)
month=1;
Lcd8_Decimal2(count[cur],month);
}
else if(dec==0)
{
while(!dec);
month--;
if(month==0xff)
month=12;
Lcd8_Decimal2(count[cur],month);
}
}
else if(count[cur]==0x8c)
{
if(inc==0)
{
while(!inc);
year++;
if(year>99)
year=0;
Lcd8_Decimal2(count[cur],year);
}
else if(dec==0)
{
while(!dec);
year--;
if(year==0xff)
year=99;
Lcd8_Decimal2(count[cur],year);
}
}
else if(count[cur]==0xc6)
{
if(inc==0)
{
while(inc==0);
hour++;
if(hour>=24)
hour=0;
Lcd8_Decimal2(count[cur],hour);
}
else if(dec==0)
{
while(dec==0);
hour--;
if(hour==0)
hour=23;
Lcd8_Decimal2(count[cur],hour);
}
}
else if(count[cur]==0xc9)
{
if(inc==0)
{
while(inc==0);
min++;
if(min>=60)
min=0;
Lcd8_Decimal2(count[cur],min);
}
else if(dec==0)
{
while(dec==0);
min--;
if(min==0)
min=59;
Lcd8_Decimal2(count[cur],min);
}
}
else if(count[cur]==0xcc)
{
if(inc==0)
{
while(inc==0);
Lcd8_Command(0x0c);
sec++;
if(sec>=60)
sec=0;
Lcd8_Decimal2(count[cur],sec);
}
else if(dec==0)
{
while(dec==0);
Lcd8_Command(0x0c);
sec--;
if(sec==0xff)
sec=59;
Lcd8_Decimal2(count[cur],sec);
}
}
}
Lcd8_Command(0x0c);
Lcd8_Display(0x80," TIME IS ",16);
Lcd8_Display(0xc0," STORED ",16);
Delay(65000);Delay(65000);
dd=dec_hex(date);
mn=dec_hex(month);
yy=dec_hex(year);
hh=dec_hex(hour);
mm=dec_hex(min);
ss=dec_hex(sec);
DS1307_settime(hh,mm,ss);
DS1307_setdate(dd,mn,yy);
Lcd8_Command(0x01);
Lcd8_Display(First_Line,"Date:",5);
Lcd8_Display(Second_Line,"Time:",5);
Delay(65000);
}
void Lcd8_Init()
{
Lcd8_Command(0x38); //to select function set
Lcd8_Command(0x06); //entry mode set
Lcd8_Command(0x0c); //display on
Lcd8_Command(0x01); //clear display
}
void Lcd8_Command(unsigned char com)
{
Data_Port=com;
Lcd_en=1;
Lcd_rs=Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}
void Lcd8_Write(unsigned char com,unsigned char lr)
{
Lcd8_Command(com);
Data_Port=lr; // Data
Lcd_en=Lcd_rs=1;
Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}
void Lcd8_Display(unsigned char com,const unsigned char *word,unsigned int n)
{
unsigned char Lcd_i;
for(Lcd_i=0;Lcd_i<n;Lcd_i++)
{
Lcd8_Write(com+Lcd_i,word[Lcd_i]);
}
}
void Lcd8_Decimal2(unsigned char com,unsigned char val)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
Lcd8_Write(com,Lcd_t+0x30);
Lcd8_Write(com+1,Lcd_o+0x30);
}
void Lcd8_Decimal3(unsigned char com,unsigned char val)
{
unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;
Lcd_h=val/100;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
Lcd8_Write(com,Lcd_h+0x30);
Lcd8_Write(com+1,Lcd_t+0x30);
Lcd8_Write(com+2,Lcd_o+0x30);
}
void Lcd8_Decimal4(unsigned char com,unsigned int val)
{
unsigned int Lcd_th,Lcd_thr,Lcd_h,Lcd_hr,Lcd_t,Lcd_o;
val = val%10000;
Lcd_th=val/1000;
Lcd_thr=val%1000;
Lcd_h=Lcd_thr/100;
Lcd_hr=Lcd_thr%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
Lcd8_Write(com,Lcd_th+0x30);
Lcd8_Write(com+1,Lcd_h+0x30);
Lcd8_Write(com+2,Lcd_t+0x30);
Lcd8_Write(com+3,Lcd_o+0x30);
}
void Delay(unsigned int del)
{
while(del--);
}
void del()
{
Delay(65000);
Delay(65000);
}
/*********************************LCD HEADER FILE END*************************************/
/*********************************RTC HEADER FILE START*********************************/
DS1307_get(unsigned char addr)
{
Rtc_Start();
Rtc_Read(addr);
Rtc_Stop();
return;
}
void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
Rtc_Start();
Rtc_Write(0x00,ss); /* Write sec on RAM address 00H */
Rtc_Write(0x01,mm); /* Write min on RAM address 01H */
Rtc_Write(0x02,hh); /* Write hour on RAM address 02H */
Rtc_Stop(); /* Stop i2c bus */
}
void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy)
{
Rtc_Start();
Rtc_Write(0x04,dd); /* Write date on RAM address 04H */
Rtc_Write(0x05,mm); /* Write month on RAM address 05H */
Rtc_Write(0x06,yy); /* Write year on RAM address 06H */
Rtc_Stop(); /* Stop i2c bus */
}
Send2lcd(unsigned char value)
{
unsigned char buf1,buf2,buf = 0;
buf1 = value & 0xF0; /* Filter for high byte */
buf1 = (buf1>>4); /* Convert to ascii code */
buf2 = value & 0x0F; /* Filter for low byte */
buf=(buf1*10)+buf2;
return buf;
}
void Rtc_Init()//lower order 256 bytes of the chip
{
Rtc_add_wr=0xd0;
Rtc_add_rd=0xd1;
}
void Rtc_Write(unsigned char zig,unsigned char zag)// program to write to EEPROM
{
dat_rtc=zig;
temp_rtc=zag;
Rtc_rd_wr_sub();
above:
d_rtc=temp_rtc;
Rtc_Tx();
if (CY==1)goto above;
CY=0;
Rtc_Stop();
}
Rtc_Read(unsigned char zig)// program to read from EEPROM
{
dat_rtc=zig;
Rtc_rd_wr_sub();
Rtc_Start();
be:
d_rtc=Rtc_add_rd; // 0xd1 =Rtc_add_rd
Rtc_Tx();
if(CY==1)goto be;
Rtc_Rx();
Rtc_Ack();
CY=0;
Rtc_Stop();
return(datain_rtc);
}
void Rtc_Start()// must for any operation on EEPROM
{
sda_rtc=1;
scl_rtc=1;
sda_rtc=0;
scl_rtc=0;
}
void Rtc_Stop()// this is similar to the START operation whereas this should be performed after the completion of any operation
{
sda_rtc=0;
scl_rtc=1;
sda_rtc=1;
}
void Rtc_Tx()// program to send the device address, read/write address,data to be written
{
signed char i_rtc;
for(i_rtc=7;i_rtc>=0;i_rtc--)// should necessarily be initialised as signed char.
{
CY=(d_rtc>>i_rtc)&0x01;
sda_rtc=CY;
scl_rtc=1;// clock is essential inorder to write or read
scl_rtc=0;// clk should be alternated
}
sda_rtc=1;
scl_rtc=1;
CY=sda_rtc;
scl_rtc=0;
}
void Rtc_Rx()// program read the data from the EEPROM
{
unsigned char l_rtc;
sda_rtc=1;
for (l_rtc=0;l_rtc<=7;l_rtc++)
{
scl_rtc=1;
in_rtc=in_rtc<<1;
in_rtc|=sda_rtc;
scl_rtc=0;
}
datain_rtc=in_rtc;
in_rtc=0;
}
void Rtc_Ack()// this is to intimate the EEPROM that the read operation is over
{
sda_rtc=1;
scl_rtc=1;
scl_rtc=0;
}
void Rtc_rd_wr_sub()// this routine will be used by both the read & write operations to send the device address & the address at which the corresponding action is to be taken
{
Rtc_Start();
here1:
d_rtc=Rtc_add_wr;// 0xd0 device address is passed
Rtc_Tx();
if(CY==1)goto here1;
again1:
d_rtc=dat_rtc;// the address from which data is to be read/written is to be passed
Rtc_Tx();
if(CY==1)goto again1;
}
(Line 239) this doesn´t fit to 12h mode.if(hour==0)
hour=23;
(Line 231) this doesn´t fit to 12h mode.if(hour>=24)
hour=0;
Where did you folow this?The DS1307 can be run in either 12-hour or 24-hour mode. Bit 6 of the hours register is defined as the 12-hour or 24-hour mode-select bit. When high, the 12-hour mode is selected. In the 12-hour mode, bit 5 is the AM/PM bit with logic high being PM. In the 24-hour mode, bit 5 is the second 10-hour bit (20 to 23 hours). The hours value must be re-entered whenever the 12/24-hour mode bit is changed.
hour=hour & 0×1f; in line 101,
You should not write 0x40 into register, but (hour_value OR 0x40)
In what line do you READ the clock´s content? There you should mask it...And for sure when you read the value back then you will see all bits. But since in 12 h Mode only 5 lower bits represent the hours you need to mask it: hours_bcd = hours_read AND 0x1F.
#include<reg51.h>
sbit set=P1^0;
sbit inc=P1^2;
sbit mov=P1^1;
sbit dec=P3^7;
sbit ent=P1^4;
void all_disp();
void time_set();
#define First_Line 0x80
#define Second_Line 0xc0
#define Curser_On 0x0f
#define Curser_Off 0x0c
#define Clear_Display 0x01
#define Data_Port P0
sbit Lcd_rs = P2^0;
sbit Lcd_rw = P2^1;
sbit Lcd_en = P2^2;
sbit sda_rtc =P2^5;
sbit scl_rtc =P2^4;
/*sbit Lcd_rs = P3^5;
sbit Lcd_rw = P3^6;
sbit Lcd_en = P3^7; */
bit d;
void Lcd8_Init();
void Lcd8_Command(unsigned char);
void Lcd8_Write(unsigned char,unsigned char);
void Lcd8_Display(unsigned char,const unsigned char*,unsigned int);
void Lcd8_Decimal2(unsigned char,unsigned char);
void Lcd8_Decimal3(unsigned char,unsigned char);
void Lcd8_Decimal4(unsigned char,unsigned int);
void Delay(unsigned int);
void del();
#define DS1307_ID 0xD0
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02
#define DATE 0x04
#define MONTH 0x05
#define YEAR 0x06
DS1307_get(unsigned char);
void DS1307_settime(unsigned char, unsigned char, unsigned char);
void DS1307_setdate(unsigned char, unsigned char, unsigned char);
Send2lcd(unsigned char);
void Rtc_Write(unsigned char,unsigned char);
Rtc_Read(unsigned char);
void Rtc_rd_wr_sub();
void Rtc_Init();
void Rtc_Start();
void Rtc_Tx();
void Rtc_Rx();
void Rtc_Stop();
void Rtc_Ack();
void display_ampm();
void AM_PM_mode();
void set_mod();
unsigned int Rtc_add_wr,Rtc_add_rd;
unsigned char d_rtc,datain_rtc,in_rtc,temp_rtc,dat_rtc,flag_rtc;
unsigned char i,j,a[10],cur=0;
unsigned char sec,hour,min,date,month,year,mode,sec1,hour1,min1,sec2, hour2,min2,hh,mm,ss,dd,mn,yy;
unsigned char count[7]={0x86,0x89,0x8c,0xc6,0xc9,0xcc,0xce};
unsigned char dec_hex(unsigned char tt)
{
if(tt>59)
tt+=36;
else if(tt>49)
tt+=30;
else if(tt>39)
tt+=24;
else if(tt>29)
tt+=18;
else if(tt>19)
tt+=12;
else if(tt>9)
tt+=6;
return(tt);
}
void all_disp()
{
sec = DS1307_get(SEC);
min = DS1307_get(MIN);
hour = DS1307_get(HOUR);
AM_PM_mode();
hour=hour& 0x1f;
display_ampm();
date = DS1307_get(DATE);
month = DS1307_get(MONTH);
year = DS1307_get(YEAR);
sec=Send2lcd(sec);
min=Send2lcd(min);
hour=Send2lcd(hour);
date=Send2lcd(date);
month=Send2lcd(month);
year=Send2lcd(year);
Lcd8_Decimal2(0x86,date);
Lcd8_Write(0x88,'-');
Lcd8_Decimal2(0x89,month);
Lcd8_Write(0x8b,'-');
Lcd8_Decimal2(0x8c,year);
Lcd8_Decimal2(0xc6,hour);
Lcd8_Write(0xc8,'.');
Lcd8_Decimal2(0xc9,min);
Lcd8_Write(0xcb,'.');
Lcd8_Decimal2(0xcc,sec);
}
void main()
{
Lcd8_Init();
Lcd8_Display(0x80,"RTC TESTING: ",16);
Lcd8_Display(0xc0,"ZAKARIYA: ",16);
Delay(65000);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(First_Line,"Date:",5);
Lcd8_Display(Second_Line,"Time:",5);
Rtc_Init();
//DS1307_setdate(0x04,0x11,0x11);
//DS1307_settime(0x16,0x00,0x00);
while(1)
{
all_disp();
if(!set)
time_set();
}
}
void AM_PM_mode()
{
if(hour==0x00)
{
d=0; // for displaying AM
Rtc_Start();
Rtc_Write(0x02,0x40);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x01)
{
d=0; // for displaying AM
Rtc_Start();
Rtc_Write(0x02,0x41);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x02)
{
d=0; // for displaying AM
Rtc_Start();
Rtc_Write(0x02,0x42);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x03)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x43);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x04)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x44);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x05)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x45);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x06)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x46);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x07)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x47);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x08)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x48);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x09)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x49);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x10)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x4a);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x11)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x4b);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x12)
{
d=1; // for displaying PM
Rtc_Start();
Rtc_Write(0x02,0x72);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x13)
{
d=1; // for displaying PM
Rtc_Start();
Rtc_Write(0x02,0x61);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x14)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x62);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x15)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x63);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x16)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x64);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x17)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x65);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x18)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x66);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x19)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x67);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x20)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x68);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x21)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x69);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x22)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x6a);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x23)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x6b);
Rtc_Stop(); /* Stop i2c bus */
}
}
void setco()
{
//d=0;
}
void display_ampm()
{
if(d==0)
Lcd8_Display(0xce,"AM ",2);/////////////////////////////////////////////////////////changed
if(d==1)
Lcd8_Display(0xce,"PM ",2);/////////////////////////////////////////////////////////changed
}
void time_set()
{
Lcd8_Display(0x80," TIME SET ",16);
Lcd8_Display(0xc0," MODE ",16);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(First_Line,"Date:",5);
Lcd8_Display(Second_Line,"Time:",5);
all_disp();
cur=0;
while(ent)
{
Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);
if(mov==0)
{
while(!mov);
cur++;
if(cur==7)
cur=0;
}
else if(count[cur]==0x86)
{
if(inc==0)
{
while(!inc);
date++;
if(date>=32)
date=1;
Lcd8_Decimal2(count[cur],date);
}
else if(dec==0)
{
while(!dec);
date--;
if(date==0xff)
date=31;
Lcd8_Decimal2(count[cur],date);
}
}
else if(count[cur]==0x89)
{
if(inc==0)
{
while(!inc);
month++;
if(month>=13)
month=1;
Lcd8_Decimal2(count[cur],month);
}
else if(dec==0)
{
while(!dec);
month--;
if(month==0xff)
month=12;
Lcd8_Decimal2(count[cur],month);
}
}
else if(count[cur]==0x8c)
{
if(inc==0)
{
while(!inc);
year++;
if(year>99)
year=0;
Lcd8_Decimal2(count[cur],year);
}
else if(dec==0)
{
while(!dec);
year--;
if(year==0xff)
year=99;
Lcd8_Decimal2(count[cur],year);
}
}
else if(count[cur]==0xc6)
{
if(inc==0)
{
while(inc==0);
hour++;
if(hour>12)
hour=0;
Lcd8_Decimal2(count[cur],hour);
}
else if(dec==0)
{
while(dec==0);
hour--;
if(hour==0)
hour=12;
Lcd8_Decimal2(count[cur],hour);
}
}
else if(count[cur]==0xc9)
{
if(inc==0)
{
while(inc==0);
min++;
if(min>=60)
min=0;
Lcd8_Decimal2(count[cur],min);
}
else if(dec==0)
{
while(dec==0);
min--;
if(min==0)
min=59;
Lcd8_Decimal2(count[cur],min);
}
}
else if(count[cur]==0xcc)
{
if(inc==0)
{
while(inc==0);
Lcd8_Command(0x0c);
sec++;
if(sec>=60)
sec=0;
Lcd8_Decimal2(count[cur],sec);
}
else if(dec==0)
{
while(dec==0);
Lcd8_Command(0x0c);
sec--;
if(sec==0xff)
sec=59;
Lcd8_Decimal2(count[cur],sec);
}
}
else if(count[cur]==0xce)
{
if(inc==0)
{
while(inc==0);
Lcd8_Command(0x0c);
mode++;
set_mod();
if(mode>=2)
mode=0;
//Lcd8_Decimal2(count[cur],mode);
}
else if(dec==0)
{
while(dec==0);
Lcd8_Command(0x0c);
mode--;
set_mod();
if(mode==0)
mode=1;
//Lcd8_Decimal2(count[cur],mode);
}
}
}
Lcd8_Command(0x0c);
Lcd8_Display(0x80," TIME IS ",16);
Lcd8_Display(0xc0," STORED ",16);
Delay(65000);Delay(65000);
dd=dec_hex(date);
mn=dec_hex(month);
yy=dec_hex(year);
hh=dec_hex(hour);
mm=dec_hex(min);
ss=dec_hex(sec);
if(d==1)
hh= hh | 0x60;
if(d==0)
hh= hh | 0x40;
DS1307_settime(hh,mm,ss);
DS1307_setdate(dd,mn,yy);
Lcd8_Command(0x01);
Lcd8_Display(First_Line,"Date:",5);
Lcd8_Display(Second_Line,"Time:",5);
Delay(65000);
}
void set_mod()
{
if(mode==1)
{
d=0;
Lcd8_Display(0xce,"AM ",2);/////////////////////////////////////////////////////////changed
}
if(mode==2)
{
d=1;
Lcd8_Display(0xce,"PM ",2);/////////////////////////////////////////////////////////changed
}
}
void Lcd8_Init()
{
Lcd8_Command(0x38); //to select function set
Lcd8_Command(0x06); //entry mode set
Lcd8_Command(0x0c); //display on
Lcd8_Command(0x01); //clear display
}
void Lcd8_Command(unsigned char com)
{
Data_Port=com;
Lcd_en=1;
Lcd_rs=Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}
void Lcd8_Write(unsigned char com,unsigned char lr)
{
Lcd8_Command(com);
Data_Port=lr; // Data
Lcd_en=Lcd_rs=1;
Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}
void Lcd8_Display(unsigned char com,const unsigned char *word,unsigned int n)
{
unsigned char Lcd_i;
for(Lcd_i=0;Lcd_i<n;Lcd_i++)
{
Lcd8_Write(com+Lcd_i,word[Lcd_i]);
}
}
void Lcd8_Decimal2(unsigned char com,unsigned char val)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
Lcd8_Write(com,Lcd_t+0x30);
Lcd8_Write(com+1,Lcd_o+0x30);
}
void Lcd8_Decimal3(unsigned char com,unsigned char val)
{
unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;
Lcd_h=val/100;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
Lcd8_Write(com,Lcd_h+0x30);
Lcd8_Write(com+1,Lcd_t+0x30);
Lcd8_Write(com+2,Lcd_o+0x30);
}
void Lcd8_Decimal4(unsigned char com,unsigned int val)
{
unsigned int Lcd_th,Lcd_thr,Lcd_h,Lcd_hr,Lcd_t,Lcd_o;
val = val%10000;
Lcd_th=val/1000;
Lcd_thr=val%1000;
Lcd_h=Lcd_thr/100;
Lcd_hr=Lcd_thr%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
Lcd8_Write(com,Lcd_th+0x30);
Lcd8_Write(com+1,Lcd_h+0x30);
Lcd8_Write(com+2,Lcd_t+0x30);
Lcd8_Write(com+3,Lcd_o+0x30);
}
void Delay(unsigned int del)
{
while(del--);
}
void del()
{
Delay(65000);
Delay(65000);
}
/*********************************LCD HEADER FILE END*************************************/
/*********************************RTC HEADER FILE START*********************************/
DS1307_get(unsigned char addr)
{
Rtc_Start();
Rtc_Read(addr);
Rtc_Stop();
return;
}
void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
Rtc_Start();
Rtc_Write(0x00,ss); /* Write sec on RAM address 00H */
Rtc_Write(0x01,mm); /* Write min on RAM address 01H */
Rtc_Write(0x02,hh); /* Write hour on RAM address 02H */
Rtc_Stop(); /* Stop i2c bus */
}
void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy)
{
Rtc_Start();
Rtc_Write(0x04,dd); /* Write date on RAM address 04H */
Rtc_Write(0x05,mm); /* Write month on RAM address 05H */
Rtc_Write(0x06,yy); /* Write year on RAM address 06H */
Rtc_Stop(); /* Stop i2c bus */
}
Send2lcd(unsigned char value)
{
unsigned char buf1,buf2,buf = 0;
buf1 = value & 0xF0; /* Filter for high byte */
buf1 = (buf1>>4); /* Convert to ascii code */
buf2 = value & 0x0F; /* Filter for low byte */
buf=(buf1*10)+buf2;
return buf;
}
void Rtc_Init()//lower order 256 bytes of the chip
{
Rtc_add_wr=0xd0;
Rtc_add_rd=0xd1;
}
void Rtc_Write(unsigned char zig,unsigned char zag)// program to write to EEPROM
{
dat_rtc=zig;
temp_rtc=zag;
Rtc_rd_wr_sub();
above:
d_rtc=temp_rtc;
Rtc_Tx();
if (CY==1)goto above;
CY=0;
Rtc_Stop();
}
Rtc_Read(unsigned char zig)// program to read from EEPROM
{
dat_rtc=zig;
Rtc_rd_wr_sub();
Rtc_Start();
be:
d_rtc=Rtc_add_rd; // 0xd1 =Rtc_add_rd
Rtc_Tx();
if(CY==1)goto be;
Rtc_Rx();
Rtc_Ack();
CY=0;
Rtc_Stop();
return(datain_rtc);
}
void Rtc_Start()// must for any operation on EEPROM
{
sda_rtc=1;
scl_rtc=1;
sda_rtc=0;
scl_rtc=0;
}
void Rtc_Stop()// this is similar to the START operation whereas this should be performed after the completion of any operation
{
sda_rtc=0;
scl_rtc=1;
sda_rtc=1;
}
void Rtc_Tx()// program to send the device address, read/write address,data to be written
{
signed char i_rtc;
for(i_rtc=7;i_rtc>=0;i_rtc--)// should necessarily be initialised as signed char.
{
CY=(d_rtc>>i_rtc)&0x01;
sda_rtc=CY;
scl_rtc=1;// clock is essential inorder to write or read
scl_rtc=0;// clk should be alternated
}
sda_rtc=1;
scl_rtc=1;
CY=sda_rtc;
scl_rtc=0;
}
void Rtc_Rx()// program read the data from the EEPROM
{
unsigned char l_rtc;
sda_rtc=1;
for (l_rtc=0;l_rtc<=7;l_rtc++)
{
scl_rtc=1;
in_rtc=in_rtc<<1;
in_rtc|=sda_rtc;
scl_rtc=0;
}
datain_rtc=in_rtc;
in_rtc=0;
}
void Rtc_Ack()// this is to intimate the EEPROM that the read operation is over
{
sda_rtc=1;
scl_rtc=1;
scl_rtc=0;
}
void Rtc_rd_wr_sub()// this routine will be used by both the read & write operations to send the device address & the address at which the corresponding action is to be taken
{
Rtc_Start();
here1:
d_rtc=Rtc_add_wr;// 0xd0 device address is passed
Rtc_Tx();
if(CY==1)goto here1;
again1:
d_rtc=dat_rtc;// the address from which data is to be read/written is to be passed
Rtc_Tx();
if(CY==1)goto again1;
}
#include<reg51.h>
sbit set=P1^0;
sbit inc=P1^2;
sbit mov=P1^1;
sbit dec=P3^7;
sbit ent=P1^4;
void all_disp();
void time_set();
#define First_Line 0x80
#define Second_Line 0xc0
#define Curser_On 0x0f
#define Curser_Off 0x0c
#define Clear_Display 0x01
#define Data_Port P0
sbit Lcd_rs = P2^0;
sbit Lcd_rw = P2^1;
sbit Lcd_en = P2^2;
sbit sda_rtc =P2^5;
sbit scl_rtc =P2^4;
/*sbit Lcd_rs = P3^5;
sbit Lcd_rw = P3^6;
sbit Lcd_en = P3^7; */
bit d;
void Lcd8_Init();
void Lcd8_Command(unsigned char);
void Lcd8_Write(unsigned char,unsigned char);
void Lcd8_Display(unsigned char,const unsigned char*,unsigned int);
void Lcd8_Decimal2(unsigned char,unsigned char);
void Lcd8_Decimal3(unsigned char,unsigned char);
void Lcd8_Decimal4(unsigned char,unsigned int);
void Delay(unsigned int);
void del();
#define DS1307_ID 0xD0
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02
#define DATE 0x04
#define MONTH 0x05
#define YEAR 0x06
DS1307_get(unsigned char);
void DS1307_settime(unsigned char, unsigned char, unsigned char);
void DS1307_setdate(unsigned char, unsigned char, unsigned char);
Send2lcd(unsigned char);
void Rtc_Write(unsigned char,unsigned char);
Rtc_Read(unsigned char);
void Rtc_rd_wr_sub();
void Rtc_Init();
void Rtc_Start();
void Rtc_Tx();
void Rtc_Rx();
void Rtc_Stop();
void Rtc_Ack();
void display_ampm();
void AM_PM_mode();
void set_mod();
unsigned int Rtc_add_wr,Rtc_add_rd;
unsigned char d_rtc,datain_rtc,in_rtc,temp_rtc,dat_rtc,flag_rtc;
unsigned char i,j,a[10],cur=0;
unsigned char sec,hour,min,date,month,year,mode,sec1,hour1,min1,sec2, hour2,min2,hh,mm,ss,dd,mn,yy;
unsigned char count[7]={0x86,0x89,0x8c,0xc6,0xc9,0xcc,0xce};
unsigned char dec_hex(unsigned char tt)
{
if(tt>59)
tt+=36;
else if(tt>49)
tt+=30;
else if(tt>39)
tt+=24;
else if(tt>29)
tt+=18;
else if(tt>19)
tt+=12;
else if(tt>9)
tt+=6;
return(tt);
}
void all_disp()
{
sec = DS1307_get(SEC);
min = DS1307_get(MIN);
hour = DS1307_get(HOUR);
AM_PM_mode();
hour=hour& 0x1f;
display_ampm();
date = DS1307_get(DATE);
month = DS1307_get(MONTH);
year = DS1307_get(YEAR);
sec=Send2lcd(sec);
min=Send2lcd(min);
hour=Send2lcd(hour);
date=Send2lcd(date);
month=Send2lcd(month);
year=Send2lcd(year);
Lcd8_Decimal2(0x86,date);
Lcd8_Write(0x88,'-');
Lcd8_Decimal2(0x89,month);
Lcd8_Write(0x8b,'-');
Lcd8_Decimal2(0x8c,year);
Lcd8_Decimal2(0xc6,hour);
Lcd8_Write(0xc8,'.');
Lcd8_Decimal2(0xc9,min);
Lcd8_Write(0xcb,'.');
Lcd8_Decimal2(0xcc,sec);
}
void main()
{
Lcd8_Init();
Lcd8_Display(0x80,"RTC TESTING: ",16);
Lcd8_Display(0xc0,"ZAKARIYA: ",16);
Delay(65000);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(First_Line,"Date:",5);
Lcd8_Display(Second_Line,"Time:",5);
Rtc_Init();
//DS1307_setdate(0x04,0x11,0x11);
//DS1307_settime(0x16,0x00,0x00);
while(1)
{
all_disp();
if(!set)
time_set();
}
}
void AM_PM_mode()
{
if(hour==0x00)
{
d=0; // for displaying AM
Rtc_Start();
Rtc_Write(0x02,0x40);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x01)
{
d=0; // for displaying AM
Rtc_Start();
Rtc_Write(0x02,0x41);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x02)
{
d=0; // for displaying AM
Rtc_Start();
Rtc_Write(0x02,0x42);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x03)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x43);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x04)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x44);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x05)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x45);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x06)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x46);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x07)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x47);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x08)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x48);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x09)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x49);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x10)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x4a);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x11)
{
d=0;
Rtc_Start();
Rtc_Write(0x02,0x4b);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x12)
{
d=1; // for displaying PM
Rtc_Start();
Rtc_Write(0x02,0x72);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x13)
{
d=1; // for displaying PM
Rtc_Start();
Rtc_Write(0x02,0x61);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x14)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x62);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x15)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x63);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x16)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x64);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x17)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x65);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x18)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x66);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x19)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x67);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x20)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x68);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x21)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x69);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x22)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x6a);
Rtc_Stop(); /* Stop i2c bus */
}
if(hour==0x23)
{
d=1;
Rtc_Start();
Rtc_Write(0x02,0x6b);
Rtc_Stop(); /* Stop i2c bus */
}
}
void setco()
{
//d=0;
}
void display_ampm()
{
if(d==0)
Lcd8_Display(0xce,"AM ",2);/////////////////////////////////////////////////////////changed
if(d==1)
Lcd8_Display(0xce,"PM ",2);/////////////////////////////////////////////////////////changed
}
void time_set()
{
Lcd8_Display(0x80," TIME SET ",16);
Lcd8_Display(0xc0," MODE ",16);
Delay(65000);
Lcd8_Command(0x01);
Lcd8_Display(First_Line,"Date:",5);
Lcd8_Display(Second_Line,"Time:",5);
all_disp();
cur=0;
while(ent)
{
Lcd8_Command(0x0f);
Lcd8_Command(count[cur]);
if(mov==0)
{
while(!mov);
cur++;
if(cur==7)
cur=0;
}
else if(count[cur]==0x86)
{
if(inc==0)
{
while(!inc);
date++;
if(date>=32)
date=1;
Lcd8_Decimal2(count[cur],date);
}
else if(dec==0)
{
while(!dec);
date--;
if(date==0xff)
date=31;
Lcd8_Decimal2(count[cur],date);
}
}
else if(count[cur]==0x89)
{
if(inc==0)
{
while(!inc);
month++;
if(month>=13)
month=1;
Lcd8_Decimal2(count[cur],month);
}
else if(dec==0)
{
while(!dec);
month--;
if(month==0xff)
month=12;
Lcd8_Decimal2(count[cur],month);
}
}
else if(count[cur]==0x8c)
{
if(inc==0)
{
while(!inc);
year++;
if(year>99)
year=0;
Lcd8_Decimal2(count[cur],year);
}
else if(dec==0)
{
while(!dec);
year--;
if(year==0xff)
year=99;
Lcd8_Decimal2(count[cur],year);
}
}
else if(count[cur]==0xc6)
{
if(inc==0)
{
while(inc==0);
hour++;
if(hour>12)
hour=0;
Lcd8_Decimal2(count[cur],hour);
}
else if(dec==0)
{
while(dec==0);
hour--;
if(hour==0)
hour=12;
Lcd8_Decimal2(count[cur],hour);
}
}
else if(count[cur]==0xc9)
{
if(inc==0)
{
while(inc==0);
min++;
if(min>=60)
min=0;
Lcd8_Decimal2(count[cur],min);
}
else if(dec==0)
{
while(dec==0);
min--;
if(min==0)
min=59;
Lcd8_Decimal2(count[cur],min);
}
}
else if(count[cur]==0xcc)
{
if(inc==0)
{
while(inc==0);
Lcd8_Command(0x0c);
sec++;
if(sec>=60)
sec=0;
Lcd8_Decimal2(count[cur],sec);
}
else if(dec==0)
{
while(dec==0);
Lcd8_Command(0x0c);
sec--;
if(sec==0xff)
sec=59;
Lcd8_Decimal2(count[cur],sec);
}
}
else if(count[cur]==0xce)
{
if(inc==0)
{
while(inc==0);
Lcd8_Command(0x0c);
mode++;
set_mod();
if(mode>=2)
mode=0;
//Lcd8_Decimal2(count[cur],mode);
}
else if(dec==0)
{
while(dec==0);
Lcd8_Command(0x0c);
mode--;
set_mod();
if(mode==0)
mode=1;
//Lcd8_Decimal2(count[cur],mode);
}
}
}
Lcd8_Command(0x0c);
Lcd8_Display(0x80," TIME IS ",16);
Lcd8_Display(0xc0," STORED ",16);
Delay(65000);Delay(65000);
dd=dec_hex(date);
mn=dec_hex(month);
yy=dec_hex(year);
hh=dec_hex(hour);
mm=dec_hex(min);
ss=dec_hex(sec);
if(d==1)
hh= hh | 0x60;
if(d==0)
hh= hh | 0x40;
DS1307_settime(hh,mm,ss);
DS1307_setdate(dd,mn,yy);
Lcd8_Command(0x01);
Lcd8_Display(First_Line,"Date:",5);
Lcd8_Display(Second_Line,"Time:",5);
Delay(65000);
}
void set_mod()
{
if(mode==1)
{
d=0;
Lcd8_Display(0xce,"AM ",2);/////////////////////////////////////////////////////////changed
}
if(mode==2)
{
d=1;
Lcd8_Display(0xce,"PM ",2);/////////////////////////////////////////////////////////changed
}
}
void Lcd8_Init()
{
Lcd8_Command(0x38); //to select function set
Lcd8_Command(0x06); //entry mode set
Lcd8_Command(0x0c); //display on
Lcd8_Command(0x01); //clear display
}
void Lcd8_Command(unsigned char com)
{
Data_Port=com;
Lcd_en=1;
Lcd_rs=Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}
void Lcd8_Write(unsigned char com,unsigned char lr)
{
Lcd8_Command(com);
Data_Port=lr; // Data
Lcd_en=Lcd_rs=1;
Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}
void Lcd8_Display(unsigned char com,const unsigned char *word,unsigned int n)
{
unsigned char Lcd_i;
for(Lcd_i=0;Lcd_i<n;Lcd_i++)
{
Lcd8_Write(com+Lcd_i,word[Lcd_i]);
}
}
void Lcd8_Decimal2(unsigned char com,unsigned char val)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
Lcd8_Write(com,Lcd_t+0x30);
Lcd8_Write(com+1,Lcd_o+0x30);
}
void Lcd8_Decimal3(unsigned char com,unsigned char val)
{
unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;
Lcd_h=val/100;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
Lcd8_Write(com,Lcd_h+0x30);
Lcd8_Write(com+1,Lcd_t+0x30);
Lcd8_Write(com+2,Lcd_o+0x30);
}
void Lcd8_Decimal4(unsigned char com,unsigned int val)
{
unsigned int Lcd_th,Lcd_thr,Lcd_h,Lcd_hr,Lcd_t,Lcd_o;
val = val%10000;
Lcd_th=val/1000;
Lcd_thr=val%1000;
Lcd_h=Lcd_thr/100;
Lcd_hr=Lcd_thr%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
Lcd8_Write(com,Lcd_th+0x30);
Lcd8_Write(com+1,Lcd_h+0x30);
Lcd8_Write(com+2,Lcd_t+0x30);
Lcd8_Write(com+3,Lcd_o+0x30);
}
void Delay(unsigned int del)
{
while(del--);
}
void del()
{
Delay(65000);
Delay(65000);
}
/*********************************LCD HEADER FILE END*************************************/
/*********************************RTC HEADER FILE START*********************************/
DS1307_get(unsigned char addr)
{
Rtc_Start();
Rtc_Read(addr);
Rtc_Stop();
return;
}
void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
Rtc_Start();
Rtc_Write(0x00,ss); /* Write sec on RAM address 00H */
Rtc_Write(0x01,mm); /* Write min on RAM address 01H */
Rtc_Write(0x02,hh); /* Write hour on RAM address 02H */
Rtc_Stop(); /* Stop i2c bus */
}
void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy)
{
Rtc_Start();
Rtc_Write(0x04,dd); /* Write date on RAM address 04H */
Rtc_Write(0x05,mm); /* Write month on RAM address 05H */
Rtc_Write(0x06,yy); /* Write year on RAM address 06H */
Rtc_Stop(); /* Stop i2c bus */
}
Send2lcd(unsigned char value)
{
unsigned char buf1,buf2,buf = 0;
buf1 = value & 0xF0; /* Filter for high byte */
buf1 = (buf1>>4); /* Convert to ascii code */
buf2 = value & 0x0F; /* Filter for low byte */
buf=(buf1*10)+buf2;
return buf;
}
void Rtc_Init()//lower order 256 bytes of the chip
{
Rtc_add_wr=0xd0;
Rtc_add_rd=0xd1;
}
void Rtc_Write(unsigned char zig,unsigned char zag)// program to write to EEPROM
{
dat_rtc=zig;
temp_rtc=zag;
Rtc_rd_wr_sub();
above:
d_rtc=temp_rtc;
Rtc_Tx();
if (CY==1)goto above;
CY=0;
Rtc_Stop();
}
Rtc_Read(unsigned char zig)// program to read from EEPROM
{
dat_rtc=zig;
Rtc_rd_wr_sub();
Rtc_Start();
be:
d_rtc=Rtc_add_rd; // 0xd1 =Rtc_add_rd
Rtc_Tx();
if(CY==1)goto be;
Rtc_Rx();
Rtc_Ack();
CY=0;
Rtc_Stop();
return(datain_rtc);
}
void Rtc_Start()// must for any operation on EEPROM
{
sda_rtc=1;
scl_rtc=1;
sda_rtc=0;
scl_rtc=0;
}
void Rtc_Stop()// this is similar to the START operation whereas this should be performed after the completion of any operation
{
sda_rtc=0;
scl_rtc=1;
sda_rtc=1;
}
void Rtc_Tx()// program to send the device address, read/write address,data to be written
{
signed char i_rtc;
for(i_rtc=7;i_rtc>=0;i_rtc--)// should necessarily be initialised as signed char.
{
CY=(d_rtc>>i_rtc)&0x01;
sda_rtc=CY;
scl_rtc=1;// clock is essential inorder to write or read
scl_rtc=0;// clk should be alternated
}
sda_rtc=1;
scl_rtc=1;
CY=sda_rtc;
scl_rtc=0;
}
void Rtc_Rx()// program read the data from the EEPROM
{
unsigned char l_rtc;
sda_rtc=1;
for (l_rtc=0;l_rtc<=7;l_rtc++)
{
scl_rtc=1;
in_rtc=in_rtc<<1;
in_rtc|=sda_rtc;
scl_rtc=0;
}
datain_rtc=in_rtc;
in_rtc=0;
}
void Rtc_Ack()// this is to intimate the EEPROM that the read operation is over
{
sda_rtc=1;
scl_rtc=1;
scl_rtc=0;
}
void Rtc_rd_wr_sub()// this routine will be used by both the read & write operations to send the device address & the address at which the corresponding action is to be taken
{
Rtc_Start();
here1:
d_rtc=Rtc_add_wr;// 0xd0 device address is passed
Rtc_Tx();
if(CY==1)goto here1;
again1:
d_rtc=dat_rtc;// the address from which data is to be read/written is to be passed
Rtc_Tx();
if(CY==1)goto again1;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?