In this article, we introduce the registers and functions of Timer 2 of 51 MCU.
1 Timer 2 control register T2CON
Timer 2 is a 16-bit timer/counter. It can be used as a timer or counter by setting the C/T2 bit in the special function register T2CON. Timer 2 has three modes: capture, auto-reload (count up or down) and baud rate generator. These three modes are set by T2CON. The byte address of the T2CON register is C8H. When the microcontroller is reset, all T2CON bits are cleared to 0. The definition of T2CON is shown in the following table.
Timer 2 Control Register T2CON
TF2—Timer 2 overflow flag.
Set when Timer 2 overflows, must be cleared by software. TF2 will not be set when RCLK=1 or TCLK=1.
EXF2—Timer 2 external flag.
When EXEN2 = 1 and a negative transition of T2EX (P1.1 port of the microcontroller) generates a capture or reload, EXF2 is set. When the Timer 2 interrupt is enabled, EXF2 = 1 will cause the CPU to enter the interrupt service routine of Timer 2. EXF2 must be cleared to 0 by software. In the up/down counter mode (DCEN = 1), EXF2 will not cause an interrupt.
RCLK—Receive clock flag.
When RCLK=1, the overflow pulse of timer 2 is used as the receive clock of serial port mode 1 or mode 3; when RCLK=0, the overflow pulse of timer 1 is used as the receive clock.
TCLK—Transmit clock flag.
When TCLK=1, the overflow pulse of timer 2 is used as the transmit clock of serial port mode 1 or mode 3; when TCLK=0, the overflow pulse of timer 1 is used as the transmit clock.
EXEN2—Timer 2 external enable flag.
When EXEN2=1 and Timer 2 is not used as the serial port clock, a negative transition on the T2EX pin is allowed to generate a capture or reload. When EXEN2=0, a negative transition on the T2EX pin is invalid for Timer 2.
Here we need to explain capture. In layman's terms, capture is to capture the value of a certain moment. It is usually used to measure the width or period of an external pulse. The capture function can accurately measure the width or period of the pulse. The working principle of capture is: there are two groups of registers inside the microcontroller, one of which has internal values that increase or decrease according to a fixed machine cycle. Usually, this group of registers is the counter register (TLn, THn) of the internal timer of the microcontroller. When an external pin related to the capture function has a negative jump, the capture will immediately capture the value in the first group of registers at this time and store it in another group of registers. This group of registers is usually called "trap registers" (RCAPnL, RCAPnH), and at the same time request an interrupt to the CPU. After the software records the data between two captures, it can accurately calculate the period of the pulse.
TR2—Timer 2 start/stop control bit.
Setting this bit to 1 starts Timer 2 and clearing it to 0 stops Timer 2.
C/T2—Timer/counter selection bit for T2.
When C/T2 = 1, it is set as an external event counter (falling edge triggered); when C/T2 = 0, it is set as an internal timer.
CP/RL2—Capture/Reload flag.
When CP/RL2=1 and EXEN2=1, a negative transition on the T2EX pin generates a capture.
When CP/RL2 = 0 and EXEN2 = 0, the overflow of Timer 2 or the negative transition of the T2EX pin can cause the timer to automatically reload. When RCLK = 1 or TCLK = 1, this bit is invalid and the timer is forced to automatically reload when overflowing.
This register sets the mode of Timer/Counter 2 as shown in the following table.
Three operating modes of timer/counter 2
2 Timer 2 mode control register T2MOD
This register is used to set the increment or decrement mode of the timer 2 auto-reload mode. The byte address is C9H. This register is not bit addressable. When the microcontroller is reset, all T2MOD bits are cleared to 0. The definition of this register is shown in the following table.
Timer 2 mode control register T2MOD
T2OE—Timer 2 output enable bit.
DCEN—Down count enable bit.
3 Three modes of timer 2
(1) Capture mode
In the capture mode, two options are set by EXEN2 in T2CON. If EXEN2=0, Timer 2 acts as a 16-bit timer or counter (selected by the C/T2 bit in T2CON), and TF2 (Timer 2 overflow flag) is set when overflowing. This bit can be used to generate an interrupt (by enabling the Timer 2 interrupt enable bit in the IE register). If EXEN2=1, it is the same as described above, but with an additional feature, that is, when the external input T2EX pin changes from 1 to 0, the current values of TL2 and TH2 in Timer 2 are captured into RCAP2L and RCAP2H respectively. In addition, the negative transition of the T2EX pin sets EXF2 in T2CON, and EXF2 can also generate an interrupt like TF2 (its vector is the same as the Timer 2 overflow interrupt address, and the Timer 2 interrupt service routine determines the event that caused the interrupt through TF2 and EXF2). The capture mode is shown in the figure below. In this mode, TL2 and TH2 have no reload values. Even when T2EX generates a capture event, the counter still counts at 1/12 (12-clock mode) or 1/6 (6-clock mode) of the negative transition or oscillation frequency of the T2 pin.
(2) Automatic reload mode (up/down counter)
In 16-bit auto-reload mode, Timer 2 can be configured as a timer/counter by the C/T2 bit, and the count can be programmed to increase or decrease. The counting direction is determined by the DCEN bit in the T2MOD register. When DCEN=0, Timer 2 counts up by default. When DCEN=1, Timer 2 can determine the count to increase or decrease by the T2EX pin. The following figure shows the schematic diagram of Timer 2 automatically counting up when DCEN=0. In this mode, the selection is made by setting the EXEN2 bit. If EXEN2=0, Timer 2 increments to 0FFFFH, and sets TF2 after overflow, and then loads the 16-bit value in RCAP2L and RCAP2H into Timer 2 as the reload value. The values of RCAP2L and RCAP2H are preset by software. If EXEN2=1, 16-bit reload can be achieved by overflow or negative transition of the T2EX pin. This negative transition also sets EXF2. If the interrupt of Timer 2 is enabled, an interrupt is generated when TF2 or EXF2 is set to 1.
When DCEN=1, Timer 2 can count up or down. The working principle of this mode is shown in the figure below. In this mode, the T2EX pin is allowed to control the direction of counting. When the T2EX pin is set to 1, Timer 2 counts up, overflows after counting to 0FFFFH and sets TF2, and also generates an interrupt (if the interrupt is enabled). The overflow of Timer 2 will cause the 16-bit value in RCAP2L and RCAP2H to be placed in TL2 and TH2 as the reload value. When T2EX is set to 0, Timer 2 counts down. When TL2 and TH2 count to equal RCAP2L and RCAP2H, Timer 2 sets TF2 and generates an interrupt, and loads 0FFFFH into TL2 and TH2.
(2) Baud rate generator mode
The TCLK and (or) RCLK bits of register T2CON allow the serial port to obtain the baud rate of transmission and reception from timer 1 or timer 2. When TCLK=0, timer 1 is used as the serial port transmission baud rate generator; when TCLK=1, timer 2 is used as the serial port transmission baud rate generator. RCLK has the same effect on the setting of the serial port reception baud rate. Through these two bits, the serial port can obtain different reception and transmission baud rates, one generated by timer 1 and the other generated by timer 2. The working principle of baud rate generation will be introduced in the subsequent serial port experiments, so I will not introduce it too much here.
Previous article:51 MCU (XVIII) - Timer 0 Example Test
Next article:51 MCU (Sixteen) - Introduction and function description of timer 0 and timer 1 registers
Recommended ReadingLatest update time:2024-11-17 01:56
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Fundamentals and Applications of Single Chip Microcomputers (Edited by Zhang Liguang and Chen Zhongxiao)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- 【Laser target detector】Belated unboxing, RPI400 unit only
- Getting Started Using TouchGFX to Develop STM32 Interface Applications (Part 1) - Software Installation and Hello World
- psoc creator soft imitation function
- Developer Case: Design of Smart Agriculture Based on Gizwits IoT and RT-Thread
- Basic Theory of System Timing
- The story of aerospace information
- GD32L233C-START evaluation development environment construction and official DEMO download test
- Several technical principles of unlocking shared bicycles with mobile phones,,,
- 【GD32E503 Review】——04.MCU CoreMark Performance Test
- Comparison of IIC data and logic diagram between the reference prototype and your own product