You are on page 1of 14

#include<reg51.

h>
void _nop_(void);
#define delay_us _nop_(); //generates 1 microsecond
#define SCKHIGH SCK=1;
#define SCKLOW SCK=0;
#define SDAHIGH SDA=1;
#define SDALOW SDA=0;
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit

EDGE=TCON^0;
ir=P3^3;
st=P3^2;
de=P3^5;
ch=P3^4;
d1=P2^3; //connect p2.0 to date pin of lcd
d2=P2^4; //connect p2.1 to date pin of lcd
m1=P2^5; //connect p2.2 to month pin of lcd
m2=P2^6; //connect p2.3 to month pin of lcd
y1=P2^7; //connect p2.4 to year pin of lcd
y2=P0^6; //connect p2.5 to year pin of lcd
y3=P2^1; //connect p2.6 to year pin of lcd
y4=P2^0; //connect p2.7 to year pin of lcd
h1=P0^5; //connect p1.0 to hour pin of lcd
h2=P0^4; //connect p1.1 to hour pin of lcd
mi1=P0^3; //connect p1.2 to min pin of lcd
mi2=P0^2; //connect p1.3 to min pin of lcd
s1=P0^1; //connect p1.4 to sec pin of lcd
s2=P0^0; //connect p1.5 to sec pin of lcd
dy=P2^2; //connect p1.6 to day pin of lcd

sbit SCK=P3^6; //serial clock pin at p1.0


sbit SDA=P3^7; //serial data pin at p1.1
void delay_ms(unsigned int);
void delay_u10()
{
unsigned int u;
for(u=0;u<=400;u++);
}
void start(void);
void stop(void);
//void convert();
void send_byte(unsigned char);
unsigned char receive_byte(unsigned char);
void write_i2c(unsigned char,unsigned char,unsigned char);
unsigned char read_i2c(unsigned char,unsigned char);
void write_lcd(unsigned int);
//clock[]={seconds,minutes,hours,day_of_week,date,month,year};
unsigned int clock[]={0x00,0x00,0x09,0x07,0x01,0x01,0x00};
//unsigned char s[]={"SUN","MON","TUE","WED","THU","FRI","SAT"};
unsigned char slave_ack,add=0,d,e,f,k,h,i,j,l,y,z,m=2,n=0,o,q,r,s,t;
unsigned int num,b=0;
unsigned int c[5];
unsigned int digi_val[10]={0x10,0xbb,0x0c,0x09,0xa3,0xc1,0xc0,0x9b,0x80,0x81};
unsigned int digi_val1[8]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf};
unsigned int a_p[2]={0x82,0x86};
unsigned int w=0,p=0,x=0 ;

// Real Time Clock Mode


