MANO.5
Member level 1
- Joined
- Mar 15, 2013
- Messages
- 36
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 1,618
See if you can copy this and complete your project. https://saeedsolutions.blogspot.in/2012/11/interfacing-of-8051-with-ds1307-rtc.html
#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);
}
}
so 20 * 50ms is 1sec for proteus changing the frame per second will change the Proteus time.... Try putting 26 in frame per second.....
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?