/*=========================1602显示时钟==============*/
/*============三个独立按键设置时间==========*/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
char shi1,shi2,fen1,fen2,miao1,miao2,aa,bb,shi,fen,miao,count;
uchar a[]="0123456789";
sbit wlen=P2^7;
sbit wlrs=P2^5;
sbit RW=P2^6;
sbit EN=P3^2; //==========独立按键1选择设置
sbit jia=P3^3;//==========独立按键2调时间加
sbit jian=P3^4; //=========独立按键3调时间减
void delay(uchar z); //======延时函数
void write_com(uchar com); //====1602写命令函数
void write_date(uchar date); //===1602写数据函数
void init(); //=========初始化1602
void write();//=========将时,分,秒送显示
void do_date();//========对时,分,秒进行分解
void main()
{
init();
TMOD = 0x01;
IE = 0x82;
TH0 = (65536-46296)/256; //====计时50ms
TL0 = (65536-46296)%256;
TR0 = 1;
while(1)
{
write_com(0x80+0x46);
delay(2);
write_date(':');
write_com(0x80+0x49);
delay(2);
write_date(':');
if(EN==0) //=====对独立按键进行检测并用软件消抖
{
delay(5);
if(EN==0)
{
while(EN==0);
TR0=0;
count++; //=====count计数选择对时,分,秒调动
if(count==4)//====设置结束,时钟跑
{
count=0;
TR0=1;
}
}
}
//==================调秒
if(count==1&&jia==0)
{
delay(5);
if(count==1&&jia==0)
{
while(jia==0);
miao++;
if(miao==60)
{miao=0;}
}
}
if(count==1&&jian==0)
{
delay(5);
if(count==1&&jian==0)
{
while(jian==0);
miao--;
if(miao+1==0)
{miao=59;}
}
}
//==================调分
if(count==2&&jia==0)
{
delay(5);
if(count==2&&jia==0)
{
while(jia==0);
fen++;
if(fen==60)
{fen=0;}
}
}
if(count==2&&jian==0)
{
delay(5);
if(count==2&&jian==0)
{
while(jian==0);
fen--;
if(fen+1==0)
{fen=59;}
}
}
//==================调时
if(count==3&&jia==0)
{
delay(5);
if(count==3&&jia==0)
{
while(jia==0);
shi++;
if(shi==24)
{shi=0;}
}
}
if(count==3&&jian==0)
{
delay(5);
if(count==3&&jian==0)
{
while(jian==0);
shi--;
if(shi+1==0)
{shi=23;}
}
}
do_date();
write();
}
}
/*......................................延时程序,延时1ms................*/
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*......................................1602命令函数.....................*/
void write_com(uchar com)
{
wlrs=0; //写命令0/写数据1
RW=0; //读0/写1
P0=com; //给1602送命令
delay(5);
wlen=1;
delay(5);
wlen=0;
}
/*......................................1602数据函数.....................*/
void write_date(uchar date)
{
wlrs=1;
RW=0;
P0=date;
delay(5);
wlen=1;
delay(5);
wlen=0;
}
void init()
{
wlen=0;
write_com(0x38);
write_com(0x01);
write_com(0x0c);
write_com(0x04);
}
void write()
{
write_com(0x80+0x44);
write_date(a[shi2]);
write_com(0x80+0x45);
write_date(a[shi1]);
write_com(0x80+0x47);
write_date(a[fen2]);
write_com(0x80+0x48);
write_date(a[fen1]);
write_com(0x80+0x4a);
write_date(a[miao2]);
write_com(0x80+0x4b);
write_date(a[miao1]);
}
void timer0() interrupt 1
{
TH0=(65536-46296)/256;
TL0=(65536-46296)%256;
aa++;
if(aa==20)
{
aa=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
}
if(fen==60)
{
fen=0;
shi++;
}
if(shi==24)
{shi=0;}
}
}
//===========对时,分,秒进行分解
void do_date()
{
shi2=shi/10;
shi1=shi%10;
fen2=fen/10;
fen1=fen%10;
miao2=miao/10;
miao1=miao%10;
} |