Principle of single chip microcomputer frequency measurement

Publisher:a407895356Latest update time:2016-09-01 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
In the single-chip microcomputer application system, it is often necessary to measure the frequency of a continuous pulse wave. In practical applications, the measurement of physical quantities such as rotation speed, displacement, speed, flow, etc. is generally converted into a pulse electrical signal by a sensor and realized by measuring the frequency.

The frequency or period measurement using a single-chip microcomputer is usually completed by using the timing counter of the single-chip microcomputer. There are two basic methods and principles of measurement:

Frequency measurement method: detect the number of pulses within a limited time (such as 1 second).

Period measurement method: Test the time between a limited number of pulses.

Although the principles of these two methods are the same, in actual use, it is necessary to make comprehensive and specific considerations based on factors such as the frequency range to be measured, the system clock cycle, the length of the counter, and the required measurement accuracy, in order to find and design a measurement method that suits specific requirements.

In the measurement of specific frequencies, the following factors need to be considered and noted.

ü System clock. First of all, the system clock used to measure the frequency must be accurate, because no matter it is to limit the measurement time or the period of the limited number of pulses, the basic time reference is generated by the system clock itself. Secondly, the frequency value of the system clock, because the higher the system clock frequency, the higher the accuracy of the frequency measurement. Therefore, when using AVR to measure the frequency, it is recommended to use the system oscillator circuit composed of an external crystal instead of its internal RC oscillator source, and try to use a system clock with a higher frequency.

ü The number of bits of the timing counter used. The timing counter is used to measure the frequency. The longer the number of bits of the timing counter, the longer the limited time that can be generated, or the more pulses that can be recorded in the limited time, thus improving the accuracy of the frequency measurement. Therefore, when there are certain requirements for the frequency measurement accuracy, try to use a 16-bit timing counter.

ü The range of the measured frequency. The frequency measurement needs to select the measurement method according to the range of the measured frequency. When the measured frequency range is relatively low, it is best to use the period measurement method to measure the frequency. When the measured frequency is relatively high, it is more appropriate to use the frequency measurement method. It should be noted that the maximum value of the measured frequency generally cannot exceed 1/2 of the MCU system clock frequency of the frequency measurement, because when the measured frequency is higher than 1/2 of the MCU clock, the MCU often cannot correctly detect the level change of the measured pulse.

In addition to the above three factors, we also need to consider the frequency of frequency measurement (the number of measurements per second), how to coordinate with other tasks in the system, etc. When the frequency measurement accuracy is high, we should also consider the impact of other interrupts and interrupt response time, and even consider using an algorithm for averaging multiple measurements in the software.

 

 

Design and implementation of frequency meter using frequency measurement method

1) Hardware circuit

 

In the display part of the hardware circuit, the PA port is the segment output of the 8 LED digital tubes, and the PC port controls the bit scanning of the 8 LED digital tubes. T/C0 is used to count the number of pulses of the measured signal input, and the measured frequency signal is input by PB0 (T0).

2) Software Design

We first give the system program and then make necessary explanations.

/************************************************

File name: demo_11_1.c

Chip type: ATmega16

Program type: Application

Clock frequency: 4.000000 MHz

Memory model: Small

External SRAM size: 0

Data Stack size: 256

*********************************************/

 

#include

