void IntToStr(unsigned int t, unsigned char *str, unsigned char n)
{
unsigned char a[5]; char i, j;
a[0]=(t/10000)%10; //取得整數值到數組
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++) //轉成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++; } //加入有效的數字
*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) //如果定時時間到了1s
{
flag=0; //標志位清零
x=T0count*65536+TH0*256+TL0; //獲得整型的頻率值,T0count計數器在1s內溢出的次數,每溢出一次就計數了T0count*65536次 _ // 再加上當前計數寄存器的值即為實際計數總數
IntToStr(x, &TempBuffer[0], 5);
while(TempBuffer != '\0')
{
write_date(TempBuffer);
i++;
delay(200); //延時200ms
}
write_com(0x80+0x48);
timecount=0;
T0count=0;
TH0=0;
TL0=0;
TR0=1;
TR1=1;
i = 0;
}
}
}
這個是我寫的一個關于51單片機的一個計數器程序。
最高可以計數到65536,但是到1KHz或者更大的時候有誤差100多,當頻率越高誤差越大。
請問一下各位,這個誤差是出在哪些地方的,通過改進哪里可以提高誤差的,希望高手能指點一下
謝謝了
|