89C52电子钟程序
#include<reg52.h> #define uint unsigned int
#define uchar unsigned char
sbit wela=P2^7; //键盘位选
sbit dula=P2^6; //键盘段选
sbit lcdrs=P3^5; //写数据与命令选择
sbit lcden=P3^4; //脉冲
sbit s1=P3^1; //调时选择调哪部分
sbit s2=P3^2; //增加
sbit s3=P3^3; //减少
sbit ss=P3^7; //检测信号
sbit kai=P3^6; //开始的确定
uchar code table[]=" 2011-03-12 sat"; //时间标准,开始的初始化
uchar code table1[]=" 00:00:00";
uchar code table2[]="Welcome my watch"; //欢迎界面
uchar code table3[]=" Thanks";
uchar code table4[]="Mon,Tue,Wen,Thu,Fri,Sat,Sun";//输出星期
uchar code table5[]=" Only for";
uchar code table6[]=" 2000-2099";
uchar shi,fen,miao,tt0,s1num,nian,yue,ri,week;
void delay(uint z) //延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--)
;
}
void write_com(uchar com) //写命令函数
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
delay(5);
}
void write_date(uchar date) //写数据函数
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
delay(5);
}
void write_sfm(uchar add,uchar date) //有地址的写数据
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(add);
write_date(shi+0x30);
write_date(ge+0x30);
}
void write_week() //写星期
{
uchar num;
num=week;
write_com(0x80+13);
for(;num<week+3;num++)
write_date(table4[num]);
}
void keyscan() //扫描键盘
{
ss=0;
if(s1==0) //选择调试的部位
{
delay(5);
if(s1==0)
{
while(!s1);
TR0=0;
s1num++;
}
}
switch(s1num) //调试的部位,光标闪烁
{
case 1:
write_com(0x0f);
write_com(0x80+0x40+10);
break;
case 2:
write_com(0x0f);
write_com(0x80+0x40+7);
break;
case 3:
write_com(0x0f);
write_com(0x80+0x40+4);
break;
case 4:
write_com(0x0f);
write_com(0x80+15);
break;
case 5:
write_com(0x0f);
write_com(0x80+11);
break;
case 6:
write_com(0x0f);
write_com(0x80+8);
break;
case 7:
write_com(0x0f);
write_com(0x80+5);
break;
case 8:
s1num=0;
write_com(0x0c);
TR0=1;
break;
}
/*
if(s1num==1)
{
write_com(0x0f);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
write_com(0x0f);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
write_com(0x0f);
write_com(0x80+0x40+4);
}
if(s1num==4)
{
s1num=0;
write_com(0x0c);
TR0=1;
}
*/
if(s2==0) //增加选择的部位数据
{
delay(5);
if(s2==0)
{
while(!s2);
switch(s1num)
{
case 1:
miao++;
if(miao==60)
miao=0;
write_sfm(0x80+0x40+9,miao);
break;
case 2:
fen++;
if(fen==60)
fen=0;
write_sfm(0x80+0x40+6,fen);
break;
case 3:
shi++;
if(shi==24)
shi=0;
write_sfm(0x80+0x40+3,shi);
break;
case 4:
week+=4;
if(week==28)
{
week=0;
}
write_week();
break;
case 5:
ri++;
if(ri==32)
ri=1;
write_sfm(0x80+10,ri);
break;
case 6:
yue++;
if(yue==13)
yue=1;
write_sfm(0x80+7,yue);
break;
case 7:
nian++;
if(nian==100)
nian=0;
write_sfm(0x80+4,nian);
break;
}
/*
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(0x80+0x40+9,miao);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(0x80+0x40+6,fen);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(0x80+0x40+3,shi);
}
*/
}
}
if(s3==0) //减少选择部位的数据
{
delay(5);
if(s3==0)
{
while(!s3);
switch(s1num)
{
case 1:
if(miao==0)
miao=59;
else
miao--;
write_sfm(0x80+0x40+9,miao);
break;
case 2:
if(fen==0)
fen=59;
else
fen--;
write_sfm(0x80+0x40+6,fen);
break;
case 3:
if(shi==0)
shi=23;
else
shi--;
write_sfm(0x80+0x40+3,shi);
break;
case 4:
if(week==0)
{
week=24;
}
else
week-=4;
write_week();
break;
case 5:
if(ri==1)
ri=31;
else
ri--;
write_sfm(0x80+10,ri);
break;
case 6:
if(yue==1)
yue=12;
else
yue--;
write_sfm(0x80+7,yue);
break;
case 7:
if(nian==0)
{
nian=99;
}
else
nian--;
write_sfm(0x80+4,nian);
break;
}
/*
if(s1num==1)
{
if(miao==0)
miao=59;
else
miao--;
write_sfm(0x80+0x40+9,miao);
}
if(s1num==2)
{
if(fen==0)
fen=59;
else
fen--;
write_sfm(0x80+0x40+6,fen);
}
if(s1num==3)
{
if(shi==0)
shi=23;
else
shi--;
write_sfm(0x80+0x40+3,shi);
}
*/
}
}
}
void show() //时间运行后显示月,日,
{
switch(yue)
{
case 1:
ri++;
if(ri==32)
{
ri=1;
yue++;
}
break;
case 2:
ri++;
if(nian%400==0||(nian%4==0&&nian%100!=0))
{
if(ri==30)
{
ri=1;
yue++;
}
}
else
{
if(ri==29)
{
ri=1;
yue++;
}
}
break;
case 3:
ri++;
if(ri==32)
{
ri=1;
yue++;
}
break;
case 4:
ri++;
if(ri==31)
{
ri=1;
yue++;
}
break;
case 5:
ri++;
if(ri==32)
{
ri=1;
yue++;
}
break;
case 6:
ri++;
if(ri==31)
{
ri=1;
yue++;
}
break;
case 7:
ri++;
if(ri==32)
{
ri=1;
yue++;
}
break;
case 8:
ri++;
if(ri==32)
{
ri=1;
yue++;
}
break;
case 9:
ri++;
if(ri==31)
{
ri=1;
yue++;
}
break;
case 10:
ri++;
if(ri==32)
{
ri=1;
yue++;
}
break;
case 11:
ri++;
if(ri==31)
{
ri=1;
yue++;
}
break;
case 12:
ri++;
if(ri==32)
{
ri=1;
yue=1;
nian++;
if(nian==100)
nian=0;
write_sfm(0x80+4,nian);
}
break;
}
write_sfm(0x80+7,yue);
write_sfm(0x80+10,ri);
}
void shizhong() //用单片机时钟作为时间运行的脉冲
{
if(tt0==18)
{
tt0=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
show();
week+=4;
if(week==28)
{
week=0;
}
write_week();
}
write_sfm(0x80+0x40+3,shi);
}
write_sfm(0x80+0x40+6,fen);
}
write_sfm(0x80+0x40+9,miao);
}
}
void init1() //初始化欢迎界面
{
uchar num;
write_com(0x80);
for(num=0;num<16;num++)
{
write_date(table2[num]);
}
write_com(0x80+0x40);
for(num=0;num<11;num++)
{
write_date(table3[num]);
}
kai=0;
while(1)
{
if(s1==0)
{
delay(5);
if(s1==0)
{
while(!s1);
write_com(0x01);
break;
}
}
}
write_com(0x80);
for(num=0;num<13;num++)
{
write_date(table5[num]);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table6[num]);
}
while(1)
{
if(s1==0)
{
delay(5);
if(s1==0)
{
while(!s1);
write_com(0x01);
break;
}
}
}
}
void init() //初始化1602,中断
{
uchar num;
s1num=0;
shi=fen=miao=tt0=0;
nian=yue=ri=week=0;
// shi=23;
// fen=58;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
wela=0;
dula=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x01);
write_com(0x80);
init1();
write_com(0x80);
for(num=0;num<16;num++)
{
write_date(table[num]);
}
write_com(0x80+0x40);
for(num=0;num<11;num++)
{
write_date(table1[num]);
}
TR0=1;
}
void main() //主函数,调用子函数
{
init();
while(1)
{
keyscan();
shizhong();
}
}
void t0() interrupt 1 //t0中断
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tt0++;
}
用郭天祥的板子可以用一下
text9.rar
(2.05 KB)
互相学习!指点指点。