#include "reg51.h"
#define char unsigned char
char code dis_7[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
char code scan_con[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
char data dis[8]={0x00,0x00,0x0b,0x00,0x00,0x0b,0x00,0x00};
char data timedata[3]={0x00,0x00,0x00};
char data ms50=0x00,con=0x00,con1=0x00,con2=0x00;
sbit k0=P1^0;
sbit k1=P1^1;
sbit k2=P1^2;
delay1ms(int t)
{
int i,j;
for (i=0;i
for (j=0;j<120;j++);
}
keyscan()
{
EA=0;
if (k0==0)
{
delay1ms(10);
while (k0==0);
con++;TR0=0;ET0=0;
if (con>=3)
{con=0;TR0=1;ET0=1;}
}
if (con!=0)
{
if (k1==0)
{
delay1ms(10);
while (k1==0);
timedata[con]++;
if (con==2) con1=24;else con1=60;
if (timedata[con]>=con1)
{timedata[con]=0;}
}
}
if (con!=0)
{
if (k2==0)
{
delay1ms(10);
while (k2==0);
timedata[con]--;
if (con==2) con2=23;else con2=59;
if (timedata[con]<=0)
{timedata[con]=con2;}
}
}
EA=1;
}
scan()
{
char k;
dis[0]=timedata[0]%10;dis[1]=timedata[0]/10;
dis[3]=timedata[1]%10;dis[4]=timedata[1]/10;
dis[6]=timedata[2]%10;dis[7]=timedata[2]/10;
for (k=0;k<8;k++)
{
P0=dis_7[dis[k]];P2=scan_con[k];delay1ms(1);P2=0x00;
}
}
main()
{
TH0=0x3c;TL0=0xb0;
TMOD=0x01;ET0=1;TR0=1;EA=1;
while (1)
{
scan();
keyscan();
}
}
void time_intt0(void) interrupt 1
{
ET0=0;
TR0=0;
TH0=0x3c;
TL0=0xb0;
TR0=1;
ms50++;
if (ms50==20)
{
ms50=0x00;timedata[0]++;
if (timedata[0]==60)
{
timedata[0]=0;timedata[1]++;
if (timedata[1]==60)
{
timedata[1]=0;timedata[2]++;
if (timedata[2]==24)
{
timedata[2]=0;
}
}
}
}
ET0=1;
}
图片1.png
(380.85 KB, 下载次数: 44)
2018-12-27 16:20 上传