|
#include <AT89X52.H>
unsigned char code dispbit[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //P2的掃描位
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40}; //數(shù)碼管的字形編碼
unsigned char dispbuf[8]={0,0,0,0,0,0,10,10}; //初始化顯示值
unsigned char temp[8]; //存放顯示的數(shù)據(jù)
unsigned char dispcount; //顯示計(jì)數(shù)器值
unsigned char T0count; //T0的計(jì)數(shù)器值
unsigned char timecount; //計(jì)時(shí)計(jì)數(shù)器值
bit flag; //標(biāo)志位
unsigned long x; //頻率值
//頻率計(jì)算函數(shù)
void HzCal(void)
{
unsigned char i;
x=T0count*65536+TH0*256+TL0; //得到T0的16位計(jì)數(shù)器值
for(i=0;i<8;i++)
{
temp[i]=0;
}
i=0;
while(x/10) //拆分
{
temp[i]=x%10;
x=x/10;
i++;
}
temp[i]=x;
for(i=0;i<6;i++) //換算為顯示數(shù)據(jù)
{
dispbuf[i]=temp[i];
}
timecount=0;
T0count=0;
}
void main(void)
{
TMOD=0x15; //設(shè)置定時(shí)器工作方式
TH0=0;
TL0=0;
TH1=(65536-5000)/256;
TL1=(65536-5000)%256; //初始化T1
TR1=1;
TR0=1;
ET0=1;
ET1=1;
EA=1; //開(kāi)中斷
while(1)
{
if(flag==1)
{
flag=0;
HzCal(); //頻率計(jì)算函數(shù)
TH0=0;
TL0=0;
TR0=1;
}
}
}
//定時(shí)器T0中斷服務(wù)子函數(shù)
void t0(void) interrupt 1 using 0
{
T0count++;
}
//定時(shí)器T1中斷服務(wù)子函數(shù)
void t1(void) interrupt 3 using 0
{
TH1=(65536-5000)/256;
TL1=(65536-5000)%256; //初始化T1預(yù)裝值,1ms定時(shí)
timecount++; //掃描
if(timecount==200) //秒定時(shí)
{
TR0=0; //啟動(dòng)T0
timecount=0;
flag=1;
}
P2=0xff; //初始化選擇引腳
P0=dispcode[dispbuf[dispcount]]; //輸出待顯示數(shù)據(jù)
P2=dispbit[dispcount];
dispcount++; //切換到下一個(gè)選擇引腳
if(dispcount==8) //如果已經(jīng)掃描完成切換
{
dispcount=0;
}
}
復(fù)
|
|