//目测目前出现的问题是年输入方面,已经标出,想做一个比较完整的数字时钟,调试了30分钟没找出问题,还望心细同学提醒一下
//我用的是独立键盘 一下关于年输入的已标出,有两处。其他都没问题。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P2^7;
sbit lcdwr=P2^5;
sbit lcdrs=P2^6;
sbit k1=P1^0;
sbit k2=P1^1;
sbit k3=P1^2;
uchar code table[]="2014-02-08";
uchar code table1[]="00:00:00";
uchar code table3[]="MONTUEWEDTHUFRISATSUN";
uint shi,fen,miao,s,week=1,n=2014,y=2,r=8;
void delay(uint ms)
{
uint x,y;
for(x=ms;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
lcdwr=0;
lcdrs=0;
P0=com;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
void write_data(uchar dat)
{
lcdwr=0;
lcdrs=1;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
uint num;
lcden=0;
write_com(0x38);
write_com(0x0e);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<10;num++)
{
write_data(table[num]);
delay(50);
}
write_com(0x80+0x40);
for(num=0;num<8;num++)
{
write_data(table1[num]);
delay(50);
}
write_com(0x80+11);
for(num=0;num<=2;num++)
{
write_data(table3[num]);
delay(50);
}
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
}
void write_sfm(uchar add,uchar date)
{
uint shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_nyr(uchar add,uchar date)
{
uint qian,bai,shi,ge;
if(date>=100) //这里是关于年输入的
{
qian=date/1000;
bai=date/100%10;
shi=date%100/10;
ge=date%10;
write_com(0x80+add);
write_data(0x30+qian);
write_data(0x30+bai);
write_data(0x30+shi);
write_data(0x30+ge);
}
else
{
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
}
void write_week(uchar add,uchar date)
{
uint num;
switch(date)
{
case 1: write_com(0x80+add);
for(num=0;num<3;num++)
write_data(table3[num]);
break;
case 2: write_com(0x80+add);
for(num=3;num<6;num++)
write_data(table3[num]);
break;
case 3: write_com(0x80+add);
for(num=6;num<9;num++)
write_data(table3[num]);
break;
case 4: write_com(0x80+add);
for(num=9;num<12;num++)
write_data(table3[num]);
break;
case 5: write_com(0x80+add);
for(num=12;num<15;num++)
write_data(table3[num]);
break;
case 6: write_com(0x80+add);
for(num=15;num<18;num++)
write_data(table3[num]);
break;
case 7: write_com(0x80+add);
for(num=18;num<21;num++)
write_data(table3[num]);
break;
}
}
void keyscan()
{
if(k1==0)
{
delay(5);
if(k1==0)
{
s++;
while(!k1);
delay(5);
while(!k1);
switch(s)
{
case 1: TR0=0;
write_com(0x80+0x40+7);
break;
case 2: write_com(0x80+0x40+4);
break;
case 3: write_com(0x80+0x40+1);
break;
case 4: write_com(0x80+3);
break;
case 5: write_com(0x80+6);
break;
case 6: write_com(0x80+9);
break;
case 7: write_com(0x80+11);
break;
case 8: s=0;
write_com(0x0f);
TR0=1;
break;
}
}
}
if(s!=0)
{
delay(5);
if(k2==0)
{
if(k2==0)
{
while(!k2);
delay(5);
while(!k2);
switch(s)
{
case 1: miao++;
if(miao==60)
miao=0;
write_sfm(6,miao);
write_com(0x80+0x40+7);
break;
case 2: fen++;
if(fen==60)
fen=0;
write_sfm(3,fen);
write_com(0x80+0x40+4);
break;
case 3: shi++;
if(shi==24)
shi=0;
write_sfm(0,shi);
write_com(0x80+0x40+1);
break;
case 4: n++; //年输入
write_nyr(0,n);
write_com(0x80);
break;
case 5: y++;
if(y==13)
y=1;
write_nyr(5,y);
write_com(0x80+6);
break;
case 6: r++;
if(r==31)
r=1;
write_nyr(8,r);
write_com(0x80+9);
break;
case 7: week++;
if(week==8)
week=1;
write_week(11,week);
write_com(0x80+11);
break;
}
}
}
if(k3==0)
{
delay(5);
if(k3==0)
{
while(!k3);
delay(5);
while(!k3);
switch(s)
{
case 1: miao--;
if(miao==-1)
miao=59;
write_sfm(6,miao);
write_com(0x80+0x40+7);
break;
case 2: fen--;
if(fen==-1)
fen=50;
write_sfm(3,fen);
write_com(0x80+0x40+4);
break;
case 3: shi--;
if(shi==-1)
shi=23;
write_sfm(0,shi);
write_com(0x80+0x40+1);
break;
case 4: n--;
write_nyr(0,n);
write_com(0x80);
break;
case 5: y--;
if(y==0)
y=12;
write_nyr(5,y);
write_com(0x80+6);
break;
case 6: r--;
if(r==0)
r=31;
write_nyr(8,r);
write_com(0x80+9);
break;
case 7: week--;
if(week==0)
week=7;
write_week(11,week);
write_com(0x80+11);
break;
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
}
void
timer0() interrupt 1
{
uint count;
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;
shi++;
if(shi==24)
{
shi=0;
week++;
if(week==8)
{
week=1;
}
write_week(11,week);
r++;
if(r==31)
{
r=1;
y++;
if(y==13)
{
y=1;
n++;
write_nyr(0,n);
}
write_nyr(5,y);
}
write_nyr(8,r);
}
write_sfm(0,shi);
}
write_sfm(3,fen);
}
write_sfm(6,miao);
}
}
if(r==31) write_nyr(5,y); //并不是只有到31日 月才能加1 有的月只有28天,还有的时候是29天
另外,本人手机的时间只允许设置为1980-2079年,所以根本不需要放开那么长的时间
再就是,可以用时钟芯片做这个程序,那样比较严谨,而且时间误差小
{:1:}{:1:}
厉害。。。。
一周热门 更多>