#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 char char_a[]={0x00,0x04,0x04,0x04,0x04,0x0a,0x11,0x00};//¤µ¤
unsigned char b[]={0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10};//¤Ó
unsigned char char_c[]={0x00,0x1f,0x01,0x02,0x04,0x08,0x10,0x00};//¤¡
unsigned char d[]={0x0a,0x0a,0x1a,0x0a,0x0a,0x1a,0x0a,0x0a};//¤Æ
unsigned char e[]={0x00,0x0a,0x15,0x11,0x11,0x0a,0x04,0x00};
unsigned char f[]={0x00,0x04,0x04,0x04,0x04,0x0a,0x11,0x00};
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 write_cg(char a)
{
int i;
LCD_RS=1;
LCD_RW=0;
LCD_E=1;
LCD_DATA=a;
LCD_E=0;
for(i=0;i<100;i++);
}
void write_cmd(char cmd)
{
LCD_RS=0;
LCD_RW=0;
LCD_DATA=cmd;
E_Pulse();
}

void cg_ad(char x, char y)
{
if(y==0) write_cmd(x+0x41);
else if (y==1) write_cmd(x+0x61);
}


void main(void)
{
	char i,c;  
	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)
	{
		cg_ad(0,0);
		for(i=0;i<9;i++)
		{
			c=char_a[i];
			write_cg(c);}
			cg_ad(8,0);
			
			for(i=0;i<9;i++)
			{
				c=b[i];
				write_cg(c);
			}
			
			cg_ad(0x10,0);
			
			for(i=0;i<9;i++)
			{
				c=char_c[i];
				write_cg(c);
			}
			
			cg_ad(0x18,0);
			
			for(i=0;i<9;i++)
			{
				c=d[i];
				write_cg(c);
			}
			
			cg_ad(0,1);
			
			for(i=0;i<9;i++)
			{
				c=e[i];
				write_cg(c);
			}
			
			cg_ad(8,1);
			
			for(i=0;i<9;i++)
			{
				c=f[i];
				write_cg(c);
			}

			lcd_disp(1,0);
			write_char(4);
			
			lcd_disp(2,0);
			write_char(4);
			
			write_lcd(3,0,"LCD");
			
			lcd_disp(7,0);
			write_char(5);
			
			lcd_disp(8,0);
			write_char(1);
			
			lcd_disp(9,0);
			write_char(2);
			
			lcd_disp(10,0);
			write_char(3);
			
			lcd_disp(11,0);
			write_char(4);
			
			lcd_disp(12,0);
			write_char(4);
			
			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>9999)
{
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++;
}
}
}
}
}
}
}
