#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code Disp[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char code Disp_Sel[] = {0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};
uchar aa=0;
unsigned int tmp;
long f;
void init();
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3;
unsigned char num=0;
uint ff;
uchar count;
uchar first;
void display();
void diffence();
void main()
{
init();
first=0;
while(1)
{
switch(count)//
{
case 0:P21=0;P22=0;P23=0;tmp=f*1;break;
case 1:P21=0;P22=0;P23=1;tmp=f*10;break;
case 2:P21=0;P22=1;P23=0;tmp=f*100;break;
case 3:P21=0;P22=1;P23=1;tmp=f*1000;break;
case 4:P21=1;P22=0;P23=0;tmp=f*10000;break;
case 5:P21=1;P22=0;P23=1;tmp=f*100000;break;
case 6:P21=1;P22=1;P23=0;tmp=f*1000000;break;
case 7:P21=1;P22=1;P23=1;tmp=f*10000000;break;
default:break;
}
}
}
void init()
{
TMOD=0x51;//T1计数。T0定时
TH1=0;
TL1=0;
TH0=(65536-50000)/256;//T0初值为50ms
TL0=(65536-50000)%256;
TR0=1;
TR1=1;
EA=1;
ET0=1;
}
void TT0() interrupt 1
{
TH0=(65536-50000)/256;//重赋T0初值为50ms
TL0=(65536-50000)%256;
aa++;
if(first == 0)//开始测频时,单片机进行粗略的计算
{
if(aa==20)
{
first =1;
aa=0;
f = (TH1*256 + TL1)+ff*65536;
ff=0;
TL1 = 0;
TH1 = 0;
diffence();//分挡位
}
}
else//分频后,精确测频
{
if(aa==20)
{
first =0;
aa=0;
f = (TH1*256 + TL1)+ff*65536;
ff=0;
TL1 = 0;
TH1 = 0;
display();//加入显示函数
}
}
}
void diffence()//分挡位
{
if(f < 5000)
count=0;
else if(((f == 5000)||(f > 5000)) && ((f == 10000)||(f < 10000)))
count=1;
else if((f > 10000) && (f < 50000))
count=2;
else if((f>50000)&&(f<100000))
count=3;
else if((f>100000)&&(f<500000))
count=4;
else if((f>500000)&&(f<1000000))
count=5;
else if((f>1000000)&&(f<10000000))
count=6;
else if((f>100000000)&&(f<200000000))
count=7;
}
void TT1() interrupt 3
{
ff++;
}
void display()
{
num++;
num=(num%8);
P1=0xff;
P0=0xff;
P0=Disp_Sel[num];
switch(num)
{
case 0: P1=Disp[f%10]; break;
case 1: P1=Disp[f%100/10]; break;
case 2: P1=Disp[f%1000/100]; break;
case 3: P1=Disp[f%10000/1000]; break;
case 4: P1=Disp[f%100000/10000]; break;
case 5: P1=Disp[f%1000000/10000]; break;
case 6: P1=Disp[f%1000000/100000]; break;
case 7: P1=Disp[f/1000000]; break;
default: break;
}
}