#include <io51.h>
bit P1_0    = 0x90;
bit P1_1    = 0x91;
bit P1_2    = 0x92;

bit P2_0    = 0xA0;


#define  LCD_RS    P1_0
#define  LCD_RW    P1_1
#define  LCD_E     P1_2
#define  LCD_DATA P0
#define  LED       P2_0

unsigned int h;  
unsigned int count0=0,count=0;

unsigned char sec,hour,min,sec1,min1,hour1; 

 void E_Pulse(void)
 {
 char i ; 
	LCD_E = 1 ;	        
	for (i=0; i< 10 ; i++) ;
	LCD_E = 0 ; 
    }
void Func_set(void)
 {
 LCD_RW = 0 ;
 LCD_RS = 0 ;
 LCD_DATA = 0x38 ;
E_Pulse() ;
  }

 void Init_Lcd(void)
 {
  int i ;
  LCD_E = 0 ;
  for ( i=0 ; i<2000 ; i++); 
  Func_set() ;
   for ( i=0 ; i<1000 ; i++);  
  Func_set() ;
    for ( i=0 ; i<200 ; i++) ; 
  Func_set() ;
   LCD_DATA = 0x0f;
   E_Pulse() ;
   LCD_DATA = 0x06 ;
  E_Pulse() ;
  }
  
void write_char(char s)
{
LCD_RS=1;
LCD_DATA=s;
E_Pulse();
}

void clrscr(void)
{
int i ;
	
   LCD_RS = 0 ;
   LCD_RW = 0 ;
   LCD_DATA = 0x01 ;
   E_Pulse() ;
    for (i = 0 ; i < 600 ; i++ ) ;
  } 

void lcd_disp ( char x, char y )
{
  LCD_RS = 0 ;
  LCD_RW = 0 ;
  
 if (y==0) LCD_DATA = x +0x80 ;
 else if (y==1) LCD_DATA = x + 0xc0 ;
   E_Pulse() ;
}

void write_lcd(char x, char y, char *str )
{
     lcd_disp(x, y) ;
          while(*str)
          write_char(*str++) ;
}


 void ascii_char(unsigned int k)
{
unsigned char i;
for(i=0;i<1;i++)
{
write_char((k%10)+0x30);
k/=10;
}
}

 void main(void)
  {
count=0, sec=0, hour1=1,hour=2, min=0,sec1=0, min1=0,h=0;
Init_Lcd() ;
clrscr();
TCON=0x00;
TMOD=0x00;
TH0=0x19;
TL0=0x13;
TR0=1;
EA=1;
EX0=1;
ET0=1;
while(1){
write_lcd(1,0,"DIGITAL CLOCK");
lcd_disp(11,1);
ascii_char(sec);
lcd_disp(10,1);
ascii_char(sec1);
write_lcd(9,1,":");
lcd_disp(8,1);
ascii_char(min);
lcd_disp(7,1);
ascii_char(min1);
write_lcd(6,1,":");
lcd_disp(5,1);
ascii_char(hour);
lcd_disp(4,1);
ascii_char(hour1);
if(sec==0 |sec==2 |sec==4 |sec==6 |sec==8)
LED=0;
else
LED=1;


if(hour1>0 && hour>2)
{
hour1=0;
hour=1;

}

if(h==0 |h==2 |h==4 |h==6 |h==8)
write_lcd(1,1,"AM");
else
write_lcd(1,1,"PM");
}


}

interrupt[0x03] void EX0_int (void)
{
TH0=0x19;
TL0=0x13;

count0++;


if(count0>8999)
{
if(hour1==1 && hour==1 && min1==5 && min==9 && sec1==0 && sec==0)
{
 h++;
 }

 if(h>9)
{
 h=0;
}
count0=0;
sec=0;
sec1=0;
min=min+1;
while(min>9)
{

min=0;
min1++;
while(min1>5)
{
min1=0;
hour++;
while(hour>9)
{
hour=0;
hour1++;

}
}
}
}

}

interrupt[0x0b] void T0_int(void)
{
TH0=0x19;
TL0=0x13;

count++;
if(count>249)
{
if(hour1==1 && hour==1 && min1==5 && min==9 && sec1==5 && sec==9)
{
 h++;
 }

 if(h>9)
{
 h=0;
}
count=0;
sec++;
while(sec>9)
{
sec=0;
sec1++;
while(sec1>5)
{
sec1=0;
min++;
while(min>9 )
{
min=0;
min1++;
while(min1>5)
{
min1=0;
hour++;
while(hour>9)
{
hour=0;
hour1++;


}
}
}
}
}
}

}

