本帖最后由 739211964 于 2012-10-29 16:42 编辑
//液晶显示与时钟,及摁健的使用,遇到一个问题:主要是时间刷新时,二月(28、29)号、小月30号、大月31号没有变为1号,其他都好使。主要看定时器0中断1后的代码,求各路人才、高手帮忙,看看能不能把代码修改正确,临表涕零,不胜感激!!!(声明:小弟还没有用到ds12c887,只用到简单的定时器中断;还有,代码也是网上找的
)
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit lcdrw=P3^6;
sbit s1=P2^0;
sbit s2=P2^1;
sbit s3=P2^2;
uchar count,s1num;
char shi,fen,miao,xq,nian,yue,ri;
uchar code table1[]="2012-10-31 SUN";
uchar code table2[]=" 23:59:00";
// 周一周二 周三 周四周五 周六周日
uchar code table3[]={0xff,0x4d,0x54,0x57,0x54,0x46,0x53,0x53};
// M T W T F S S
uchar code table4[]={0xff,0x4f,0x55,0x45,0x48,0x52,0x41,0x55};
// O U E H R A U
uchar code table5[]={0xff,0x4e,0x45,0x44,0x55,0x49,0x54,0x4e};
// N E D U I T N
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;
lcdrw=0;
lcden=0;
delay(5);
P1=com;
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)
{
lcdrs=1;
lcdrw=0;
lcden=0;
delay(5);
P1=date;
lcden=1;
delay(5);
lcden=0;
}
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 write_nyrx(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 init()
{
uchar num;
shi=23;
fen=59;
miao=55;
// nian=2012;
yue=10;
ri=31;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<14;num++)
{
write_date(table1[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<11;num++)
{
write_date(table2[num]);
delay(5);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void keyscan()
{
if(s1==0)
{
delay(5);
if(s1==0)
{
s1num++;
while(!s1); //判断松手
if(s1num==1)
{
TR0=0; //关闭定时器,让时间停住
write_com(0x80+0x40+9);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+6);
}
if(s1num==3)
{
write_com(0x80+0x40+3);
}
if(s1num==4)
{
write_com(0x80+11);
}
if(s1num==5)
{
write_com(0x80+8);
}
if(s1num==6)
{
write_com(0x80+5);
}
if(s1num==7)
{
write_com(0x80+2);
}
if(s1num==8)
{
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(9,miao);
write_com(0x80+0x40+9);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(6,fen);
write_com(0x80+0x40+6);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(3,shi);
write_com(0x80+0x40+3);
}
if(s1num==4)
{
xq++;
if(xq==8)
xq=0;
// write_nyrx(9,xq);
write_date(table3[xq]);
write_date(table4[xq]);
write_date(table5[xq]);
write_com(0x80+11);
}
if(s1num==5)
{
ri++;
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{
if(ri==32)
ri=1;
}
else if(yue==4||yue==6||yue==9||yue==11)
{
if(ri>=31)
ri=1;
}
else
{
if(nian%4==0)
{
if(ri>=30)
ri=1;
}
else
{
if(ri>=29)
ri=1;
}
}
write_nyrx(8,ri);
write_com(0x80+8);
}
if(s1num==6)
{
yue++;
if(yue==13)
yue=1;
write_nyrx(5,yue);
write_com(0x80+5);
}
if(s1num==7)
{
nian++;
if(nian==100)
nian=0;
write_nyrx(2,nian);
write_com(0x80+2);
}
}
}
if(s3==0) //功能键:减
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(9,miao);
write_com(0x80+0x40+9);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(6,fen);
write_com(0x80+0x40+6);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(3,shi);
write_com(0x80+0x40+3);
}
if(s1num==4)
{
xq--;
if(xq==0)
xq=7;
write_date(table3[xq]);
write_date(table4[xq]);
write_date(table5[xq]);
write_com(0x80+11);
}
if(s1num==5)
{
ri--;
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{
if(ri==0)
ri=31;
}
else if(yue==4||yue==6||yue==9||yue==11)
{
if(ri==0)
ri=30;
}
else
{
if(nian%4==0)
{
if(ri==0)
ri=29;
}
else
{
if(ri==0)
ri=28;
}
}
write_nyrx(8,ri);
write_com(0x80+8);
}
if(s1num==6)
{
yue--;
if(yue==0)
yue=12;
write_nyrx(5,yue);
write_com(0x80+5);
}
if(s1num==7)
{
nian--;
if(nian==-1)
nian=99;
write_nyrx(2,nian);
write_com(0x80+2);
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
}
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(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{
if(ri==31)
{
ri=1;
yue++;
}
xq++;
if(xq==8)
xq=1;
if(yue==12)
{
yue=1;
nian++;
write_nyrx(2,nian);
}
write_nyrx(5,yue);
write_com(0x80+11);
write_date(table3[xq]);
write_date(table4[xq]);
write_date(table5[xq]);
}
else if(yue==4||yue==6||yue==9||yue==11)
{
if(ri==30)
{
ri=1;
yue++;
}
xq++;
if(xq==8)
xq=1;
if(yue==12)
{
yue=1;
nian++;
write_nyrx(2,nian);
}
write_nyrx(5,yue);
write_com(0x80+11);
write_date(table3[xq]);
write_date(table4[xq]);
write_date(table5[xq]);
// write_com(0x80+9);
}
else
{
if(nian%4==0)
{
if(ri==29)
{
ri=1;
yue++;
}
xq++;
if(xq==8)
xq=1;
if(yue==12)
{
yue=1;
nian++;
write_nyrx(2,nian);
}
write_nyrx(5,yue);
write_com(0x80+11);
write_date(table3[xq]);
write_date(table4[xq]);
write_date(table5[xq]);
// write_com(0x80+9);
}
else
{
if(ri==28)
{
ri=1;
yue++;
}
xq++;
if(xq==8)
xq=1;
if(yue==12)
{
yue=1;
nian++;
write_nyrx(2,nian);
}
write_nyrx(5,yue);
write_com(0x80+11);
write_date(table3[xq]);
write_date(table4[xq]);
write_date(table5[xq]);
// write_com(0x80+9);
}
}
write_nyrx(8,ri);
}
write_sfm(3,shi);
}
write_sfm(6,fen);
}
write_sfm(9,miao);
}
}
一周热门 更多>