Digital frequency meter made with ATmega8 microcontroller

Publisher:baiyuguojiLatest update time:2020-03-10 Source: 21icKeywords:ATmega8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction: This article introduces the relevant functions of T/CO and T/C1 based on the relevant knowledge used in case programming. Readers can deepen their understanding by referring to the relevant content of the supporting program when studying.


Frequency refers to the number of times a periodic signal changes within a unit of time (1s). The instrument that measures frequency is called a frequency meter. The experiment in this lecture mainly introduces how to use ATmega8 and LCD1602 liquid crystal display to form a digital frequency meter. The frequency measurement range is 1Hz~4MHz. Through experiments and learning, everyone can master the functions and basic usage of ATmega8's timer/counter.


1. Frequency measurement principle


There are two methods for frequency measurement: counting method and measuring cycle method. The counting method is to count the number of times N that the signal changes repeatedly within a certain time interval T. The frequency can be calculated according to the formula f=N/T. For convenience, the time interval T is usually taken as 1s, so the result of the counting is the frequency; the measuring cycle method is to first measure the period of the signal, and then calculate the frequency according to the formula f=1/T. The above two measurement methods have their own advantages and disadvantages. In order to improve the measurement accuracy, the counting method is generally used when the signal frequency is high, and when the frequency is low, the counting method has an error of ±1, which will cause a large relative error. Therefore, the measuring cycle method must be used in this case. For simplicity, this article only introduces the counting method.


2. ATmega8 Timer/Counter


The ATmega8 microcontroller has three timers/counters: two 8-bit T/C0 and T/C2, and one 16-bit T/C1. Unlike the 51 series microcontrollers, these timers/counters have functions of capture, comparison, PWM output, and real-time clock counting in addition to the usual timing and counting functions. In this experiment, only the counting and timing functions of T/CO and T/C1 are used.


The following is an introduction to the functions of T/CO and T/C1 based on the relevant knowledge needed in case programming. Readers can deepen their understanding by referring to the relevant content of the supporting program when studying.


1.8-bit timer/counter T/C0


Both T/CO and T/C1 can independently divide the MCU clock frequency through the prescaler to obtain the clock source, or input the clock source from an external pin. The prescaler structure is shown in the figure below.

Prescaler Structure

(1) T/C0 control register TCCR0


The definition of TCCR0 is shown in the table below.

Bit 7~3 2 1 0 TCCR0 CS02 CS01 CS00

The three flags CS02 to CS00 in TCCR0 are used to select the clock source, that is, to determine the prescaler division coefficient. The default value is 0. The definition is shown in the table below. When an external clock source is selected, the change in the logic level on the TO pin will drive the T/C0 count. This feature allows the user to control the count through software.

0 0 0 No clock source (stop T/CO) 0 0 1 CK (system clock) 0 1 0 CK/8 0 1 1 CK/64 1 0 0 CK/256 1 0 1 CK/1024 1 1 0 External TO pin, falling edge trigger 1 1 1 External TO pin, rising edge trigger

(2) T/C0 count register TCNT0


TCNT0 is an 8-bit register with an initial value of 0x00. It can be directly read and written. It is the count value register of T/CO.


(3) T/C0r cut-off mask register TIMSK

Bit 7 6 5 4 3 2 1 0 TIMSK OCIEE2 TOCIE2 TICIE1 OCIE1A OCIE1B TOIE1 - TOIE0

The definition of TIMSK is shown in the table above. TOIE0 is the T/C0 overflow interrupt enable flag (TOIE1 is the T/C1 overflow interrupt enable flag). When TOIE0 is set to 1 and the global interrupt enable I bit in the status register SREG is set to 1, the T/C0 overflow interrupt will be enabled. If T/C0 overflows (TOV0=1), the T/C0 overflow interrupt service routine will be executed.


(4) T/C0 interrupt flag register TIFR


When T/CO overflows, TOV0=1 bit is set to 1. When the MCU enters the T/CO overflow interrupt service routine, TOV0 is automatically cleared by hardware.


