Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

seting 12 hour mode in ds 1307

Status
Not open for further replies.

tapu

Full Member level 4
Joined
Sep 15, 2014
Messages
234
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
india
Activity points
3,041
Dear all,
I studied the code of Ds 1307 online & designed alarm clock .clock running accurately but it is in 24 hour mode.how to set bit 6 of hour register?how to set it in 12 hour mode & read am/pm bit? Please reply.
 

Hi,

Datasheet says:
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.

I´m sure you once successfully wrote byte address 0x02 to set the hours...

Now, to the hours of byte address 0x02:
* OR 0x40 if you want to set 12h mode
* OR 0x20 if you are in 12h mode and want to set it to PM

then write this byte to the RTC

Klaus
 
Dear sir,when I write 40 in hour register then time shows 40 hour & second.how to set hour register? As hour register continuously read for time.
 

Hi,

You should not write 0x40 into register, but (hour_value OR 0x40)

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.

Klaus
 

[Moved]programming for am/pm mode in RTC ds 1307.

Dear All,
I have code for RTC clock by using 89c52.& i am using lcd 16x2 as display.Clock running successfully but time shows in 24 hour mode.I want am/pm mode.I am trying to get am/pm mode but still it is in 24 hour format.please check this code & reply.

Code:
		#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;
}
 

Re: [Moved]programming for am/pm mode in RTC ds 1307.

Hi,

you asked the same question about one week ago.

Your code is completely undocumented. But some information on the code helps us to understand your code and for sure it also helps you to understand your code.

****
You want it do work in 12h mode?

But in your code:
if(hour==0)
hour=23;
(Line 239) this doesn´t fit to 12h mode.

if(hour>=24)
hour=0;
(Line 231) this doesn´t fit to 12h mode.

In the last thread I told you where and how to set the RTC into 12h mode. But i can´t find it in your code.

In the last thread I told you where and how to mask the hour byte to 12h mode. But i can´t find it in your code.

Datasheet says:
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.
Where did you folow this?

It seems you didn´t do anything to set to 12h mode.

Please try to implement 12h mode. In case you have any problems show us code and a detailed error description.

Klaus
 

Dear sir,thank you for reply.
I have removed code step described above.i write
hour=hour & 0×1f; in line 101,but problem is same. Actually in which step I have to mask?
 

Hi,

In your code you WRITE to the clock:
hour=hour & 0×1f; in line 101,

but you should:
You should not write 0x40 into register, but (hour_value OR 0x40)

********
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.
In what line do you READ the clock´s content? There you should mask it...

Klaus
 

Dear sir,sorry for replying late.the logic ''hour_value OR 0x40'' can fit to every value of hour??
I wrote like this, clock shows am or pm but after 11:59AM/PM it does not change to PM/AM. please check it.
Code:
		#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;
}

- - - Updated - - -

Dear sir,sorry for replying late.the logic ''hour_value OR 0x40'' can fit to every value of hour??
I wrote like this, clock shows am or pm but after 11:59AM/PM it does not change to PM/AM. please check it.
Code:
		#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;
}
 

Hi,

Maybe I don't understand your code.

On the other hand I'm not sure if you treat the values right, especially sometimes you treat them like hex/bcd numbers and sometimes as decimal numbers.

Here the variable "hours". Is it decimal or bcd?.
The clock seems to store it in bcd. So 0x11 is 11 o'clock, but the decimal value is 17. I didn't see where you form from decimal to bcd and back.
***
Setting the 12th mode bit is one simple OR operation, no need for your many "if" operations.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top