#include<reg52.h> #define uint unsigned int #define uchar unsigned char
sbit P10=P1^0; sbit P11=P1^1; //位碼口 sbit P12=P1^2; sbit P13=P1^3;
sbit P32=P3^2;
unsigned char code Duan[]={0x3F, 0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //對(duì)應(yīng)0---9 unsigned char Data_Buffer[4]={0,0,0,0}; //存放統(tǒng)計(jì)的數(shù)據(jù)。 單位為(100uS)
uint time=0;
void fenge() //分割數(shù)據(jù),以便于統(tǒng)計(jì)較大間隔時(shí)間。 { Data_Buffer[0]=time/1000; Data_Buffer[1]=time/100%10; Data_Buffer[2]=time/10%10; Data_Buffer[3]=time%10; }
void display() //用于四個(gè)數(shù)碼管顯示數(shù)據(jù) { uchar Bit=0; while(1) { Bit++; if(Bit>=4) Bit=0; P0=Duan[Data_Buffer[Bit]]; //數(shù)據(jù)的輸出 switch(Bit)//開(kāi)位碼 { case 0: P10=0;break; case 1: P11=0;break; case 2: P12=0;break; case 3: P13=0;break; } } }
void mian() { EX0=0; EA=1; IT0=0;//低電平有效
TMOD=0x02; //設(shè)置為自動(dòng)重載方式 TH0=0x92; //設(shè)置為100uS的計(jì)數(shù) TL0=0x92; TR0=0;
while(1) { fenge(); display(); time=0; //當(dāng)前顯示完畢后,清當(dāng)前time的值 EX0=1; //開(kāi)啟外部中斷 }
}
void zhongduan() interrupt 0 { TR0=1; if(TH0==1) //100uS定時(shí)器溢出 { time++; //統(tǒng)計(jì)兩個(gè)脈沖的時(shí)間 TH0=0; } if(P32==1) //如果外部中斷變?yōu)楦唠娖,關(guān)閉中斷。保證統(tǒng)計(jì)的是兩個(gè)脈沖的間隔時(shí)間。為主函數(shù)顯示數(shù)據(jù)爭(zhēng)取時(shí)間。 EX0=0;
}
有點(diǎn)問(wèn)題,誰(shuí)能幫我改改嗎! |