Digital frequency meter made with ATmega8 microcontroller
Source: InternetPublisher:MartinFowler Keywords: ATmega8 single-chip microcomputer Updated: 2024/08/05
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
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/C0 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 the external pin. The prescaler structure is shown in the figure below.
(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 to 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 counting 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/C0.
(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 microcontroller 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, and 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 output comparison and PWM function setting of T/C1. TCCR1B is mainly used for input capture and clock division coefficient setting of T/C1. 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 that of T/CO, the definition method of flag bits CS12~CS10 and flag bits CS02~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, which can be directly read and written.
The method of using T/C1 as a timer and counter is similar to that of T/C0.
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 .
2. Programming
The program consists of main function, initialization function, T/C0 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 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 division
TCCRIB=0x06; //T/C1 works in counting mode, external falling edge trigger SREG=0x80; //Open interrupt TIMSK=0x05; //T/C0, P/C1 interrupt is allowed
TCNT0 = 4; // Initial value of T/C0 counter Combining the previous content, it is easy to understand the meaning of these statements. 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 to transfer to the execution of this interrupt function through the interrupt vector. Since T/C1 needs to input a maximum of 65535 (i.e. 0xFFFF) pulses at a time, 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 is interrupted. In this way, the frequency of the pulse signal can be obtained by calculating the count value of 65535×n+T/C1's counter register 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 the 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).
3. Frequency measurement
Use the download cable to write the target file of the supporting program into the ATmega8 on the experimental board , turn on the power of the experimental board, and send a pulse signal with an input amplitude of about 5V to the J2 interface, and the frequency value can be displayed on the LCD1602 liquid crystal display. If there is no signal source, you can make a signal generator for testing by yourself using the NOT gate as shown below.
In actual use, since the measured signal is often small in amplitude and irregular in waveform, an input signal processing circuit must be added at this time, and its main function is to amplify and shape the signal. The figure below provides a circuit made with a NOT gate. The two circuits in the above and below figures can be made together with a six-NOT gate circuit CD4069 , using a 5V power supply. It should be noted that since the ATmega8 on the experimental board uses an internal RC oscillator as the clock source, the accuracy of this digital frequency meter is low. If you want to improve the measurement accuracy, you must use an external crystal oscillator as the clock source.
- Hardware interface circuit between CH375 and single chip microcomputer
- AD8351 and SAW filter interface circuit
- How to design an ESP32-based microcontroller development board
- Application circuit composed of CSJ-R05B and microcontroller
- Use microcontroller to make simple timing controller for electric fan
- Minimum system circuit of XC4003
- Circuit diagram: AT88RF
- Usb mouse circuit diagram
- Interface circuit between digital measuring tool and 51 microcontroller
- Computer remote control circuit
- Microcontroller power-on reset circuit
- PCF8591 hardware interface (circuit diagram pin diagram)
- Design of infrared communication circuit based on single chip microcomputer
- Negative voltage generation circuit diagram
- Interface circuit design of SPCE061A microcontroller and fingerprint identification module
- Signal input circuitb
- Wiring of the power supply part of the microcontroller
- Typical circuit for connecting MAX1101 and microcontroller
- Interface circuit between ICL7109 and microcontroller
- Method of two-way serial communication between PC and 8031 microcontroller