#include <reg52.h>
#define Segment P2
#define sl P0
unsigned char cnt; //定时1s计数
unsigned char d[]; //对应数码管的各位
unsigned char level; //档位
unsigned int tN; //不同档位计时值
unsigned int fCnt; //脉冲下降沿次数
unsigned int regCnt; //脉冲次数暂存
unsigned int pCnt; //频率显示
unsigned char Num[10]={0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void Delay(unsigned int DelayTime)
{
while(DelayTime--) ;
}
void disp()
{ if(level==0) //B位档
{
d[3]=0;
d[2]=pCnt/100;
d[1]=(pCnt%100)/10;
d[0]=pCnt%10;
sl=0xfe;Delay(1);Segment=Num[d[3]];Delay(300);
sl=0xfd;Delay(1);Segment=Num[d[2]];Delay(300);
sl=0xfb;Delay(1);Segment=Num[d[1]];Delay(300);
sl=0xf7;Delay(1);Segment=Num[d[0]];Delay(300);
}
else if(level==1)
{
d[3]=pCnt/1000;
d[2]=(pCnt%1000)/100;
d[1]=(pCnt%100)/10;
d[0]=pCnt%10;
sl=0xfe;Delay(1);Segment=Num[d[3]];Delay(300);
sl=0xfd;Delay(1);Segment=Num[d[2]];Delay(300);
sl=0xfb;Delay(1);Segment=Num[d[1]]+0x80;Delay(300); //带小数点
sl=0xf7;Delay(1);Segment=Num[d[0]];Delay(300);
}
else if(level==2)
{
d[3]=pCnt/1000;
d[2]=(pCnt%1000)/100;
d[1]=(pCnt%100)/10;
d[0]=pCnt%10;
sl=0xfe;Delay(1);Segment=Num[d[3]];Delay(300);
sl=0xfd;Delay(1);Segment=Num[d[2]];Delay(300);
sl=0xfb;Delay(1);Segment=Num[d[1]];Delay(300);
sl=0xf7;Delay(1);Segment=Num[d[0]];Delay(300);
}
}
else
{ d[3]=0;
d[2]=0;
d[1]=0;
d[0]=0;
sl=0xfe;Delay(1);Segment=Num[d[3]];Delay(300);
sl=0xfd;Delay(1);Segment=Num[d[2]];Delay(300);
sl=0xfb;Delay(1);Segment=Num[d[1]];Delay(300);
sl=0xf7;Delay(1);Segment=Num[d[0]];Delay(300);
}
void intial()
{
clear=1;
addr=1; //分频器初始化
tN=20; //档位初始化为Hz档
level=0;
ledM=1; //显示Hz档
ledK=1;
ledB=0;
TMOD=0x61; //定时器0,工作方式1,计数器1,工作方式2(8位自动重装)
//开定时器0中断
ET0=1; //定时器0允许
TR1=1; //开计数器1中断
ET1=1; //允许计数器1中断
EA=1; //总中断允许
}
void main()
{
intial();
while(1)
{
disp();
}
}
void timer0() interrupt 1
{
cnt++;
if(cnt==tN)
{
regCnt=fCnt; //更新显示
fCnt=0; //频率计数清零
clear=0; //硬件分频器清零
clear=1;
if(level==0) //档位切换
{
pCnt=regCnt;
if(regCnt>999)
{
pCnt=0; //显示清零
}
}
else if(level==1)
{
if(regCnt<1)
{
level=0;
tN=20; //定时1s
addr2=1; //不分频
}
else if(regCnt<1000) //KHz档
{
level=1;
tN=1; //定时50ms
addr2=0; //硬件分频处理
TH1=0xfb; //50分频 256-5
TL1=0xfb; //256-5
pCnt=regCnt*10; //换算成相应的显示:f=regCnt*100
ledM=1; //显示KHz档
ledK=0;
ledB=1;
}
else
{
level=2;
//显示MHz档
ledK=1;
ledB=1;
}
}
else if(level==2)
{
if(regCnt<500) //KHz档
{
level=1;
tN=1; //定时50ms
addr2=0; //分频
//显示KHz档
ledK=0;
ledB=1;
}
else if(regCnt<5000)
{
level=2;
tN=1; //定时50ms
addr2=0; //分频
TH1=0xf6; //100分频 256-10
TL1=0xf6; //256-10
ledB=1;
}
else
{
level=3; //超出范围
ledM=1; //全灭
ledK=1;
ledB=1;
}
}
ET1=1; //恢复外部计数1
TR1=1;
}
TH0=0x3c; //(65536-50000)/256;
TL0=0xb0; //(65536-50000)%256;
}
void enterFre() interrupt 3 //外部频率输入
{
fCnt++;
} |