//---------函數(shù)聲明,變量定義--------------------------
#include <reg51.h>
sbit GATE=P3^0; //預(yù)制門限
sbit CLR =P3^1; //請(qǐng)零
sbit RGATE=P3^2; //實(shí)際門限
sbit SEL0=P3^4; //數(shù)據(jù)選擇位0
sbit SEL1=P3^5; //數(shù)據(jù)選擇位1
sbit SEL2=P3^6; //數(shù)據(jù)選擇位2
#define data_in P1
unsigned char Nx[4]; //待測(cè)頻率計(jì)數(shù)值
unsigned char Ns[4]; //標(biāo)準(zhǔn)頻率計(jì)數(shù)值
unsigned char GATE_time=1; //門限時(shí)間,預(yù)定義為1S
unsigned char time_count;
bit PRE_judge=1;
//-----------------------變量聲明---------------------------------------------------------------------
void system_init(void ); //初始化,設(shè)置定時(shí)器0的工作方式,供主程序調(diào)用
void TIMER0_SCANkey(); //定時(shí)器0中斷處理函數(shù)
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: system_init()
// 函數(shù)功能: 初始化設(shè)置
// 設(shè)定INT0的工作方式
//--------------------------------------------------------------------------------------------------
void system_init(void )
{
TMOD=0x01; //定時(shí)器0工作在方式1
ET0=1; //定時(shí)器0中斷允許
TH0=-5000/256; //12M時(shí)鐘時(shí),定時(shí)0.01秒
TL0=-5000%256;
TR0=1; //定時(shí)器0開始計(jì)數(shù)
EA=1; //系統(tǒng)中斷允許
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: read_result
// 函數(shù)功能: 將計(jì)數(shù)值讀出到Nx、Ns
//--------------------------------------------------------------------------------------------------
void read_result()
{
GATE=0;
while(RGATE); //等待實(shí)際門限的結(jié)束
SEL0=0;
SEL1=0;
SEL2=0;
Nx[0]=data_in; //SEL=000
SEL0=1;
Nx[1]=data_in; //001
SEL1=1;
Nx[3]=data_in; //011
SEL0=0;
Nx[2]=data_in; //010
SEL0=0;
SEL1=0;
SEL2=1;
Ns[0]=data_in; //100
SEL0=1;
Ns[1]=data_in; //101
SEL1=1;
Ns[3]=data_in; //111
SEL0=0;
Ns[2]=data_in; //110
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: judge_Prage
// 函數(shù)功能: 判斷頻率范圍,
// 大于1M 即Nx>0x00 0F 42 40, GATE=1
// 小于1M大于1k即0x00 00 03 E8<Nx<0x00 0F 42 40,GATE=5
// 小于1k 即 Nx<0x00 00 03 E8, GATE=10
//--------------------------------------------------------------------------------------------------
void judge_Prage()
{
if((Nx[3]==0)&&(Nx[2]==0))
if((Nx[1]<3)||((Nx[1]==3)&&(Nx[0]<0xE8)))
GATE_time=10;
else GATE_time=5;
else if((Nx[3]==0)&&(Nx[2]<0x0f))
GATE_time=5;
else if((Nx[3]==0)&&(Nx[2]==0x0f))
if(Nx[1]<0x42)
GATE_time=5;
else if((Nx[1]==0x42)&&(Nx[0]<0x40))
GATE_time=5;
else
GATE_time=1;
else GATE_time=1;
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: calcu_Fx
// 函數(shù)功能: 計(jì)算頻率,根據(jù)Fx=(Nx/Ns)Fs 計(jì)算結(jié)果
//--------------------------------------------------------------------------------------------------
void calcu_Fx()
{
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: TIMER0_intrupt
// 函數(shù)功能: 定時(shí)器0中斷處理程序
//--------------------------------------------------------------------------------------------------
void TIMER0_intrupt() interrupt 1 using 1
{
EA=0; //系統(tǒng)中斷禁止
GATE=1;
TH0=-5000/256; //12M時(shí)鐘時(shí),定時(shí)0.01秒
TL0=-5000%256;
time_count++;
if(++time_count==100) GATE_time--;
while(GATE_time==0)
{
GATE=0;
read_result();
if(PRE_judge) //預(yù)測(cè),判斷頻率范圍
{
judge_Prage();
PRE_judge=0;
if(GATE_time) //如果頻率大于1M,直接計(jì)算,無(wú)須再測(cè)
calcu_Fx();
}
else
calcu_Fx(); //不是預(yù)測(cè),直接計(jì)算結(jié)果
}
EA=1;
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: main
// 函數(shù)功能: 主函數(shù)
//--------------------------------------------------------------------------------------------------
void main()
{
CLR=0;
GATE=1;
system_init();
}