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.

[51] RTC DS1307 Interfacing with 8051 problem

Status
Not open for further replies.

hello sir,
when i run the simulation rtc time is 8.33.00 and my pc time is 7.54 am.After 17 minutes rtc changed only 12 minutes(my pc time is 8.12 am) .
But proteus simulation time is 12.26 minutes.So if the statement in this thread

https://www.edaboard.com/threads/270617/

The time in proteus simulation is not same as real time of pc is true??

i am going to test my code also..after testing i will post the result...
i upload the picture also
when starting simulation
Untitled1.jpg

after 17 minutes
rtc.jpg
 
Last edited:

Yeah, The proteus simulations doesnt run in real time that will be lagging it becuase of CPU load allocation and animation timings....... you can change them altering the animation properties..
 
  • Like
Reactions: chana

    chana

    Points: 2
    Helpful Answer Positive Rating
hai venkadesh,
looks like that after 1 hour 3 minutes from 12.34 proteus simulation time is increased only 45 minutes .but rtc time also 12.34+45 minutes (1.24). So may be proteus is the problem...
hello jayanth,
i don't know timer interrupt this is the code,please help me


Code:
#include<reg51.h>
 #include"rtcds1307.h"
sbit rs=P1^0;
sbit en=P1^2;
sbit  sda_rtc =P1^3;
sbit  scl_rtc =P1^4;
sbit rw=P1^1;
sfr ldata=0xA0;	   //p2
sfr KEYPRT=0xB0;  // Port for keypad		
sbit ROW0 = P3^4;
sbit ROW1 = P3^5;
sbit ROW2 = P3^6;
sbit ROW3 = P3^7;
sbit COL0 = P3^0;
sbit COL1 = P3^1;
sbit COL2 = P3^2;
sbit COL3 = P3^3; 


DS1307_get(unsigned char);
unsigned char KEYGET(void);
void lcdcommnd(unsigned char value);
void lcddata(unsigned char value);
void lcd_putc(unsigned char ascii);
void MSDelay(int ms);
void LcdLine1(unsigned char *value);
void LcdLine2(unsigned char *value);
 send2lcd(unsigned char value);
 void time_set();

unsigned int Rtc_add_wr,Rtc_add_rd;
unsigned char d_rtc,datain_rtc,in_rtc,temp_rtc,dat_rtc,flag_rtc;
void MSDelay(int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1275;j++);
}
}
void lcdcommnd(unsigned char value)
{
	ldata= value;
	rs= 0;
	rw=0;
	en=1;
	MSDelay(1);
	en=0;
	return;

} 
void lcddata(unsigned char value)
{

	ldata = value;
	rs= 1;
	rw=0;
	en=1;
	MSDelay(1);
	en=0;
	return;


}
void lcd_string(unsigned char *value)	// Function to send string to LCD
{
	 int x;
	for(x=0;value[x]!=0;x++)
	{
		lcddata(value[x]);
	}

}
void lcd_init()
{
        lcdcommnd(0x38);
  	MSDelay(100);		
	lcdcommnd(0x0e);
	MSDelay(100);	   
	lcdcommnd(0x01);
	MSDelay(100);

}
void lcd_decimal2(unsigned char val)
{
    unsigned char d,d1,d2;
 d=val%100;
d1=d/10;
d2=d%10;
 lcddata(d1+48);
lcddata(d2+48);
}
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);
}
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;
} 
unsigned char KEYGET() 
{
unsigned char colloc, rowloc, keyvalue;
unsigned char keypad[4] [4] = {'0', '1', '2','3',
'4', '5', '6', '7',
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F'};
do
{ 
ROW0 = 0;
ROW1 = 0;
ROW2 = 0;
ROW3 = 0; // ground all rows
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits 
} while ( colloc != 0X0F); // check until all keys released

do
{ do
{
MSDelay(5);
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits
} while ( colloc == 0X0F); //keep checking for keypress

MSDelay(5); //delay for debounce
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits
} while ( colloc == 0X0F); //keep checking for keypress

while (1)

{
ROW0 = 0; // ground row0
ROW1 = 1;
ROW2 = 1;
ROW3 = 1;
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits
if( colloc!=0x0F)
{
rowloc = 0;
break; 
}

ROW0 = 1; // ground row1
ROW1 = 0;
ROW2 = 1;
ROW3 = 1;
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits
if( colloc!=0x0F)
{
rowloc = 1;
break; 
}

ROW0 = 1; // ground row2
ROW1 = 1;
ROW2 = 0;
ROW3 = 1;
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits
if( colloc!=0x0F)
{
rowloc = 2;
break; 
}
ROW0 = 1; // ground row3
ROW1 = 1;
ROW2 = 1;
ROW3 = 0;
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits
rowloc = 3;
break; 
}

if( colloc ==0x0E)
keyvalue = keypad [rowloc] [0];
else if ( colloc ==0x0D)
keyvalue = keypad [rowloc] [1];
else if ( colloc ==0x0B)
keyvalue = keypad [rowloc] [2];
else
keyvalue = keypad [rowloc] [3];
return (keyvalue);
}
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_Ack()// this is to intimate the EEPROM that the read operation is over
{
sda_rtc=1;
scl_rtc=1;
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_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_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;// 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;
}
 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;
Rtc_Tx();
if(CY==1)goto be;
Rtc_Rx();
Rtc_Ack();
CY=0;
Rtc_Stop();
return(datain_rtc);
}
 DS1307_get(unsigned char addr)
{
unsigned char ret;
 
Rtc_Start();            
ret = Rtc_Read(addr);
Rtc_Stop();
 return ret;
}
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();
}
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 */
}


