lu50211 发表于 2014-7-28 08:47
用外部中断加定时器会比较准确。。。。
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit P30=P3^0;
/*共阴数码管段码表*/
uchar code smgd[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
/***数码管位选表***/
uchar code smgw[]=
{0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uint count=0,n=0,prd=0;
uint read()
{
uchar tl1,th1;
uint t1;
tl1=TL1;
TL1=0;
th1=TH1;
TH1=0;
t1=th1*256+tl1;
return t1;
}
void delayms(uint t)
{
uint x,y;
for(x=t;x>0;x--)
{
for(y=114;y>1;y--);
}
}
void T0timer() interrupt 1
{
n=n+1;
if(n==10000)
{
n=0;
count=read();
prd=2000000/count;
}
}
void smgdisplay(uint smgdata)
{
uchar s0,s1,s2,s3,s4;
s4=smgdata/10000;
if(s3>15)
{
s3=15;
}
s3=smgdata%10000/1000;
s2=smgdata%10000%1000/100;
s1=smgdata%10000%1000%100/10;
s0=smgdata/10;
P0=smgw[0];
wela=1;
wela=0;
P0=smgd[s0];
dula=1;
dula=0;
delayms(5);
P0=smgw[1];
wela=1;
wela=0;
P0=smgd[s1];
dula=1;
dula=0;
delayms(5);
P0=smgw[2];
wela=1;
wela=0;
P0=smgd[s2];
dula=1;
dula=0;
delayms(5);
P0=smgw[3];
wela=1;
wela=0;
P0=smgd[s3];
dula=1;
dula=0;
delayms(5);
P0=smgw[4];
wela=1;
wela=0;
P0=smgd[s4];
dula=1;
dula=0;
delayms(5);
}
void EXint0() interrupt 0 //外部中断0
{
ET0=1;
}
void EXint1() interrupt 2 //外部中断1
{
ET0=0;
prd=0;
}
void main()
{
P30=0;
EA=1;
EX0=1;
IT0=1;
EX1=1;
IT1=1;
ET0=1;
ET1=1;
TMOD=0x52;
TH0=56;
TL0=56;
TH1=0;
TL1=0;
TR0=1;
TR1=1;
while(1)
{
smgdisplay(prd);
}
}
能帮我看一下这段程序吗,我用这段程序来测量时只有一个值总是没有什么变化。
|