51 microcontroller to make digital frequency meter

Introduction

A digital frequency meter is an instrument that can measure the frequency of periodically changing signals. Traditional frequency meters are usually implemented with a lot of logic circuits and sequential circuits. Such circuits generally run slowly and have a small range of measured frequencies. This article introduces the microcontroller STC89C52 as the core, by counting the input pulses, using the calculation and control functions of the microcontroller and using a digital tube to display the measured frequency. The software is programmed in C language, uses a timing counter to measure the frequency, and then adjusts the display function to display the measured results on the digital tube. The system is simple, reliable and easy to operate, and can basically meet the needs under normal circumstances. This not only ensures the frequency measurement accuracy of the system, but also makes the system have better real-time performance.

Design ideas

This design is mainly divided into two major aspects: hardware circuit design and software program design. In terms of hardware circuit, the minimum system using STC89C52 microcontroller can achieve the requirements. In terms of program design, C language is used to write the program. Its overall block diagram is shown in Figure 1:
Insert image description here

working principle

This digital frequency meter uses the P3.4 (T0) pin of the microcontroller as the input terminal of the measured rectangular wave signal, and the microcontroller crystal oscillator FOSC=12MHZ. When the external pulse signal, that is, the measured rectangular wave signal enters the microcontroller from P3.4, Start timer T0 and counter T1 at the same time. T0 works in the counting state and counts the input frequency signal. The maximum counting value of T0 working in the counting state is 65535. Then: the maximum counting frequency of T0 is 65535Hz, and T1 is When working in the timing state, T1 stops counting every 1 second, and the count value read from the counting unit of T1 is sent to the digital display tube for display after data processing, because T1 works in the timing state. The maximum timing time is 65ms, which cannot reach 1 second timing, so using 50ms and timing 20 times in total can complete the 1 second timing function.

Proteus software simulation

As shown in the figure, it is the simulation of a digital frequency meter in the proteus software.
Insert image description here
Input the pulse to be measured into the P3.4 pin of the microcontroller.
Insert image description here

software program

 #include "reg52.h"
sbit L1 = P1^0;
sbit S1 = P3^2;
unsigned char code SMG_duanma[18]=
		{
    
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
     0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,
     0xbf,0x7f};
unsigned char count = 0;

unsigned int frequency = 0;
unsigned char start=1;
unsigned char flag = 0;
void InitTimer()	  
{
    
    
	TMOD = 0x15;  //T1定时,T0计数,
	TH1 = (65535 - 50000) / 256;
	TL1 = (65535 - 50000) % 256;

	TH0 = 0x00;
	TL0 = 0x00;
	
	ET1 = 1;
	ET0 = 1;
	EA = 1;
	TR1 = 1;
	TR0 = 1;
	
}

unsigned int i = 0;
void ServiceTimer1() interrupt 3
{
    
    
	
	TH1 = (65535 - 50000) / 256;
	TL1 = (65535 - 50000) % 256;

    i++;  

	 if(i==20)
	{
    
    
		i = 0;
		
		TR0=0; //停止计数
	    TR1=0; //停止定时
		frequency=(TH0*256+TL0); //求出频率值 就是1秒内脉冲次数
		TH0=0x00; //计数值清零
		TL0=0x00;
	
		
		TH1 = (65535 - 50000) / 256;
		TL1 = (65535 - 50000) % 256;
		start=1; //启动定时器开启变量
	
	}
}
   void Init_INT0()
{
    
    
	IT0 = 1;
	EX0 = 1;
	EA = 1;
}
					   

void ServiceINT0() interrupt 0
{
    
    	
	if(flag == 0)
	{
    
    	
		
		frequency = 0;
		
	}
	if(flag!=0)
		frequency++;

	flag = 1;
	

}

void DisplaySMG_Bit(unsigned char value, unsigned char pos)
{
    
    
	P0 = 0xff;
 	P2 = 0x01 << pos; 
	P0 = value;
}

void DelaySMG(unsigned int t)
{
    
    
	while(t--);
}

