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.
(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.
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).
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
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- The State of Open Source Hardware in 2021
- Implementation of HPI Boot Mode in TMS320C62x
- What is the physical meaning of Gauss's formula in advanced mathematics?
- 【Portable Environmental Status Detector】Compile and download function test
- Learn about TI 15.4-Stack's support for 470M frequency band
- 【Me and Yatli】+My impression of Yatli
- Advanced Algebra Concise Course
- [RT-Thread reading notes] Three timers
- AP6212 driver all firmware & nvram
- Bosch air pressure sensor gives sweeping robots more monitoring functions