#include<stdio.h>
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar miao,fen,shi,num1,num2,mshi,mge,fshi,fge,sshi,sge;
sbit key1=P2^7;
sbit led1=P2^4;
sbit key2=P2^6;
sbit key3=P2^5;
unsigned char code Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//段选码
void display(uchar mshi,uchar mge,uchar fshi,uchar fge,uchar sshi,uchar sge);
void keyscan();
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y++);
}
void main()
{
TMOD=0x11;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
while(1)
{
if(num2==20)
{
num2=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
}
if(fen==60)
{
fen=0;
shi++;
}
if(shi==24)
{
shi=0;
}
mshi=miao/10;
mge=miao%10;
fshi=fen/10;
fge=fen%10;
sshi=shi/10;
sge=shi%10;
}
keyscan();
display(mshi,mge,fshi,fge,sshi,sge);
}
}
void display(uchar mshi,uchar mge,uchar fshi,uchar fge,uchar sshi,uchar sge)
{
P1=0xdb;
P0=0x40;
delay(2);
P1=0xbf;
P0=Disp_Tab[mshi];
delay(2);
P1=0x7f;
P0=Disp_Tab[mge];
delay(2);
P1=0xef;
P0=Disp_Tab[fge];
delay(2);
P1=0xf7;
P0=Disp_Tab[fshi];
delay(2);
P1=0xfd;
P0=Disp_Tab[sge];
delay(2);
P1=0xfe;
P0=Disp_Tab[sshi];
delay(2);
}
void keyscan()
{
if(key1==0)
{
delay(10);
if(key1==1)
{
miao++;
if(miao==60)
miao=0;
while(!key1);
}
}
if(key2==0)
{
delay(10);
if(key2==1)
{
fen++;
if(fen==60)
fen=0;
while(!key2);
}
}
if(key3==0)
{
delay(10);
if(key3==1)
{
shi++;
if(shi==24)
shi=0;
while(!key3);
}
}
}
void T0_time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num1++;
if(num1==20)
{
num1=0;
led1=~led1;
}
}
void T1_time() interrupt 3
{
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
num2++;
}
原理很简单 不过忘了注释了 呵呵 献丑了 希望大侠们多提提好的建议 小弟初入门 谢谢了 不胜感激 |