void Display_Dynamic()
{
    
    
	DisplaySMG_Bit(SMG_duanma[frequency/100000],0);	       
	DelaySMG(500);
	DisplaySMG_Bit(SMG_duanma[frequency%100000/10000],1);		 
	DelaySMG(500);
	DisplaySMG_Bit(SMG_duanma[frequency%10000/1000],2);			 
	DelaySMG(500);
	DisplaySMG_Bit(SMG_duanma[frequency%1000/100],3);	
	DelaySMG(500);
	DisplaySMG_Bit(SMG_duanma[frequency%100/10],4);			 
	DelaySMG(500);
	DisplaySMG_Bit(SMG_duanma[frequency%100%10],5);	
	DelaySMG(500);

}

void Delay(unsigned char t)
{
    
    
		while(t--)
		{
    
    
			Display_Dynamic();
		}
}

void DelayK(unsigned char t)
{
    
    
	while(t--);
}

void ScanKeys_Alone()
{
    
    
	if(S1 == 0)
	{
    
    
		DelayK(100);
		if(S1 == 0)
		{
    
    
			TR0=0; //停止计数
	   		TR1=0; //停止定时
			if(flag == 0)
			{
    
    
					frequency = 0;
			}
			if(flag!=0)
				frequency++;
			flag = 1;
			
			while(!S1);
		
		}
	}
}
void main()
{
    
    	
	InitTimer();
	Init_INT0();
	while(1)
	{
    
     	 
		ScanKeys_Alone();
	if(start==1)
		{
    
    
			TR0=1; //启动定时器
			TR1=1; //启动计数器
			start=0; //关闭启动变量位 保证1秒时间
		}
     Display_Dynamic();
	 Delay(200);

		if(flag == 1)				                      
		{
    
    
			start = 0;
		}
     }
}

Experimental phenomena

Double-click the device DCLOCK to set the frequency of the external input rectangular pulse:
50hz:
Insert image description here
Insert image description here
100hz:
Insert image description here

Insert image description here
500hz:
Insert image description here
Insert image description here
1000hz:
Insert image description here
Insert image description here

Measurement error and range

When measuring frequency, when starting the timing counter, if the rectangular wave input from T0 (P3.4) happens to be high level, and when the 1s timing expires, it happens to be high level, the frequency value measured at this time is the most accurate. If the input rectangular wave is at a low level when the timer counter is started, and when the 1s timer expires, the rectangular wave is about to transition negatively, the frequency error measured at this time is the largest. The selection of the working mode of the timing counter and the assignment of the initial value are not necessarily accurate and can easily cause errors. Depending on how the timing counter works, the final result will be different. The error of working mode 2 is smaller than that of working mode 0 and working mode 1. Secondly, the use of interruption or query will also affect the experimental results. The error using the query method is smaller than the error using the interrupt method.

The electronic counter frequency measurement method mainly adds the measured frequency signal to the counting input end of the counter, and then allows the counter to count within the standard time Ts1 to obtain the count value N1. The relationship with the frequency fx1 of the measured signal is as follows:
Insert image description here
The main error source is the ±1 error caused by the counter only being able to count integers:
Insert image description here
the maximum count value of the 16-bit counter T0 working in the counting state is 65535, which can theoretically be measured The frequency range is 0-65535hz, the maximum actual simulation test is 65.5KHz, the measurement display value is 65530hz, and the error is 0.04%.
Insert image description here
After testing, the digital frequency meter can work normally within a certain error tolerance and measurement range. The figure below is a design reference for the schematic diagram of a digital frequency meter based on the above solution. The following functions can be achieved:
(1) Input the external rectangular pulse into the T0 pin, that is, connect the external input pulse to the P3.4 pin with a wire, and it can be used as an external pulse input digital frequency meter.
(2) Extended function: When the button is pressed, it stops counting external rectangular pulses and changes to single pulse counting. When the button is pressed once, the count value increases by 1 and is displayed on the digital tube. (PS: LED can be used as other function extensions).

Insert image description here

Summarize

Frequency measurement can be implemented using digital logic circuits or controlled by a microcontroller. The former not only implements a complex circuit, but also has a smaller range of measurement frequency. The timer of the microcontroller can be used to easily measure the signal frequency. You only need to write a program on the computer and then display it on the corresponding display circuit. , you can use the timer, counter timing and counting functions of the STC89C51 microcontroller, externally expand the 6-bit LED digital tube, accumulate the number of external pulses entering the microcontroller per second, and display it with the LED digital tube, realizing the production of a digital frequency meter based on the microcontroller. .

Guess you like

Origin blog.csdn.net/qq_42250136/article/details/134676313