//液晶屏显示的时钟,可以通过键盘进行控制的#include<reg52.h>#define uint unsigned int#define uchar unsigned char
sbit s1=P2^0;sbit s2=P2^1;sbit s3=P2^2;sbit rd=P3^3;sbit rs=P1^0;sbit rw=P1^1;sbit lcden=P1^2;sbit psb=P1^3;
uchar count,s1num;uchar code table[]="2012--07--04";uchar code table1[]="23: 59: 50";
uchar count,shi,fen,miao,s1num,xiaoshi,day,mouth,year;void delay(uint z){
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);}
void write_com(uchar com)
//写指令{
rs=0;
rw=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;}
void write_dat(uchar dat)
//写数据{
rs=1;
lcden=0;
rw=0;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;}
void init()
//初始化{
uchar num; psb=1;
lcden=0;
xiaoshi=23;
fen=59;
miao=55;
year=2012;
mouth=7;
day=4;
write_com(0x30);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<13;num++)
{
write_dat(table[num]);
delay(5);
}
write_com(0x88);
for(num=0;num<10;num++)
{
write_dat(table1[num]);
delay(5);
}
TMOD=0x01;
//开启中断
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;}
void write_sfm(uchar add, uchar dat)
//写时分秒函数{
uchar shi,ge;
shi=dat/10;
ge=dat%10;
write_com(0x88+add);
write_dat(shi+0x30);
write_dat(ge+0x30);}
void keyscan()
//键盘扫描{
s1num=0;
//功能键程序
if(s1==0)
{
delay(5);
if(s1==0)
{
if(s1==0);
delay(5);
s1num=s1num+1;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x88+4);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x88+2);
}
if(s1num==3)
{
write_com(0x88);
}
if(s1num==4)
{
s1num=0;
write_com(0x0c);
TR0=1;
}
}
}
//###########################################
if(s1num!=0)
//加数程序
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
{
miao=0;
}
write_sfm(4,miao);
write_com(0x88+4);
//+0x40+10
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(2,fen);
write_com(0x88+2);
//
+0x40+7
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,xiaoshi);
write_com(0x88);
//
+0x40+4
}
}
}
//##########################################
if(s3==0)
//减数程序
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(4,miao);
write_com(0x88);
//+0x40+10
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(2,fen);
write_com(0x88+2);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(0,shi);
write_com(0x88);
}
}
}
}}
void main()
//主函数{
init();
while(1)
{
keyscan();
}
}
void timer0() interrupt 1
//中断函数{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
xiaoshi++;
if(xiaoshi==24)
{
xiaoshi=0;
day++;
if(day==4)
{
day=0;
mouth++;
if(mouth==4)
{
mouth=0;
year++;
} write_sfm(-5,mouth);
} write_sfm(-3,day);
}
write_sfm(0,xiaoshi);
}
write_sfm(2,fen);
}
write_sfm(4,miao);
}}
此帖出自
小平头技术问答
一周热门 更多>