|
70黑幣
下面是我在論壇上下載的頻率計(jì)的代碼和電路圖,我在原電路圖的基礎(chǔ)上加了一個(gè)整形電路,晶振頻率設(shè)置的是11.0592MHZ,仿真的結(jié)果也在下面。我的問題是:
1.為什么它會(huì)在較高頻率時(shí)出現(xiàn)較大誤差。我覺得這段代碼設(shè)計(jì)算頻率用的是直接測(cè)頻法,可為什么會(huì)在測(cè)高頻出現(xiàn)較大誤差,而在測(cè)低頻時(shí)很準(zhǔn)確,是我理解錯(cuò)了,它用的是測(cè)周法?
2.為什么當(dāng)我上調(diào)輸入頻率時(shí)(大概5kHZ以上),顯示結(jié)果會(huì)出現(xiàn)閃動(dòng),調(diào)的越高閃動(dòng)的越快,是動(dòng)態(tài)顯示的哪里出了問題嗎?有什么解決思路?
3.x=T0count*65536+TH0*256+TL0,這段代碼我還是不太明白,為什么要將計(jì)數(shù)器值這樣轉(zhuǎn)換才能成為頻率值?
以上是我的疑惑,希望哪位大佬能幫我解答一下,十分感謝!
電路圖
單片機(jī)源程序如下:
#include <AT89X51.H>
unsigned char codedispbit[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //P2的掃描位
unsigned char codedispcode[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40}; //數(shù)碼管的字形編碼
unsigned char dispbuf[8]={0,0,0,0,0,0,10,10};//初始化顯示值
unsigned char temp[8]; //存放顯示的數(shù)據(jù)
unsigned char dispcount; //顯示計(jì)數(shù)器值
unsigned char T0count; //T0的計(jì)數(shù)器值
unsigned char timecount; //計(jì)時(shí)計(jì)數(shù)器值
bit flag; //標(biāo)志位
unsigned long x; //頻率值
//頻率計(jì)算函數(shù)
void HzCal(void)
{
unsigned char i;
x=T0count*65536+TH0*256+TL0; //得到T0的16位計(jì)數(shù)器值
for(i=0;i<8;i++)
{
temp[ i]=0;
}
i=0;
while(x/10) //拆分
{
temp[ i]=x%10;
x=x/10;
i++;
}
temp[ i]=x;
for(i=0;i<6;i++) //換算為顯示數(shù)據(jù)
{
dispbuf[ i]=temp[ i];
}
timecount=0;
T0count=0;
}
void main(void)
{
TMOD=0x15; //設(shè)置定時(shí)器工作方式,T0計(jì)數(shù),T1定時(shí),均工作在方式1
TH0=0;
TL0=0;
TH1=(65536-5000)/256;
TL1=(65536-5000)%256; //初始化T1
TR1=1;
TR0=1;
ET0=1;
ET1=1;
EA=1; //開中斷
while(1)
{
if(flag==1)
{
flag=0;
HzCal(); //頻率計(jì)算函數(shù)
TH0=0;
TL0=0;
TR0=1;
}
}
}
//定時(shí)器T0中斷服務(wù)子函數(shù)
void t0(void) interrupt 1 using 0
{
T0count++;
}
//定時(shí)器T1中斷服務(wù)子函數(shù)
void t1(void) interrupt 3 using 0
{
TH1=(65536-5000)/256;
TL1=(65536-5000)%256; //初始化T1預(yù)裝值,1ms定時(shí)
timecount++; //掃描
if(timecount==200) //秒定時(shí)
{
TR0=0; //啟動(dòng)T0
timecount=0;
flag=1;
}
P2=0xff; //初始化選擇引腳
P0=dispcode[dispbuf[dispcount]]; //輸出待顯示數(shù)據(jù)
P2=dispbit[dispcount];
dispcount++; //切換到下一個(gè)選擇引腳
if(dispcount==8) //如果已經(jīng)掃描完成切換
{
dispcount=0;
}
|
|