****************************************************************
51单片机设计的4位不带小数点的频率计c语言程序
***************************************************/
#include < AT89X51.H>
#include < stdio.h>
int value=0,n=0;
int n,n1,n2,n3,n4;
int LEDData [] = { 0xC0, //"0"
0xF9, //"1"
0xA4, //"2"
0xB0, //"3"
0x99, //"4"
0x92, //"5"
0x82, //"6"
0xF8, //"7"
0x80, //"8"
0x90, //"9"
};
void delay(short i);
void initial(void);
void display(void);
//******************************************************
main ()
{
initial();
while(1)
{
if(n==100)
{
display();
n=0;
value=0;
}
}
}
//**********************************************************
void initial(void) //定时器计数器初始化
{
IE=0x8b;
TCON=0x01;
TMOD=0x11;
TH0=0xD8; //T0作为定时器初始化0.01秒中一次断
TL0=0xF0;
TH1=0XFF; //T1产生625HZ
TL1=0X60;
TR0=1;
TR1=1;
}
//***********************************************************
void delay(short i)
{
int j=0;
int k=0;
k=i*2;
while(j
}
//**************************************************************
void extern0_ISR () interrupt 0 //INT0外部脉冲中断
{
EX0=0;
value++;
EX0=1;
}
//************************************************************
void timer0_ISR () interrupt 1 //定时器每0.01秒中断一次T0
{
n++;
TH0=0xD8;
TL0=0xF0;
}
//**********************************************************
void timer1_ISR () interrupt 3 //T1625hz
{
P1=P1^0xc0;
TH1=0XFF; //T1产生3125HZ
TL1=0X60;
}
//*************************************************
void display(void)
{
n4=value/1000;
n3=(value-n4*1000)/100;
n2=(value-n4*1000-100*n3)/10;
n1=value-n4*1000-100*n3-10*n2;
while(1)
{
P2=0x3f;
P0=LEDData [n1];
delay(50);
P2=0x5f;
P0=LEDData [n2];
delay(50);
P2=0x6f;
P0=LEDData [n3];
delay(50);
P2=0xf7;
P0=LEDData [n4];
delay(50);
P2=0xff;
}
}