电子测量 频率计实验.doc
文本预览下载声明
实验名称:频率与周期的数字测量
实验目的:掌握频率与周期的测量数字方法
了解频率计的基本结构与设计方法
用51单片机设计的简易频率电路图
实验主程序:
#include reg51.h
#include LCD1602.h
sbit CLR = P3^0;
sbit FGT = P3^1;
sbit f = P3^2;
unsigned char TempBuffer[12];
void IntToStr(unsigned int t, unsigned char *str, unsigned char n)
{
unsigned char a[5]; char i, j;
a[0]=(t/10000)%10; //取得整数值到数组
a[1]=(t/1000)%10;
a[2]=(t/100)%10;
a[3]=(t/10)%10;
a[4]=(t/1)%10;
for(i=0; i5; i++) //转成ASCII码
a[i]=a[i]+0;
for(i=0; a[i]==0 i=3; i++);
for(j=5-n; ji; j++) //填充空格
{ *str= ; str++; }
for(; i5; i++) { *str=a[i]; str++; } //加入有效的数字
*str=\0;
}
void Delay1ms(unsigned int count)
{
unsigned int i,j;
for(i=0;icount;i++)
for(j=0;j120;j++);
}
unsigned int Count = 0;
long Freq = 0;
void cn() interrupt 3 //计数器中断,测量闸门时间内的计数值
{
Count++;
}
void Timer() interrupt 1 //定时中断,产生闸门时间
{
static unsigned char temp = 0;
TH0 = 0x3c;
TL0 = 0xe3;
Freq--;
if (++temp==10)
{
FGT = 0; //关闭闸门
Freq = 65536L*256L*Count + 256L*(TH1*256+TL1)+P1; //频率计算
Freq *=2;
TH1 = TL1= 0;
Count =0;
temp =0;
CLR = 1;
CLR = 0;
FGT = 1; //打开闸门
TH0 = 0x3c;
TL0 = 0xb8;
}
}
void ini() //定时计数器初始化程序
{
TMOD = 0x51;
TH0 = 0x3c;
TL0 = 0;
TH1 = 0;
TL1 = 0;
TR1 = 1;
ET1 = 1;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void main()
{
LCD_Initial(); //液晶显示初始化
GotoXY(0,0);
Print(The 1602LCD Test);
GotoXY(0,1);
Print(Freq: Hz);
ini();
CLR=0;
while(1)
{
IntToStr(Freq,TempBuffer[0],5); //显示数据转换
GotoXY(5,1);
Print(TempBuffer[0]); / /显示频率
Delay1ms(100);
}
}
显示全部