#include<reg52.h>
#define uint unsigned int
unsigned int count=0;
unsigned h=0;
unsigned f=0;
unsigned m=1;
sbit S5=P3^2;
sbit S4=P3^3;
unsigned char code SMG[18]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f
};
void delay_SMG(uint time)
{
uint i,j;
for(i=time;i>0;i--)
for(j=114;j>0;j--);
}
void SelectHC573(unsigned char n)
{
switch(n)
{
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 Init()
{
SelectHC573(5);
P0=0x00;
SelectHC573(4);
P0=0xff;
}
void Init_Timer1()
{
TMOD=0x01;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
ET1=1;
EA=1;
TR1=1;
}
void Service_Timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
m++;
if(m==20)
{
f++;
if(f==60)
{
h++;
if(h>99)
{
h=0;
}
f=0;
}
m=0;
}
}
void Show_SMG_Bit(unsigned char dat,unsigned char pos)
{
SelectHC573(6);
P0=0x01<<pos;
SelectHC573(7);
P0=dat;
}
void Show_SMG()
{
Show_SMG_Bit(SMG[h/10],0);
delay_SMG(10);
Show_SMG_Bit(0xff,0);//消影
Show_SMG_Bit(SMG[h%10],1);
delay_SMG(10);
Show_SMG_Bit(0xff,1);
Show_SMG_Bit(0xbf,2);
delay_SMG(10);
Show_SMG_Bit(0xff,2);
Show_SMG_Bit(SMG[f/10],3);
delay_SMG(10);
Show_SMG_Bit(0xff,3);
Show_SMG_Bit(SMG[f%10],4);
delay_SMG(10);
Show_SMG_Bit(0xff,4);
Show_SMG_Bit(0xbf,5);
delay_SMG(10);
Show_SMG_Bit(0xff,5);
Show_SMG_Bit(SMG[m/10],6);
delay_SMG(10);
Show_SMG_Bit(0xff,6);
Show_SMG_Bit(SMG[m%10],7);
delay_SMG(10);
Show_SMG_Bit(0xff,7);
}
void ScanKeys_Alone()
{
if(S4==0)
{
delay_SMG(20);
if(S4==0)
{
TR1=~TR1;
while(S4==0)
{
Show_SMG();
}
}
}
if(S5==0)
{
delay_SMG(20);
if(S5==0)
{
h=0;
f=0;
m=0;
while(S5==0)
{
Show_SMG();
}
}
}
}
void main()
{
Init();
Init_Timer1();
while(1)
{
Show_SMG();//数码管也应该循环刷新
ScanKeys_Alone();
}
}
定时器实现秒表
最新推荐文章于 2023-12-18 20:22:58 发布