void time_set()

{
unsigned int y,tens,ones;
unsigned char sec, min, hour, date, month, year;
lcdcommnd(0x01);	   
lcdcommnd(0x80);
lcd_string("HR:MN:SC");
////////////////hour/////////////////////
y = KEYGET();
tens = y & 0x0F;
tens = (tens << 4) & 0xF0;
lcdcommnd(0XC0);
lcddata(y);
y = KEYGET();
ones = y & 0x0F;
lcdcommnd(0XC1);
lcddata(y);
hour = (tens |ones );
lcdcommnd(0XC2);
lcddata(':');
////////////////min/////////////////////
y = KEYGET();
tens = y & 0x0F;
tens = (tens << 4) & 0xF0;
lcdcommnd(0XC3);
lcddata(y);
y = KEYGET();
ones = y & 0x0F;
lcdcommnd(0XC4);
lcddata(y);
min = (tens |ones );
lcdcommnd(0XC5);
lcddata(':');
////////////////sec/////////////////////
y = KEYGET();
tens = y & 0x0F;
tens = (tens << 4) & 0xF0;
lcdcommnd(0XC6);
lcddata(y); 
y = KEYGET();
ones = y & 0x0F;
lcdcommnd(0XC7);
lcddata(y);
sec = (tens |ones );
///////////////////////////////////////
MSDelay(20);
////////////////////////////////////////////
lcdcommnd(0x01); // Clear Command
MSDelay(20);
lcdcommnd(0x80);
lcd_string("DD:MM:YY");
////////////////date/////////////////////
y = KEYGET();
tens = y & 0x0F;
tens = (tens << 4) & 0xF0;
lcdcommnd(0XC0);
lcddata(y);
y = KEYGET();
ones = y & 0x0F;
lcdcommnd(0XC1);
lcddata(y);
date = (tens |ones );
lcdcommnd(0XC2);
lcddata(':');
//////////////////month/////////////////
y = KEYGET();
tens = y & 0x0F;
tens = (tens << 4) & 0xF0;
lcdcommnd(0XC3);
lcddata(y);
y = KEYGET();
ones = y & 0x0F;
lcdcommnd(0XC4);
lcddata(y);
month = (tens |ones );
lcdcommnd(0XC5);
lcddata(':');
///////////////////////////////////////
//////////////////year/////////////////
y = KEYGET();
tens = y & 0x0F;
tens = (tens << 4) & 0xF0;
lcdcommnd(0XC6);
lcddata(y);
y = KEYGET();
ones = y & 0x0F;
lcdcommnd(0XC7);
lcddata(y);
year = (tens |ones );
///////////////////////////////////////
/////////////////////////////////////////
date=dec_hex(date);
month=dec_hex(month);
year=dec_hex(year);
hour=dec_hex(hour);
min=dec_hex(min);
sec=dec_hex(sec);
DS1307_settime(hour,min,sec); /* Set Time (hh:mm:ss) 	  */
DS1307_setdate(date,month,year); /* Set Date (dd/mm/yy) */
MSDelay(20);
lcdcommnd(0x01); // Clear Command		
lcdcommnd(0x80);
lcd_string("Date:"); 
lcdcommnd(0xC0);
 lcd_string("Time:"); 
 sec = DS1307_get(0x00); 
min = DS1307_get(0x01); 
hour = DS1307_get(0x02);
date = DS1307_get(0x04);
month = DS1307_get(0x05);
year = DS1307_get(0x06);
sec=send2lcd(sec);
min=send2lcd(min);
hour=send2lcd(hour);
date=send2lcd(date);
month=send2lcd(month);
year=send2lcd(year);
 lcdcommnd(0x80);
lcd_string("Date:"); 
lcdcommnd(0xC0);
 lcd_string("Time:"); 
lcdcommnd(0x85); 
lcd_decimal2(date);
 lcdcommnd(0x87);
lcddata('/');
lcdcommnd(0x88); 
lcd_decimal2(month);
lcdcommnd(0x8A);
lcddata('/');
lcdcommnd(0x8B); 
lcd_decimal2(year); 
lcdcommnd(0xC5); 
lcd_decimal2(hour);
 lcdcommnd(0xC7);
lcddata('.');
lcdcommnd(0xC8);
lcd_decimal2(min); 
lcdcommnd(0xCA);
lcddata('.');
lcdcommnd(0xCB);
lcd_decimal2(sec);
}

