本帖最后由 kangdawa 于 2018-3-19 00:50 编辑
本人新手,尝试用51单片机编一个时间显示,郭天祥的板子,开始用定时器工作方式1编写,因为程序执行误差,不太准确。然后改为定时器工作方式2,本以为很正确,可是跑了大概十小时出头后出现3s误差(和电脑比较)。困惑?是晶振不稳定么?
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
uint n;
long t;
uchar H,M,S,Hh,Hl,Mh,Ml,Sh,Sl,temp,num;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code weixuan[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
void delayms(uint);
void delayms(uint xms)
{
int i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void time0() interrupt 1
{
n++;
if(n==3600)
{
n=0;
t++;
P1=~P1;
}
}
void display()
{
S=t%60;
Sl=S%10;
Sh=S/10;
M=(t/60)%60;
Ml=M%10;
Mh=M/10;
H=t/3600;
Hl=H%10;
Hh=H/10;
wela=1;
P0=weixuan[0];
wela=0;
P0=0xff;
dula=1;
P0=table[Hh];
dula=0;
P0=0xff;
delayms(1);
wela=1;
P0=weixuan[1];
wela=0;
P0=0xff;
dula=1;
P0=table[Hl];
dula=0;
P0=0xff;
delayms(1);
wela=1;
P0=weixuan[2];
wela=0;
P0=0xff;
dula=1;
P0=table[Mh];
dula=0;
P0=0xff;
delayms(1);
wela=1;
P0=weixuan[3];
wela=0;
P0=0xff;
dula=1;
P0=table[Ml];
dula=0;
P0=0xff;
delayms(1);
wela=1;
P0=weixuan[4];
wela=0;
P0=0xff;
dula=1;
P0=table[Sh];
dula=0;
P0=0xff;
delayms(1);
wela=1;
P0=weixuan[5];
wela=0;
P0=0xff;
dula=1;
P0=table[Sl];
dula=0;
P0=0xff;
delayms(1);
}
void main()
{
t=50580+120;
TMOD=0x02;
TH0=0;
TL0=0;
EA=1;
ET0=1;
TR0=1;
P1=0xaa;
while(1)
{
display();
if(t==86400)
{
t=0;
};
};
}
|
先用频率计测量你的基准没问题再说。你可以在定时中断之中设置P口翻转,这种方法测量频率不影响振荡槽路