void IntToStr(unsigned int t, unsigned char *str, unsigned char n)
{
unsigned char a[5]; char i, j;
a[0]=(t/10000)%10; //取得整數(shù)值到數(shù)組
a[1]=(t/1000)%10;
a[2]=(t/100)%10;
a[3]=(t/10)%10;
a[4]=(t/1)%10;
for(i=0; i<5; i++) //轉(zhuǎn)成ASCII碼
a=a+'0';
for(i=0; a=='0' && i<=3; i++);
for(j=5-n; j<i; j++) //填充空格
{ *str=' '; str++; }
for(; i<5; i++)
{ *str=a; str++; } //加入有效的數(shù)字
*str='\0';
}
//count interrupt
void t0(void) interrupt 1 using 0
{
T0count++;
TH0=0;
TL0=0;
}
//time interrupt
void t1(void) interrupt 3 using 0
{
TH1=0x5d;
TL1=0x3d;
timecount++;
if(timecount==40) //1秒
flag=1;
}
main()
{
unsigned char i;
init();
while(1)
{
if(flag==1) //如果定時(shí)時(shí)間到了1s
{
flag=0; //標(biāo)志位清零
x=T0count*65536+TH0*256+TL0; //獲得整型的頻率值,T0count計(jì)數(shù)器在1s內(nèi)溢出的次數(shù),每溢出一次就計(jì)數(shù)了T0count*65536次 _ // 再加上當(dāng)前計(jì)數(shù)寄存器的值即為實(shí)際計(jì)數(shù)總數(shù)
IntToStr(x, &TempBuffer[0], 5);
while(TempBuffer != '\0')
{
write_date(TempBuffer);
i++;
delay(200); //延時(shí)200ms
}
write_com(0x80+0x48);
timecount=0;
T0count=0;
TH0=0;
TL0=0;
TR0=1;
TR1=1;
i = 0;
}
}
}
這個(gè)是我寫的一個(gè)關(guān)于51單片機(jī)的一個(gè)計(jì)數(shù)器程序。
最高可以計(jì)數(shù)到65536,但是到1KHz或者更大的時(shí)候有誤差100多,當(dāng)頻率越高誤差越大。
請(qǐng)問一下各位,這個(gè)誤差是出在哪些地方的,通過改進(jìn)哪里可以提高誤差的,希望高手能指點(diǎn)一下
謝謝了
|