#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]=" 2022-1-12 WED";
uchar code table1[]=" 00:00:00";
sbit dula=P2^6;
sbit wela=P2^7;
sbit lcden=P3^4; //液晶 LCDEN端
sbit lcdrs=P3^5; //液晶 RS端
sbit s1=P3^0; //定义按键-功能键
sbit s2=P3^1; //定义按键-增大键
sbit s3=P3^2; //定义按键-减小键
sbit rd=P3^7;
uchar count,s1num;
char shi,fen,miao;
void delay (uint z) //延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) //液晶写命令函数
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date) //液晶写数据函数
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void inti() //初始化函数
{
uchar sum;
// shi=23;
// fen=59;
// miao=53;
dula=0;
wela=0;
lcden=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80); //液晶第一行的地址
for(sum=0;sum<15;sum++)
{
write_date(table[sum]); //将字符写入液晶
delay(5);
}
write_com(0x80+0x40); //液晶第二行的地址
for(sum=0;sum<12;sum++)
{
write_date(table1[sum]);
delay(5);
}
}
void write_sfm(uchar add,uchar date) //液晶扫描函数
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add); //地址
write_date(0x30+shi); //个位的数据
write_date(0x30+ge);
}
void key() //键盘检测函数
{
rd=0;
if(s1==0)
{
delay(5); //防抖
if(s1==0) //防误触
{
s1num++;
while(!s1);
switch(s1num)
{
case 1: //第一次按下功能键
TR0=0; //定时器关闭
write_com(0x80+0x40+11); //光标指向秒的个位
write_com(0x0f); //打开光标
break;
case 2:
TR0=0;
write_com(0x80+0x40+8);
write_com(0x0f);
break;
case 3:
TR0=0;
write_com(0x80+0x40+5);
write_com(0x0f);
break;
case 4:
s1num=0;
write_com(0x0c); //关闭光标
TR0=1; //定时器开始
break;
}
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
switch(s1num)
{
case 1:
miao++;
if(miao==60)
{
miao=0;
}
write_sfm(10,miao); //秒写入液晶
write_com(0x80+0x40+11);
break;
case 2:
fen++;
if(fen==60)
{
fen=0;
}
write_sfm(7,fen);
write_com(0x80+0x40+8);
break;
case 3:
shi++;
if(shi==24)
{
miao=0;
}
write_sfm(4,shi);
write_com(0x80+0x40+5);
break;
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
switch(s1num)
{
case 1:
miao--;
if(miao==-1)
{
miao=59;
}
write_sfm(10,miao);
write_com(0x80+0x40+11);
break;
case 2:
fen--;
if(fen==-1)
{
fen=59;
}
write_sfm(7,fen);
write_com(0x80+0x40+8);
break;
case 3:
shi--;
if(shi==-1)
{
shi=23;
}
write_sfm(4,shi);
write_com(0x80+0x40+5);
break;
}
}
}
}
}
void main()
{
inti();
while(1)
{
key();
if(count==20)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}write_sfm(4,shi);
}write_sfm(7,fen);
}write_sfm(10,miao);
}
}
}
void time_0() interrupt 1 //定时器中断函数
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
}
|