|
本人做的一个万年历程序.
在调节年,月,日,时,分,秒的时候,偶尔会出错.比如出现 H6,H7等一些非所要的十进制字符.
为什么呢?
#include
#define uint unsigned int
#define uchar unsigned char
uchar code table[]=" 2008-10-12 MON";
uchar code table1[]=" 00:00:00";
uchar num,count,s1num;
char shi,fen,miao,nian,yue,ri;
sbit duan=P2^6;
sbit wei=P2^7;
sbit rs=P3^5;
sbit lcden=P3^4;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit rd=P3^7;
void init();//初始化;
void delay(uint z);//延时;
void write_com(uchar com); //1602写命令;
void write_date(uchar date); //1602写数据;
void write_sfm(uchar add,uchar date);//调整时,分,秒;
void write_n(uchar add,uchar date ); //调整年;
void write_yr(uchar add,uchar date);// 调整月日;
void keyscan();//刷新显示;
void main()
{
init();
while(1)
{
keyscan();
}
}
void delay(uint z)
{
uint x,y;
for(x=0;x
for(y=0;y<110;y++);
}
void init()
{
duan=0;
wei=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(20);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(20);
}
}
void write_com(uchar com)
{
rs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
delay(5);
}
void write_date(uchar date)
{
rs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
delay(5);
}
void write_n(uchar add,uchar date )
{
uchar qian,bai,shi,ge;
qian=date/1000;
bai=(date-qian*1000)/100;
shi=(date-qian*1000-bai*100)/10;
ge=(date-qian*1000-bai*100-shi*10);
write_com(0x80+add);
write_date(0x30+qian);
write_date(0x30+bai);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_yr(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
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 timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==18)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
ri++;
if(ri==30)
{
yue++;
if(yue==12)
{
nian++;
write_n(3,nian);
}
write_yr(6,yue);
}
write_yr(9,ri);
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
}
void keyscan()
{
rd=0;
if(s1==0)
{
delay(20);
if(s1==0)
{
s1num++;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+11);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+8);
}
if(s1num==3)
{
write_com(0x80+0x40+5);
}
if(s1num==4)
{
write_com(0x80+9);
}
if(s1num==5)
{
write_com(0x80+6);
}
if(s1num==6)
{
write_com(0x80+3);
}
if(s1num==7)
{
s1num=0;
write_com(0x0c);
TR0=1;
}
}
}
if(s1num!=0)
{
delay(5);
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
if(s1num==4)
{
ri++;
if(ri==31)
ri=1;
write_yr(9,ri);
write_com(0x80+9);
}
if(s1num==5)
{
yue++;
if(yue==13)
yue=1;
write_yr(6,yue);
write_com(0x80+6);
}
if(s1num==6)
{
nian++;
if(nian==9999)
nian=0;
write_n(3,nian);
write_com(0x80+3);
}
}
}
}
}
if(s1num!=0)
{
delay(5);
if(s1num!=0)
{
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
if(s1num==4)
{
ri--;
if(ri==0)
ri=30;
write_yr(9,ri);
write_com(0x80+9);
}
if(s1num==5)
{
yue--;
if(yue==0)
yue=12;
write_yr(6,yue);
write_com(0x80+6);
}
if(s1num==6)
{
nian--;
if(nian==0)
nian=9999;
write_n(3,nian);
write_com(0x80+3);
}
}
}
}
}
}
|
|