#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define out P3
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
uchar date[]=" H.I.T.CHINA ",
time[]=" TIME 23:59:55 ";
uchar int_time,second,minute,hour;
uchar second=55,minute=59,hour=23;
void lcd_initial(void);
void check_busy(void);
void write_command(uchar com);
void write_data(uchar dat);
void write_sfm(uchar,uchar);
void delay(uint);
void clock_init(void);
void clock_write(uint,uint,uint);
void delay(uint j)
{
uchar i=250;
for(;j>0;j--)
{
while(--i);
i=249;
while(--i);
i=250;
}
}
void check_busy(void)
{
uchar dt;
do
{
dt=0xff;
E=0;
RS=0;
RW=1;
E=1;
dt=out;
}
while(dt&0x80);
E=0;
}
void write_command(uchar com)
{
check_busy();
E=0;
RS=0;
RW=0;
out=com;
E=1;
_nop_();
E=0;
delay(1);
}
void write_data(uchar dat)
{
check_busy();
E=0;
RS=1;
RW=0;
out=dat;
E=1;
_nop_();
E=0;
delay(1);
}
void lcd_initial(void)
{
write_command(0x38);
write_command(0x0c);
write_command(0x06);
write_command(0x01);
delay(1);
}
void write_sfm(uchar ad,uchar s)
{
write_command(ad);
write_data (s);
delay(100);
write_command(++ad);
write_data(s/10);
delay(100);
}
void clock_init()
{
uchar i,j;
write_command(0x80);
for(i=0;i<16;i++)
{
write_data(date[i]);
}
write_command(0x80+0x40);
for(j=0;j<16;j++)
{
write_data(time[j]);
}
}
void clock_write(uint s,uint m,uint h)
{
write_sfm(0xc7,h);
write_sfm(0xca,m);
write_sfm(0xcd,s);
}
void T0_interserve(void) interrupt 1 using 1
{
int_time++;
if(int_time==20)
{
int_time=0;
second++;
}
if(second==60)
{
second=0;
minute++;
}
if(minute==60)
{
minute=0;
hour++;
}
if(hour==24)
{
hour=0;
}
TH0=(65536-46083)/256;
TL0=(65536-46083)%256;
}
void main()
{
lcd_initial();
clock_init();
TMOD=0x01;
EA=1;
ET0=1;
TH0=(65536-46483)/256;
TL0=(65536-46483)/256;
TR0=1;
int_time=0;
second=55;
minute=59;
hour=23;
while(1)
{
clock_write(second,minute,hour);
}
}
|