void set() interrupt 0
{
unsigned int temp_s,temp_m,temp_h,temp_d,temp_mo,temp_y,temp_da,z,lp,da_30,da_31
;
temp_s=temp_m=temp_h=temp_da=temp_y=z=lp=da_30=da_31=0;
temp_d=temp_mo=1;
while(z<=7)
{
if(ch==0)
{
delay_ms(10);
if(ch==1)
{z=z+1;}
}
if(de==0)
{
delay_ms(10);
if(de==1)
{
switch(z)
{
case 1: temp_s-=1;
if(temp_s==(-1))
{temp_s=59;}
clock[0]=16*(temp_s/10)+(temp_s%10);
d=clock[0];
b=0;
write_lcd(d);
delay_ms(10);
d=clock[0];
b=1;
write_lcd(d);
break;
case 2: temp_m-=1;
if(temp_m==(-1))
{temp_m=59;}
clock[1]=16*(temp_m/10)+(temp_m%10);
b=2;
e=clock[1];
write_lcd(e);
b=3;
delay_ms(10);
e=clock[1];
write_lcd(e);
break;
case 3:temp_h-=1;
if(temp_h==00)
{temp_h=24;}
clock[2]=16*(temp_h/10)+(temp_h%10);

b=4;
y=((f/16)*10+(f%16)) ;
if(y>=13)
{y=y-12;z=1;}
f=((y/10)*16+y%10);
write_lcd(f);
delay_ms(10);
b=5;
f=clock[2];
y=((f/16)*10+(f%16)) ;
if(y>=13)
{y=y-12;z=1;}
f=((y/10)*16+y%10);
write_lcd(f);
break;
case 4: temp_y-=1;
if(temp_y==(-1))
{temp_y=99;}
if(temp_y%4==0)
{lp=1;}
clock[6]=16*(temp_y/10)+(temp_y%10);
b=11;
delay_ms(10);
write_lcd(m);
b=12;
delay_ms(10);
write_lcd(n);
j=clock[6];
b=13;
delay_ms(10);
write_lcd(j);
j=clock[6];
b=14;
delay_ms(10);
write_lcd(j);
break;
case 5:temp_mo-=1;
if(temp_mo==0)
{temp_mo=12;}
if(temp_mo==1 || temp_mo==5 || temp_mo==3 || temp_mo==7 || temp_mo==8 || temp_mo
==10 || temp_mo==12 )
{da_31=1;}
if(temp_mo==4 || temp_mo==6 || temp_mo==9 || temp_mo==11)
{da_30=1;}
clock[5]=16*(temp_mo/10)+(temp_mo%10);
b=9;
i=clock[5];
delay_ms(10);
write_lcd(i);
b=10;
i=clock[5];

delay_ms(10);
write_lcd(i);
break;
case 6:temp_d-=1;
if(temp_d==(0) && da_30==1)
{temp_d=30;}
if(temp_d==(0) && da_31==1)
{temp_d=31;}
if(temp_d==(0) && temp_mo==2 && lp==1)
{temp_d=29;}
if(temp_d==(0) && temp_mo==2 && lp==0)
{temp_d=28;}
clock[4]=16*(temp_d/10)+(temp_d%10);
b=7;
h=clock[4];
delay_ms(10);
write_lcd(h);
b=8;
h=clock[4];
delay_ms(10);
write_lcd(h);
break;
/* case 7 :temp_da=read_i2c(0xd0,0x03);
clock[3]=temp_da;
b=6;
l=clock[3];
delay_ms(10);
write_lcd(l);
break; */
case 7:temp_da-=1;
if(temp_da==(-1))
{temp_da=7;}
clock[3]=temp_da;
b=6;
l=clock[3];
delay_ms(10);
write_lcd(l);
break;
}
}
}
if(ir==0)
{
delay_ms(10);
if(ir==1)
{
switch(z)
{

case 1: temp_s+=1;
if(temp_s==60)
{temp_s=0;}
clock[0]=16*(temp_s/10)+(temp_s%10);
d=clock[0];
b=0;
write_lcd(d);
delay_ms(10);
d=clock[0];
b=1;
write_lcd(d);
break;
case 2: temp_m+=1;
if(temp_m==60)
{temp_m=0;}
clock[1]=16*(temp_m/10)+(temp_m%10);
b=2;
e=clock[1];
write_lcd(e);
b=3;
delay_ms(10);
e=clock[1];
write_lcd(e);
break;
case 3:temp_h+=1;
if(temp_h==24)
{temp_h=0;}
clock[2]=16*(temp_h/10)+(temp_h%10);
b=4;
f=clock[2];
delay_ms(10);
y=((f/16)*10+(f%16)) ;
if(y>=13)
{y=y-12;z=1;}
f=((y/10)*16+y%10);
write_lcd(f);
delay_ms(10);
b=5;
f=clock[2];
y=((f/16)*10+(f%16)) ;
if(y>=13)
{y=y-12;z=1;}
f=((y/10)*16+y%10);
write_lcd(f);
break;
case 4: temp_y+=1;
if(temp_y==100)
{temp_y=0;}
if(temp_y%4==0)
{lp=1;}
clock[6]=16*(temp_y/10)+(temp_y%10);
b=11;
delay_ms(10);
write_lcd(m);

b=12;
delay_ms(10);
write_lcd(n);
j=clock[6];
b=13;
delay_ms(10);
write_lcd(j);
j=clock[6];
b=14;
delay_ms(10);
write_lcd(j);
break;
case 5:temp_mo+=1;
if(temp_mo==13)
{temp_mo=1;}
if(temp_mo==1 || temp_mo==5 || temp_mo==3 || temp_mo==7 || temp_mo==8 || temp_mo
==10 || temp_mo==12 )
{da_31=1;}
if(temp_mo==4 || temp_mo==6 || temp_mo==9 || temp_mo==11)
{da_30=1;}
clock[5]=16*(temp_mo/10)+(temp_mo%10);
b=9;
i=clock[5];
delay_ms(10);
write_lcd(i);
b=10;
i=clock[5];
delay_ms(10);
write_lcd(i);
break;
case 6:temp_d+=1;
if(temp_d==31 && da_30==1)
{temp_d=1;}
if(temp_d==32 && da_31==1)
{temp_d=1;}
if(temp_d==30 && temp_mo==2 && lp==1)
{temp_d=1;}
if(temp_d==29 && temp_mo==2 && lp==0)
{temp_d=1;}
clock[4]=16*(temp_d/10)+(temp_d%10);
b=7;
h=clock[4];
delay_ms(10);
write_lcd(h);
b=8;
h=clock[4];
delay_ms(10);
write_lcd(h);
break;

/* case 7 :temp_da=read_i2c(0xd0,0x03);
clock[3]=temp_da;
b=6;
l=clock[3];
delay_ms(10);
write_lcd(l);
break; */
case 7:temp_da+=1;
if(temp_da==7)
{temp_da=0;}
clock[3]=temp_da;
b=6;
l=clock[3];
delay_ms(10);
write_lcd(l);
break;
}
}
}
}
add=0;
while(add<=6) //update real time clock
{
write_i2c(0xd0,add,clock[add]);
add++;
}
}
void init_lcd(void)
{
s1=0;s2=0;mi1=0;mi2=0;h1=0;h2=0;m1=0;m2=0;d1=0;d2=0;y1=0;y2=0;y3=0;y4=0;m1=0;m2=
0;dy=0;
}
void main(void)
{
IE=0x81;
EDGE=1;
st=1;ir=1;ch=1,de=1;
init_lcd();
while(add<=6) //update real time clock
{
write_i2c(0xd0,add,clock[add]);
add++;
}
while(1)
{
b=0;
d=read_i2c(0xd0,0x00);//read hours register and display on LCD
write_lcd(d);

b=1;
d=read_i2c(0xd0,0x00);//read hours register and display on LCD
write_lcd(d);
b=2;
e=read_i2c(0xd0,0x01);//read minutes register and display on LCD
write_lcd(e);
b=3;
e=read_i2c(0xd0,0x01);//read minutes register and display on LCD
write_lcd(e);
b=4;
f=read_i2c(0xd0,0x02);//read seconds register and display on LCD
{y=((f/16)*10+(f%16)) ;
if(y>=13)
{y=y-12;z=1;}
else
{z=0;
}}
f=((y/10)*16+y%10);
write_lcd(f);
b=5;
f=read_i2c(0xd0,0x02);//read seconds register and display on LCD
y=((f/16)*10+(f%16)) ;
if(y>=13)
{y=y-12;z=1;}
f=((y/10)*16+y%10);
write_lcd(f);
b=6;
l=read_i2c(0xd0,0x03);//read day register and display
write_lcd(l);
b=7;
h=read_i2c(0xd0,0x04);//read date register and display on LCD
// h=h/2;
write_lcd(h);
b=8;
h=read_i2c(0xd0,0x04);//read date register and display on LCD
// h=h/2;
write_lcd(h);
b=9;
i=read_i2c(0xd0,0x05);//read month register and display on LCD
write_lcd(i);
b=10;
i=read_i2c(0xd0,0x05);//read month register and display on LCD
write_lcd(i);
b=11;
j=read_i2c(0xd0,0x06);//read year register and display on LCD
write_lcd(m);
b=12;
j=read_i2c(0xd0,0x06);//read year register and display on LCD

write_lcd(n);
b=13;
j=read_i2c(0xd0,0x06);//read year register and display on LCD
write_lcd(j);
b=14;
j=read_i2c(0xd0,0x06);//read year register and display on LCD
write_lcd(j);
}
}
void write_lcd(unsigned int d)
{
s1=0;s2=0;mi1=0;mi2=0;h1=0;h2=0;m1=0;m2=0;d1=0;d2=0;y1=0;y2=0;y3=0;y4=0;m1=0;m2=
0;dy=0;
switch(b)
{
case 0: P1=digi_val[d/16]; //d/16
s1=1;
delay_u10();
//b=1;
break;
case 1: P1=digi_val[d%16]; //d%16
s2=1;
delay_u10();
//b=2;
break;
case 2: P1=digi_val[e/16]; //e/16
mi1=1;
delay_u10();
//b=3;
break;
case 3: P1=digi_val[e%16]; //e%16
mi2=1;
delay_u10();
//b=4;
break;
case 4: P1=digi_val[f/16]; //f/16
h1=1;
delay_u10();
//b=5;
break;
case 5: P1=digi_val[f%16]; //f%16
h2=1;
delay_u10();
//b=6;
break;
case 6: P1=digi_val[l]; //l
dy=1;
delay_u10();
//b=7;
break;
case 7: P1=digi_val[h/16]; //h/16
d1=1;
delay_u10();
//b=8;

break;
case 8: P1=digi_val[h%16];
d2=1;
delay_u10();
//b=9;
break;
case 9: P1=digi_val[i/16];
m1=1;
delay_u10();
//b=10;
break;
case 10:P1=digi_val[i%16];
m2=1;
delay_u10();
//b=11;
break;
case 11:if(z==0)
{P1=a_p[0];}
else
P1=a_p[1];;
y1=1;
delay_u10();
//b=12;
break;
case 12:P1=digi_val[0];
y2=1;
delay_u10();
//b=13;
break;
case 13:P1=digi_val[j/16];
y3=1;
delay_u10();
//b=14;
break;
case 14:P1=digi_val[j%16];
y4=1;
delay_u10();
//b=0;
break;
}
}
void start(void)
{
if(SCK==0) //check SCK. if SCK busy, return else SCK idle
return;
if(SDA==0) //check SDA. if SDA busy, return else SDA idle
return;
SDALOW //data low
delay_us
SCKLOW //clock low

delay_us
}

