#include "reg52.h"
sbit YMQ_A=P2^5;
sbit YMQ_B=P2^6;
sbit YMQ_C=P2^7;
sbit S4=P3^3;
sbit S5=P3^2;
unsigned char min=0;
unsigned char sec=0;
unsigned char msec50=0;
unsigned char code SMG_Duanma[18]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0x88,0x83,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
void Delay(unsigned int t)
{
while(t--);
}
void InitHC138(unsigned char n)
{
switch(n)
{
case 4:
YMQ_C=1;
YMQ_B=0;
YMQ_A=0;
break;
case 5:
YMQ_C=1;
YMQ_B=0;
YMQ_A=1;
break;
case 6:
YMQ_C=1;
YMQ_B=1;
YMQ_A=0;
break;
case 7:
YMQ_C=1;
YMQ_B=1;
YMQ_A=1;
break;
}
}
void DisplaySMG_Bit(unsigned char value,unsigned char pos)
{
InitHC138(6);
P0=0x01<<pos-1;
InitHC138(7);
P0=value;
}
void DisplayTime()
{
DisplaySMG_Bit(SMG_Duanma[msec50%10],8);
Delay(500);
DisplaySMG_Bit(SMG_Duanma[msec50/10],7);
Delay(500);
DisplaySMG_Bit(SMG_Duanma[16],6);
Delay(500);
DisplaySMG_Bit(SMG_Duanma[sec%10],5);
Delay(500);
DisplaySMG_Bit(SMG_Duanma[sec/10],4);
Delay(500);
DisplaySMG_Bit(SMG_Duanma[16],3);
Delay(500);
DisplaySMG_Bit(SMG_Duanma[min%10],2);
Delay(500);
DisplaySMG_Bit(SMG_Duanma[min/10],1);
Delay(500);
}
//====定时器相关函数======
void InitTimer0()
{
TMOD=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
EA=1;
ET0=1;
TR0=0;
}
void ServiceTimer0() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
msec50++;
if(msec50==20)
{
sec++;
msec50=0;
if(sec==60)
{
min++;
sec=0;
}
if(min==99)
{
min=0;
}
}
}
//=======================
//====按键相关函数======
void ScanKeys()
{
if(S4==0) //秒表启动与暂停
{
Delay(100);
if(S4==0)
{
while(S4==0);
{
DisplayTime();
}
TR0=~TR0;
Delay(100);
}
}
if(S5==0) //秒表清零
{
Delay(100);
if(S5==0)
{
while(S5==0);
{
DisplayTime();
}
msec50=0;
sec=0;
min=0;
Delay(100);
}
}
}
//=======================
void main()
{
InitTimer0();
while(1)
{
DisplayTime();
ScanKeys();
}
}