flash char led_7[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

flash char position[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};

 

char dis_buff[8]; // Display buffer, storing the segment code value of 8 characters to be displayed

char position;

bit time_1ms_ok,display_ok=0;

char time0_old,time0_new,freq_time;

unsigned int freq;

 

void display(void) // 8-bit LED digital tube dynamic scanning function

{

        PORTC = 0xff;

        PORTA = led_7[dis_buff[posit]];

        if (posit==5) PORTA = PORTA | 0x80;

        PORTC = position[posit];

        if (++posit >=8 ) posit = 0;

}

 

// Timer 2 output compare interrupt service routine

interrupt [TIM2_COMP] void timer2_comp_isr(void)

{

        time0_new = TCNT0; // 1ms arrives, record the current count value of T/C0

        time_1ms_ok = 1;

        display_ok = ~display_ok;

        if (display_ok) display();

}

 

void freq_to_disbuff(void) //Convert the frequency value into BCD code and send it to the display buffer

{

        char i,j=7;

        for (i=0;i<=4;i++)

        {

            dis_buff[ji] = freq % 10;

            freq = freq / 10;

        }

        dis_buff[2] = freq;

}

 

void main(void)

{

        char i;

DDRA=0xFF; //LED digital tube driver

DDRC=0xFF;

            // T/C0 initialization, external counting mode

TCCR0=0x06; // External T0 pin falling edge triggers counting, normal mode

TCNT0=0x00;

OCR0=0x00;

            //T/C2 initialization

TCCR2=0x0B; // Internal clock, 32-division (4M/32=125KHz), CTC mode

TCNT2=0x00;

OCR2=0x7C; // OCR2 = 0x7C(124),(124+1)/125=1ms

 

TIMSK=0x80; // Enable T/C2 compare match interrupt

 

for (i=0;i<=7;i++) dis_buff[i] = 0;

time0_old = 0;

 

#asm("sei") // Enable global interrupt

 

while (1)

        {

        if (time_1ms_ok)

        { // Accumulate the count value of T/C0

             if (time0_new >= time0_old) freq = freq + (time0_new - time0_old);

             else freq = freq + (256 - time0_old + time0_new);

             time0_old = time0_new;

             if (++freq_time >= 100)

             {

                 freq_time = 0; // 100ms arrives,

                 freq_to_disbuff(); // Send the pulse count value within 100ms to the display

                 freq = 0;

             }

             time_1ms_ok = 0;

        }

        };

}

The LED scanning function desplay() and the pulse count value converted into BCD code and sent to the display buffer function freq_to_disbuff() in the program are relatively simple. Please analyze them by yourself.

In this program, two timer counters are used. T/C0 works in counter mode and counts the pulse signal input from the external T0 pin (falling edge trigger). T/C2 works in CTC mode and interrupts every 1ms. This timer is used as the display scan of the LED and is also the base time for the limited time. Every time T/C2 interrupts, the current count value of the T/C0 register TCNT0 is first recorded. Therefore, the difference between the two TCNT0s (time0_new – time0_old) or (256 - time0_old + time0_new) is the number of pulses input from the T0 pin within 1ms. In order to improve the measurement accuracy, the program accumulates the number of 100 1ms pulses (in the variable freq), that is, it is known that the limited time is 100ms.

Readers should also pay attention to the coordination between the continuous measurement of frequency and the LED scanning and BCD code conversion. The T/C2 interrupt interval is 1ms, so within 1ms, the program must complete the accumulation of the number of pulses, BCD code conversion and sending to the display buffer, as well as the LED scanning work, otherwise it will affect the processing after the next interrupt arrives.

In the T/C2 interrupt of this example, the display_ok flag is used to assign the LED scan to odd ms (1, 3, 5, 7, ...), and the 1ms TCNT0 difference calculation, accumulation and conversion are completed in the main program. In addition, since the BCD code conversion with a large amount of calculation is processed in even ms (100ms), the LED scan processing and BCD code conversion processing in the program will not be performed at the same time (LED scan and BCD code conversion will not be processed simultaneously within 1ms between two interrupts). This ensures that when the next interrupt arrives, the previous processing has been completed, so that the continuous measurement of frequency is not affected.

The performance and indicators of the example program are (assuming that the system clock has no error = 4MHz):

ü Absolute frequency measurement error: ±10Hz. Since the time limit is 100ms and the count value of T/C0 has an error of ±1, the frequency converted is ±10Hz.

ü The highest frequency value measured: 255KHz. Since the length of T/C0 is 8 bits, the number of pulses input by TO should be less than 255 in 1ms. If it is greater than 255, T/C0 will be automatically cleared and the number of pulses will be lost.

ü Measurement frequency: 10 times/second. The limited time is 100ms, continuous measurement, so 10 times/second.

ü Resources used: two timers and one interrupt.

Reference address:Principle of single chip microcomputer frequency measurement

Previous article:How to calculate the delay time of single chip microcomputer
Next article:MCU clock and oscillation source

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号