各位大虾我是一个初学者,写了个时钟程序遇到了问题,时钟的分调节功能没办法实现,请好心人帮忙看看:
#include<reg52.h>
#define unint unsigned int
#define unchar unsigned char
unchar code table0[]="2010-01-01";
unchar code table1[]="00:00:00";
unchar code table2[]={'0','1','2','3','4','5','6','7','8','9'};
sbit rw=P2^5;
sbit rs=P2^6;
sbit lcden=P2^7;
sbit key0=P2^4;//功能键
sbit key1=P2^3;//数值加键
sbit key2=P2^2;//数值减键
char jsnum=0,sec,ff,hh,key0num=0;
void write_sfh(unchar ,unchar );
void delay(unint z)
{
unint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(unchar com)
{
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(unchar date)
{
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void lcd_init()
{
unint t;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(t=0;t<10;t++)
{
write_date(table0[t]);
delay(5);
}
write_com(0x80+0x40);
for(t=0;t<8;t++)
{
write_date(table1[t]);
delay(5);
}
}
void time_init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void keyscan()
{
if(key0==0)
{
delay(20);
if(key0==0)
{
key0num++;
while(!key0);
}
}
if(key1==0&key0num)
{
delay(20);
if(key1==0)
{
while(!key1);
if(key0num==1)
{
sec++;
if(sec==60) sec=0;
write_sfh(6,sec);}
if(key0num==2)
{
ff++;
if(ff==60) ff=0;
write_sfh(3,ff);}
if(key0num==3)
{
hh++;
if(hh==24) hh=0;
write_sfh(0,hh);}
}
}
if(key2==0&key0num)
{
delay(20);
if(key2==0)
{
while(!key2);
if(key0num==1)
{
sec--;
if(sec==-1) sec=59;
write_sfh(6,sec);}
if(key0num==2)
{
ff--;
if(ff==-1) ff=59;
write_sfh(3,ff);}
if(key0num==3)
{
hh--;
if(hh==-1) hh=23;
write_sfh(0,hh);}
}
}
}
void main()
{
rw=0;
sec=0;
ff=0;
hh=0;
lcd_init();
time_init();
while(1)
{
keyscan();
if(jsnum==20)
{
jsnum=0;
sec++;
if(sec==60)
{
sec=0;
ff++;
if(ff==60)
{
ff=0;
hh++;
if(hh==24)
{
hh=0;
}
write_sfh(0,hh);
}
write_sfh(3,ff);
}
write_sfh(6,sec);
}
}
}
void time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
jsnum++;
}
void write_sfh(unchar add,unchar date)
{
unchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(table2[shi]);
write_date(table2[ge]);
} |