51单片机做的数字频率计,源程序和仿真图请从下面的链接下载:
http://www.51hei.com/f/51数字频率计.rar
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
unsigned char code dispbit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//共阴数码管
unsigned char code dispcode[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//显示0-9
unsigned char count[8]={0,0,0,0,0,0,0,0};
unsigned int T0count=0;
unsigned char time1tick=0;
unsigned char i=0;
unsigned long x;
void pulse_init(void)
{
TMOD=0x16;//T0工作在方式2,计数器模式
TH0=0x00;
TL0=0x00;
TH1=0xb1;//20ms中断一次
TL1=0xe0;
ET0=1;
ET1=1;
EA=1;
}
void delay(uint ms)
{
unsigned char i,j;
for(i=0;i<ms;i++)
for(j=0;j<250;j++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
static void time0_int(void) interrupt 1 using 0
{
T0count++; //T0为计数器模式
}
void t1(void) interrupt 3 using 0
{
time1tick++;
TH1=0xb1;//20ms中断一次
TL1=0xe0;
if(time1tick==50)//定时1s
{
time1tick=0;
EA=0;
TR0=0;
TR1=0;
}
}
void display(void)
{
x=256*T0count+TL0-4; //转换成10进制
x=x/17.3;
i=7;//数码管的显示高位表示数据的低位
while(x)//赋值
{
count[i]=x%10;
x=x/10;
i--;
}
count[i]=x;
for(i=0;i<8;i++)
{
P1=dispbit[i];
P0=dispcode[count[i]];
delay(1);
}
}
void main()
{
pulse_init();
TR1=1;//开启timer
TR0=1;
while(1)display();
}