增加3个标志,k1开始/清0,k2暂停/继续。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_DATAPINS P0
sbit RS=P2^6;
sbit RW=P2^5;
sbit E=P2^7;
sbit k1=P3^0;
sbit k2=P3^1;
//sbit k3=P3^2;
//sbit k4=P3^3;
uchar mil,sec,min,count;
uchar code table[ ]="WH JIAYOU";
uchar code table1[ ]=" 00:00:0 ";
bit sign_1=0,sign_2=0,sign_3=0;
void delay(uint z)
{
uint x,y;
for(x=0;x<z;x++)
for(y=0;y<100;y++);
}
void write_com(uchar com)
{
delay(1);
RS=0;
E=0;
RW=0;
delay(5);
LCD_DATAPINS=com;
E=1;
delay(5);
E=0;
}
void write_date(uchar date)
{
delay(1);
RS=1;
E=0;
RW=0;
delay(5);
LCD_DATAPINS=date;
E=1;
delay(5);
E=0;
}
void init()
{
uchar i;
E=0;
mil=0;
sec=0;
min=0;
count=0;
write_com(0x38);
write_com(0x01);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
for(i=0;i<9;i++)
{
write_date(table[i]);
}
write_com(0x80+0x40);
for(i=0;i<12;i++)
{
write_date(table1[i]);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void write_msmm(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_mm(uchar a,uchar date)
{
uchar shi;
shi=date%10;
write_com(0x80+0x40+a);
write_date(0x30+shi);
}
void keycan()
{
if(k1==0)
{
delay(5);
if(k1==0)
{
sign_1=~sign_1;//0=清0,1=开始
if(sign_1==1)
sign_2=0;
if(sign_1==0)
{
sign_2=0;
mil=0;
sec=0;
min=0;
count=0;
write_msmm(4,min);
write_msmm(7,sec);
write_mm(10,mil);
}
while(!k1);
}
}
if(k2==0)
{
delay(5);
if(k2==0)
{
sign_2=~sign_2;//0=继续,1=暂停
while(!k2);
}
}
}
void main()
{
init();
while(1)
{
keycan();
if(sign_3==1)
{
sign_3=0;
mil++;
if(mil==10)
{
mil=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
}
write_msmm(4,min);
}
write_msmm(7,sec);
}
write_mm(10,mil);
}
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
if((sign_1==1)&&(sign_2==0))
{
count++;
if(count==2)
{
count=0;
sign_3=1;
}
}
} |