void main()
{
 
unsigned char y,sec, min, hour, date, month, year;
lcd_init();
Rtc_Init();
lcdcommnd(0x80);
lcd_string("1->SET DATE&TIME"); 
lcdcommnd(0xC0);
lcd_string ("Any other-> SKIP");
COL0 = 1; // make cols input port
COL1 = 1;
COL2 = 1;
COL3 = 1;
y = KEYGET();
lcdcommnd(0x01); // Clear Command
MSDelay(20);
lcddata(y);
MSDelay(20);
if (y == '1')
{
  time_set();	
}

 lcdcommnd(0x80);
lcd_string("Date:"); 
lcdcommnd(0xC0);
 lcd_string("Time:"); 
while(1) 
{

/////////////////////////////rtc///////////////////////////////
 
sec = DS1307_get(0x00); 
min = DS1307_get(0x01); 
hour = DS1307_get(0x02);
date = DS1307_get(0x04);
month = DS1307_get(0x05);
year = DS1307_get(0x06);
sec=send2lcd(sec);
min=send2lcd(min);
hour=send2lcd(hour);
date=send2lcd(date);
month=send2lcd(month);
year=send2lcd(year);
 lcdcommnd(0x80);
lcd_string("Date:"); 
lcdcommnd(0xC0);
 lcd_string("Time:"); 
lcdcommnd(0x85); 
lcd_decimal2(date);
lcdcommnd(0x87);
lcddata('/');
lcdcommnd(0x88); 
lcd_decimal2(month);
 lcdcommnd(0x8A);
lcddata('/');
lcdcommnd(0x8B); 
lcd_decimal2(year); 
lcdcommnd(0xC5); 
lcd_decimal2(hour);
 lcdcommnd(0xC7);
lcddata('.');
lcdcommnd(0xC8);
lcd_decimal2(min); 
lcdcommnd(0xCA);
lcddata('.');
lcdcommnd(0xCB);
lcd_decimal2(sec);
MSDelay(100);
}
}
 

first in your code declare sec , min like variable globally because your time set() and main () function declare both variable.
 

hi go to the menu system -- > set Animation options --> change the frames per second and single step time..
 

hai jayanth,
this is the keil project file
View attachment clock.rar

hai nick
thank you

helo venkatesh
this is original values

frames per second -- 20
Timestep per Frame-- 50m
single step time -- 50m

increase or decrease the values???
 

so 20 * 50ms is 1sec for proteus changing the frame per second will change the Proteus time.... Try putting 26 in frame per second.....
 
  • Like
Reactions: MANO.5

    MANO.5

    Points: 2
    Helpful Answer Positive Rating
Who will post rtcds1307.h and Proteus file?

It is working fine. RTC runs independently. So, delay in code doesn't make RTC slow. File attached.
 

Attachments

  • clock.rar
    97.8 KB · Views: 86
  • rtc.rar
    94.7 KB · Views: 80
Last edited:
  • Like
Reactions: MANO.5

    MANO.5

    Points: 2
    Helpful Answer Positive Rating
so 20 * 50ms is 1sec for proteus changing the frame per second will change the Proteus time.... Try putting 26 in frame per second.....

you are correct venkadesh.when i change the frames per second value to 32 rtc runs faster than the pc clock.if it is 31 clock runs slower than pc clock.So i am going to make the hardware and test the program.After make the project i will update the status.
Thanks to jayanth and all guys for your support........
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top