#include “STC15F2K60S2.H”
#include “intrins.h”
#define uchar unsigned char
uchar bLoose;
unsigned int ms;
uchar s;
uchar m;
//uchar ckey;
//uchar cKeyCode;
uchar cScanIndex;
unsigned char SMG_duanma[18]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x80,0x8e,0xbf,0x7f};
unsigned char SMG_weima[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char ScanIndex=0;
unsigned char SMG_Segcode[8];
sbit S4 = P3^3;
sbit S5 = P3^2;
void Delay500us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 6;
j = 93;
do
{
while (--j);
} while (--i);
}
void Delay2ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 22;
j = 128;
do
{
while (--j);
} while (--i);
}
void SelectHC138(unsigned char channel)
{
switch(channel)
{
case 4:
P2 = P2 & 0x1f | 0x80;
break;
case 5:
P2 = P2 & 0x1f | 0xa0;
break;
case 6:
P2 = P2 & 0x1f | 0xc0;
break;
case 7:
P2 = P2 & 0x1f | 0xe0;
break;
}
}
void Display()
{
SMG_Segcode[0] = SMG_duanma[m/10];
SMG_Segcode[1] = SMG_duanma[m%10];
SMG_Segcode[2] = SMG_duanma[16];
SMG_Segcode[3] = SMG_duanma[s/10];
SMG_Segcode[4] = SMG_duanma[s%10];
SMG_Segcode[5] = SMG_duanma[16];
SMG_Segcode[6] = SMG_duanma[ms/10];
SMG_Segcode[7] = SMG_duanma[ms%10];
}
void show()
{
SelectHC138(7);
P0 = 0xff;
SelectHC138(6);
P0 = SMG_weima[cScanIndex];
SelectHC138(7);
P0 = SMG_Segcode[cScanIndex];
cScanIndex++;
cScanIndex %= 8;
Display();
Delay500us();
}
void Key_Judge()
{
if(S4 == 0)
{
Delay2ms();
if(S4 == 0){
TR0 = !TR0;
while(S4 == 0)
show();
}
}
else if(S5 == 0)
{
Delay2ms();
if(S5 == 0){
ms = m = s = 0;
while(S4 == 0)
show();
}
}
}
void main()
{
TMOD = 0X01;
TH0 = (65535-10000)/256;
TL0 = (65535-10000)%256;
ET0 = 1;
EA = 1;
TR0 = 1;
while(1)
{
show();
Key_Judge();
}
}
void INT0() interrupt 1
{
TH0 = (65535 - 10000) / 256;
TL0 = (65535 - 10000) % 256;
ms++;
if(ms == 100)
{
ms = 0;
s++;
}
if(s == 60)
{
s = 0;
m++;
}
if(m == 100)
m = 0;
}