#include
sbit rs=P3^5;
sbit rw=P3^6;
sbit en=P3^7;
sbit K2=P3^3;
sbit K3=P3^4;
sbit K1=P3^2;
static int temp1=0;
unsigned char second;
unsigned char minite;
unsigned char hour;
unsigned char day=14;
unsigned char month=11;
unsigned char year=9;
unsigned int temp0;
unsigned char tab[12]={0,0,0,0,0,0,4,1,1,1,9,0};
unsigned char tab1[]={"DATE:--"};
unsigned char tab2[]={"TIME:::"};
//=======延时函数===========
void Delay_ms(delay)
{unsigned char i,j;
for(i=0;i<20;i++)
for(j=0;j<50;j++)
for(;delay>0;delay--);
}
//=====lcd1602写指令函数======
void write_command(unsigned char wc)
{
rs=0;
rw=0;
P1=wc;
en=1;Delay_ms(10);
en=0;
}
//=====lcd1602写数据函数========
void write_data(unsigned char wd)
{
rs=0;
rs=1;
P1=wd;
en=1;Delay_ms(10);
en=0;
}
//=====lcd1602显示一个字符====
void disp_char(unsigned char x,unsigned char y,unsigned char _char)
{
y&=0x01;
x&=0x0f;
if(y==1)x|=0x40;
x|=0x80;
write_command(x);
write_data(_char);
}
//====lcd1602显示一列字符串========
void disp_str(unsigned char x,unsigned char y,unsigned char *str)
{ unsigned char i;
for(i=0;i<5;i++)
{ disp_char(x,y,*str); //????
x++;
str++;
}
for(i=0;i<2;i++)
{
x+=3;
disp_char(x,y,*str);
str++;
}
}
//=====lcd1602初始化函数=======
void init()
{
write_command(0x38);
write_command(0x0c);
write_command(0x06);
write_command(0x01);
}
//=====外中断服务程序 =======
void int0()interrupt 0
{
Delay_ms(2);
if(K1==0)
{ Delay_ms(10);
if(K1==0) //K1触发 模式切换
{
temp1++;
if(temp1==6)
temp1=0;
}
while(K1==0);
}
}
//=======定时中断服务程序========
void t0() interrupt 1 using 0
{
temp0++;
if(temp0==4000)
{ temp0=0;
second++;
/*if(second==60)
{
second=0;
minite++;
if(minite==60)
{
minite=0;
hour++;
if(hour==24)
{
hour=0;
day++;
if(day==31)
{
day=1;
month++;
if(month==13)
{
month=1;
year++;
if(year==100)
year=0;
}
}
}
}
} */
}
}
//========主函数=============
void main()
{
init();
disp_str(0,0,tab1);
disp_str(0,1,tab2);
TMOD=0X02;
TL0=0X06;
TH0=0X06;
IT0=0;//电平触发方式,低电平有效
PX0=1;//优先级设定
ET0=1;//定时中断允许
EX0=1;//外中断允许
EA=1; //总段允许
TR0=1;//启动定时器
while(1)
{ disp_char(15,0,temp1+'0');
if(second==60)
{second=0;minite++;}
if(minite==60)
{minite=0;hour++;}
if(hour==24)
{hour=0;day++;}
if(day==31)
{day=1;month++;}
if(month==13)
{month=1;year++;}
if(year==100)
{year=0;}
tab[0]=second%10;
tab[1]=second/10;
tab[2]=minite%10;
tab[3]=minite/10;
tab[4]=hour%10;
tab[5]=hour/10;
tab[6]=day%10;
tab[7]=day/10;
tab[8]=month%10;
tab[9]=month/10;
tab[10]=year%10;
tab[11]=year/10;
disp_char(13,1,tab[0]+'0');
disp_char(12,1,tab[1]+'0');
disp_char(10,1,tab[2]+'0');
disp_char(9,1,tab[3]+'0');
disp_char(7,1,tab[4]+'0');
disp_char(6,1,tab[5]+'0');
disp_char(13,0,tab[6]+'0');
disp_char(12,0,tab[7]+'0');
disp_char(10,0,tab[8]+'0');
disp_char(9,0,tab[9]+'0');
disp_char(7,0,tab[10]+'0');
disp_char(6,0,tab[11]+'0');
switch(temp1)
{
case 1: if(K2==0&K3==1)
{minite++;while(K2==0);}
else if(K2==1&K3==0)
{ minite--;while(K3==0);}break;
case 2: if(K2==0&K3==1)
{hour++;while(K2==0);}
else if(K2==1&K3==0)
{hour--;while(K3==0);}break;
case 3: if(K2==0&K3==1)
{day++;while(K2==0);}
else if(K2==1&K3==0)
{day--;while(K3==0);}break;
case 4: if(K2==0&K3==1)
{month++;while(K2==0);}
else if(K2==1&K3==0)
{month--;while(K3==0);}break;
case 5: if(K2==0&K3==1)
{year++;while(K2==0);}
else if(K2==1&K3==0)
{year--;while(K3==0);}break;
}
}