//stops i2c, releasing the bus


void stop(void)
{
SDALOW //data low
SCKHIGH //clock high
delay_us
SDAHIGH //data high
delay_us
}

//transmits one byte of data to i2c bus


void send_byte(unsigned char c)
{
unsigned mask=0x80;
do //transmits 8 bits
{
if(c&mask) //set data line accordingly(0 or 1)
SDAHIGH //data high
else
SDALOW //data low
SCKHIGH //clock high
delay_us
SCKLOW //clock low
delay_us
mask/=2; //shift mask
}while(mask>0);
SDAHIGH //release data line for acknowledge

SCKHIGH //send clock for acknowledge


delay_us
slave_ack=SDA; //read data pin for acknowledge
SCKLOW //clock low
delay_us
}

//receives one byte of data from i2c bus


unsigned char receive_byte(unsigned char master_ack)
{
unsigned char c=0,mask=0x80;
do //receive 8 bits
{
SCKHIGH //clock high
delay_us
if(SDA==1) //read data
c|=mask; //store data
SCKLOW //clock low
delay_us
mask/=2; //shift mask
}while(mask>0);
if(master_ack==1)
SDAHIGH //don't acknowledge
else
SDALOW //acknowledge
SCKHIGH //clock high
delay_us
SCKLOW //clock low
delay_us
SDAHIGH //data high

return c;
}
//writes one byte of data(c) to slave device(device_id) at given address(locatio
n)
void write_i2c(unsigned char device_id,unsigned char location,unsigned char c)
{
do
{
start(); //starts i2c bus
send_byte(device_id); //select slave device
if(slave_ack==1) //if acknowledge not received, stop i2c bus
stop();
}while(slave_ack==1); //loop until acknowledge is received
send_byte(location); //send address location
send_byte(c); //send data to i2c bus
stop(); //stop i2c bus
delay_ms(4);
}

//reads one byte of data(c) from slave device(device_id) at given address(locati


on)
unsigned char read_i2c(unsigned char device_id,unsigned char location)
{
unsigned char c;
do
{
start(); //starts i2c bus
send_byte(device_id); //select slave device
if(slave_ack==1) //if acknowledge not received, stop i2c bus
stop();
}while(slave_ack==1); //loop until acknowledge is received

send_byte(location); //send address location


stop(); //stop i2c bus
start(); //starts i2c bus
send_byte(device_id+1); //select slave device in read mode
c=receive_byte(1); //receive data from i2c bus
stop(); //stop i2c bus
return c;
}
void delay_ms(unsigned int i)
{
unsigned int j;
while(i-->0)
{
for(j=0;j<500;j++)
{
;
}
}
}