#includeunsigned char t=0,yichu=0,fenpin;sbit B153=P2^0;sbit A153=P2^1;sbit GATE=P2^6;sbit CLR=P2^7;sbit P33=P3^3;void t0(void)interrupt 1{t++;yichu=2; //Timer 0 overflow,yichu=2}void t1(void)interrupt 3 //Counter 1 overflow,yichu=1{TCON=0;yichu=1;TH1=0xff;TL1=0xff;}void delay(void) //1s software delay{unsigned int i=500,j;while(i) {i--; j=250; while(j) {j--;} }}unsigned char table(unsigned char x) //Software table lookup output{unsigned char code n[10]={0xee,0x82,0xdc,0xd6,0xb2,0x76,0x7e,0xc2,0xfe,0xf6};return(n[x]);}display(float f) //Serial output with measured frequency{unsigned long x;unsigned char a[5]={0,0,0,0,0},i=0,j;if(f!=0){if((f<10000)&&(f>1)) //Normal display {if(f>1000)f=f-1; //Software correction of frequency deviation while(f<1000) {f=f*10; i++; } x=f*10; a[4]=x%10; if(a[4]>=5)f=f+1;; x=f; a[1]=x%10; a[2]=(x/10)%10; a[3]=(x/100)%10; a[4]=(x/1000)%10; for(j=0;j<5;j++) {a[j]=table(a[j]);} a[i+1]++; a[0]=0; }else if(f>=10000) //Display in scientific notation {while(f>=1000) {f=f/10; i++; } x=f; a[4]=(unsigned char)((float)((fx))*10); if(a[4]>=5)x++; //Round off a[2]=x%10; a[3]=(x/10)%10; a[4]=(x/100)%10; a[0]=i+2; for(j=0;j<5;j++) {a[j]=table(a[j]);} a[4]++; a[1]=0x7c; }else {x=f*10000; //Frequency value is less than 1 if((x%10)>=5)x=x+10; //Rounding x=x/10; a[1]=x%10; a[2]=(x/10)%10; a[3]=(x/100)%10; a[4]=(x/1000)%10; for(j=0;j<5;j++) {a[j]=table(a[j]);} a[4]++; a[0]=0; } }for(j=0;j<5;j++) //Serial {SBUF=a[j]; while(TI==0);}}cepin(){unsigned char i;float sj;unsigned long js;//Time, count\'s pinyin initialsfloat f; //f is frequencyTMOD=0xd9;t=0;TH0=0;TL0=0;TH1=0;TL1=0;GATE=0;TCON=0x50;GATE=1;delay();GATE=0;for(i=0;i<250;i++){} //Delay 1mssj=((float)(TH0*256+t*65536+TL0))/1000000.0;js=(long)TH1*256+TL1+1;f=(js/sj)*fenpin;display(f);}panduan(){float zhouqi;B153=1;A153=1;TMOD=0x51; TH0=0xce; //Timer 0=12.8msTL0=0;TH1=0xff; //Counter 1 = 100 pulses TL1 = 0x9c; TCON = 0x50; while (yichu = = 0); TCON = 0; if (yichu = = 1) //Counter overflows first: Too many pulses measured within 12.8ms, indicating that the frequency is high {fenpin = 128; //Switch to measure the frequency after 128 division cepin (); }else //Timer overflows first: 100 pulses are short, that is, the frequency is low, and the division number can be reduced {yichu = 0; B153 = 1; A153 = 0; TH0 = 0xc1; //Timer 0 = 16ms TL0 = 0x80; TH1 = 0xff; //Counter 1 = 100 pulses TL1 = 0x9c; TCON = 0x50; while (yichu = = 0); TCON = 0; if (yichu = = 1) //Counter overflows first: Too many pulses measured within 16ms, indicating that the frequency is high {fenpin=16;//switch to measuring the frequency after 16 division cepin();} else //The timer overflows first: the time of 100 pulses is relatively short, that is, the frequency is relatively low, use 2 division to measure the period {B153=0; //The following is the period measurement part A153=0; TH0=0; TL0=0; t=0; TMOD=0x09; TR0=1; GATE=1; while(P33==0); GATE=0; while(P33==1); TR0=0; zhouqi=(TH0*256+t*65536+TL0)/1000000.0; if(zhouqi<0.001) //If the period is too small, switch to measuring the original frequency {fenpin=1; B153=0; A153=0; cepin();} else{display((1.0/zhouqi));} } }}main(){IE=0x8a;while(1) { panduan(); //Judge which range the original frequency belongs to, and call the corresponding function to measure its frequency delay(); }}