测0-999hz是正常的 但是测1k-9.9k 就不正常 比如测1.2k 只能显示001.0 , 3.2k 显示003.0 这样的问题啊 程序里面有MHZ档 我这不需要 可以删掉 绿色的led灯也能删掉 谢谢大神们了!!!
#include <reg52.h>
#define Segment P2
#define sl P0
sbit addr0=P1^0; // 定义数据端口地址,000时16分频,100 分频
sbit addr1=P1^1;
sbit addr2=P1^2;
sbit clear=P1^3; //计数器清零
sbit ledM=P1^4; //档位led显示
sbit ledK=P1^5;
sbit ledB=P1^6;
unsigned char cnt; //定时1s计数
unsigned char d[4]; //对应数码管的各位
unsigned char level; //档位
unsigned int tN; //不同档位计时值
unsigned int fCnt; //脉冲下降沿次数
unsigned int regCnt; //脉冲次数暂存
unsigned int pCnt; //频率显示
unsigned char Num[10]={0x3f,0x06,0x5b,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]]+0x80;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;
}
}
void intial()
{
clear=1; //分频器初始化
addr0=0;
addr1=0;
addr2=1;
tN=20; //档位初始化为Hz档
level=0;
ledM=1; //显示Hz档
ledK=1;
ledB=0;
TMOD=0x61; //定时器0,工作方式1,计数器1,工作方式2(8位自动重装)
TH0=(65536-50000)/256; //计数50ms
TL0=(65536-50000)%256;
TH1=256-1;//计数器1计数初值
TL1=256-1;
TR0=1; //开定时器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)
{
ET1=0; //计时1s时,暂停外部计数1
TR1=0;
cnt=0;
regCnt=fCnt; //更新显示
fCnt=0; //频率计数清零
clear=0; //硬件分频器清零
clear=1;
if(level==0) //档位切换
{
pCnt=regCnt;
if(regCnt>999)
{
pCnt=0; //显示清零
level=1;
tN=1; // 定时50ms
addr2=0;//进行分频处理
TH1=0xfb; //50分频 256-5
TL1=0xfb;
}
}
else if(level==1)
{
if(regCnt<1)
{
level=0;
tN=20; //定时1s
addr2=1; //不分频
TH1=0xff; // 256-1
TL1=0xff; // 256-1
ledM=1; //显示HZ档
ledK=1;
ledB=0;
}
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;
tN=1; //定时50ms
addr2=0; //分频
TH1=0xf6; // 100分频 256-10
TL1=0xf6; //256-10
pCnt=0; //换算成相应的显示:f=regCnt*100
ledM=0; //显示MHz档
ledK=1;
ledB=1;
}
}
else if(level==2)
{
if(regCnt<500) //KHz档
{
level=1;
tN=1; //定时50ms
addr2=0; //分频
TH1=0xfb; // 50分频 256-5
TL1=0xfb; //256-5
pCnt=0; //换算成相应的显示
ledM=1; //显示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
pCnt=regCnt*2; //换算成相应的显示:f=regCnt*100
ledM=0; //显示MHz档
ledK=1;
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++;
}
|