T/CO counts up each clock CKT0. The source of CKT0 is set by the flags CS02~CS00. When T/CO selects the internal clock source, it can be used as a timer; when the pulse signal input from the T0 pin is selected as the signal source, it can be used as a counter. Once the count value of register TCNT0 reaches 0xFF, it will be restored to 0x00 when the next count pulse arrives, and continue to count upward. When TCNT0 is 0x00, set the overflow flag TOV0 to 1 to request an interrupt. When T/CO selects the internal clock source, it can be used as a timer; when the pulse signal input from the TO pin is selected as the signal source, it can be used as a counter. When T/CO is used as a timer, the timing time can be set by setting the initial value of TCNTO and the flags CS02~CS00 to set the frequency division coefficient.


2.16-bit timer/counter T/CI


The interrupt mask register TIMSK and interrupt flag register TIFR of T/C1 use the same register as T/CO. The definitions of the flag bits TOIE1 and TOV1 are the same as those of the corresponding flag bits of T/CO. In addition to the functions of T/CO, T/C1 also has some special functions.


There are two control registers for T/C1: TCCR1A and TCCR1B. TCCR1A is mainly used for T/C1 output comparison and PWM function setting. TCCR1B is mainly used for T/C1 input capture and clock division coefficient setting. The definition of TCCR1B is shown in the table below.

Bit 7 6 5 4 3 2-0 TCCR1B ICNC1 ICES1 - WGMI3 WGMI2 CS2-CS10

Since the clock source of T/C1 is the same as the clock source of T/CO, the definition method of the flag bits CS12 to CS10 and the flag bits CS02 to CS00 of T/CO are the same.


Because T/C1 is a 16-bit timer/counter, the count register of T/C1 consists of two 8-bit registers, TCNTIH and TCNTIL. TCNT1H stores the upper 8 bits, and TCNTIL stores the lower 8 bits. The initial values ​​of TCNTIH and TCNT1L are both 0x00, and can be directly read and written.


The method of using T/C1 as a timer and counter is similar to that of T/CO.


3. Digital frequency meter experiment


1. Experimental circuit


The digital frequency meter is mainly composed of a single-chip microcomputer ATmega8 and a liquid crystal display LCD1602, and uses a seven-digit digital display. The circuit part related to the digital frequency meter on the experimental board is shown in the figure below. The pulse signal is input to the T1 pin of ATmega8 through J1, and the frequency measurement result is displayed through LCD1602.

Circuits related to digital frequency meter on the experimental board

2. Programming


The program consists of main function, initialization function, T/CO interrupt function, T/C1 interrupt function, count value processing function, LCD display function and delay function.


In the program, T/CO is used as a timer with a timing time of 1 second. T/C1 is used as a counter to record the number of pulses input from the T1 pin. The statements for setting the timer/counter in the initialization function are as follows:


TCCR0=0x05; //T/C0 works in timing mode, internal 1024 frequency division


TCCRIB=0x06; //T/C1 works in counting mode, external falling edge trigger SREG=0x80; //Open interrupt TIMSK=0x05; //T/C0, P/C1 interrupt enable


TCNT0=4; //T/C0 counter initial value Combining the previous content, it is easy to understand the meaning of these statements. The T/C1 interrupt function is as follows:


#pragma interrupt_andler


Timer1_0vf:9


void Timer1—ovf(void)


{n++;}


The number 9 in the first line of the program above is the interrupt vector. Once the count value of the counter register of T/C1 reaches 0xFFFF, an interrupt is requested and transferred to execute this interrupt function through the interrupt vector. Since T/C1 interrupts at most 65535 (i.e. 0xFFFF) pulses, if it is only used as a counter, the maximum number of pulses that can be recorded is only 65535, that is, the highest frequency that can be measured using 1 second as the timing unit is 65535Hz. In order to solve this problem, a counting variable n is introduced, so that the value of n is increased by 1 every time T/C1 interrupts. In this way, the frequency of the pulse signal can be obtained by calculating the count value of the counter register of 65535×n+T/C1 every 1s.


T/C1 samples the external pulse signal at the falling edge of T1 according to the program settings. In order to ensure the correct sampling of the external clock, the conversion time of the external signal must be kept at least one clock cycle. Therefore, for the ATmega8 clock frequency of 8MHz, assuming that the input pulse signal is a square wave, the highest frequency that can be measured is 4MHz. The T/C0 interrupt function is as follows:


#pragma interrupt_handler


Timer0_ovf:10


