51單片機(jī)數(shù)字頻率計(jì)-帶仿真文件
http://www.51hei.com/f/51數(shù)字頻率計(jì).rar
本文引用地址:http://m.butianyuan.cn/article/201611/323392.htm#include
#include
#define uint unsigned int
#define uchar unsigned char
unsigned char code dispbit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//共陰數(shù)碼管
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,計(jì)數(shù)器模式
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
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
static void time0_int(void) interrupt 1 using 0
{
T0count++;//T0為計(jì)數(shù)器模式
}
void t1(void) interrupt 3 using 0
{
time1tick++;
TH1=0xb1;//20ms中斷一次
TL1=0xe0;
if(time1tick==50)//定時(shí)1s
{
time1tick=0;
EA=0;
TR0=0;
TR1=0;
}
}
void display(void)
{
x=256*T0count+TL0-4;//轉(zhuǎn)換成10進(jìn)制
x=x/17.3;
i=7;//數(shù)碼管的顯示高位表示數(shù)據(jù)的低位
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;//開(kāi)啟timer
TR0=1;
while(1)display();
}
評(píng)論