/* 单片机测频 程序 下到 20hz 上到 29k */
/*如果想要测更高频率可用模拟开关加分频器*/
/*说明: 头文件中调用的“lcd1602.h”只需要把我发的lcd1602的子程序做一个h文件即可*/
#i nclude "AT89X52.h"
#i nclude "stdio.h"
#i nclude " absacc.h "
#i nclude
#i nclude
#i nclude "lcd1602.h"
sbit F_in=P1^0;
bit int_flag;
unsigned int frequency;
void Display(void);
void Mcu_init(void);
void Measure_F(void);
void Measure_T(void);
void Frequency_Measure(void);
void main()
{
Mcu_init();
while(1)
{
Frequency_Measure();
Display();
}
}
void time0(void) interrupt 1
{
int_flag=1;
}
void Frequency_Measure(void)
{
if(frequency<2000)
Measure_T();
else if(frequency>=2000)
Measure_F();
}
void Measure_F(void)
{
bit state_old=0,state_new=0;
unsigned int f_count=0;
int_flag=0;
TH0=-50000/256;
TL0=-50000%256;
TR0=1;
while(1)
{
state_new=F_in;
if(state_old!=state_new)
f_count++;
state_old=state_new;
if(int_flag)
{
TR0=0;
frequency=f_count*10;
return;
}
}
}
void Measure_T(void)
{
bit state_old=0,state_new=0;
unsigned char temp_flag=0;
unsigned int t_count;
TH0=0;
TL0=0;
while(1)
{
state_new=F_in;
if(state_old&&!state_new)
{
temp_flag++;
TR0=1;
}
if(temp_flag==2)
{
TR0=0;
t_count=TH0*256+TL0;
frequency=1000000/t_count;
return;
}
state_old=state_new;
}
}
void Display(void)
{
unsigned char a,b,c,d,e;
a=frequency/10000;
b=frequency%10000/1000;
c=frequency%1000/100;
d=frequency%100/10;
e=frequency%10;
LCD_string(LINE1,0,"F-measure:");
LCD_char(LINE1,0x0b,a 0x30);
LCD_char(LINE1,0x0c,b 0x30);
LCD_char(LINE1,0x0d,c 0x30);
LCD_char(LINE1,0x0e,d 0x30);
LCD_char(LINE1,0x0f,e 0x30);
if(frequency<2000)
LCD_string(LINE2,0,"Now is measure T");
else
LCD_string(LINE2,0,"Now is measure F");
}
void Mcu_init(void)
{
EA=1;
ET0=1;
TR0=1;
TMOD=0x01;
TH0=-(50000/256);
TL0=-(50000%256);
LCD_init();
LCD_string(LINE1,0x00," ");
LCD_string(LINE1,0x00," ");
}
|