void TimerO_ovf(void)


{


TCNT0=4;


k++:


if(k==31) //If the timer reaches 1s


{


uint m:


UNSigned long f;


k=0;


m=TCNTIL;


m=(TCNT1H<<8)+m; //get


T/C1 The mantissa after n counts


TCNT1H=0:


TCNT1L=0:


f: (unsigned long)


65536*n+m; //Get frequency value


Process(f, Data); //


Count value processing


Display(0, 0, Data); /


/show


n=O:


}


}


This program has two main functions: one is to act as a 1-second timer, and the other is to perform numerical calculations and call the display function to display the frequency.


The clock frequency of ATmega8 on the experimental board is 8MHz, and one clock cycle is only 0.125μs. The time for T/C0 counting register TCNT0 to count 256 (i.e. 0xFF) pulses is only 3.2ms, which does not meet the 1s timing requirement. Therefore, two measures are taken to solve this problem. First, the clock signal is divided by 1024 by setting the flag values ​​CS02~CS00 and then used as a counting pulse, thereby extending the pulse cycle; second, the variable k is introduced to count the number of interruptions of T/CO, so that the value of k is increased by 1 each time T/CO is interrupted. In this way, the total delay time is the product of T/CO timing time and k. Through calculation, we take the initial value of TCNT0 as 4, that is, an interrupt will be generated when TCNT0 counts to 242. When k accumulates to 31, the time required is about 1s, that is, (1024/8000000)×252×31=0.999936(s)≈ls. Therefore, the frequency of the pulse signal can be obtained by reading the total number of pulses every 1s (that is, k accumulates to 31).

[1] [2]
Keywords:ATmega8 Reference address:Digital frequency meter made with ATmega8 microcontroller

Previous article:Design of digital controlled DC regulated power supply based on VUSB
Next article:A low-cost wireless IRIG-H (DC) decoder

Recommended ReadingLatest update time:2024-11-15 14:07

ATmega8 simulation - learning of external interrupts
Earlier we learned how to use the ATmega8 I/O port as a general digital input/output port to control LED digital tubes and scan buttons. However, most of the I/O ports of ATmega8 are multiplexed ports. In addition to being used as general digital I/O ports, they also have a second function. Here we will learn the se
[Microcontroller]
ATmega8 simulation - learning of external interrupts
AVR ATMEGA8 Serial Port USART
The avr serial port configuration is very simple, just configure a few registers to send and receive; But there are a few things to understand: 1. Once the serial port is successfully configured, the IO function is automatically occupied, which is different from LPC or STM8/32 (registered configuration is required
[Microcontroller]
ATmega8 External Reset
External reset is generated by a low level applied to the RESET pin. When the reset low level duration is greater than the minimum pulse width (see Table 15), the reset process is triggered, even if there is no clock signal running at this time. When the external signal reaches the reset threshold voltage VRST (rising
[Microcontroller]
ATmega8 External Reset
Design of wireless sound amplification system based on ATmega8
The wide application of wireless sound reinforcement systems has solved the problems of wiring and mobile use in actual projects. Wireless transmission methods have also developed from traditional U-band and V-band wireless sound reinforcement to today's infrared, Bluetooth and 2.4 GHz wireless digital transmission met
[Microcontroller]
Design of wireless sound amplification system based on ATmega8
atmega8 example: 5110 LCD driver
/*****************************************************************   * Library Description: ATMEGA8 NOKIA 5110LCD driver   * Version: v1.0                                                                                            *                                                                                        
[Microcontroller]
Universal programmer made with two atmega48 (or atmega8)
The main 48 is connected to the computer via the serial port Replace 74373 latch from 48 The programming object is w39v040. The program needs to be improved. The program for detecting the completion of writing (or erasing) is replaced by a delay program. The master 48 pb port is a bidirectional port, sending d
[Microcontroller]
atmega8 example: T1 timer CTC mode 10ms timer 1s continuous timing
/* * Function library description: ATMEGA8 T1 timer CTC mode 10ms timer 1s continuous timing * Version: v1.00 * Edited by: Pang Hui, Wuhu Lianda Freescale Studio * Modified on: August 8, 2011 * * Description: Equivalent to 51 automatic reload mode * * Ver
[Microcontroller]
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号