51 MCU (Seventeen) - Timer 2 register introduction and function description

Publisher:糖果龙猫Latest update time:2021-07-15 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

image.png

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

image.png

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

image.png

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.

Reference address:51 MCU (Seventeen) - Timer 2 register introduction and function description

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

51 MCU Basics: KeilC51 New Project [1]
1. Create a new DEMO folder 2. Click New File in KeilC51 3. Name the project and save it in the DEMO folder 4. Select the chip device (we choose AT89C52 chip) 5. Add startup file (STARTUP.A51) 6. The entire project is built, but there is nothing in the project at this time, and files need to be added 7. Cr
[Microcontroller]
51 MCU Basics: KeilC51 New Project [1]
51 MCU instruction set
After you have written a program, you can see the program's instruction set through software debugging. From the instruction set you can understand how the microcontroller works internally, such as the registers and addresses for data access. The following is an instruction set article found on the Internet, which exp
[Microcontroller]
PIC microcontroller - using the overflow interrupt of Timer2 to realize dynamic scanning of digital tubes
Write a program to make the display sequence of the digital tube be: 0123,1230,2301,3012. Digital tube display is divided into static scanning and dynamic scanning. Dynamic scanning display is generally divided into two ways 1. Select a digital tube position, write a broken code display, delay for a certain period
[Microcontroller]
Example of sending characters via 51 MCU serial port
#include reg52.h #define jingzhen 11059200UL /*Use 22.1184M crystal*/      #define botelv 9600UL /*Baud rate is defined as 9600*/ unsigned char zifuchuan ="Hello! n"; //Characters to be displayed. volatile unsigned char sending; sbit S4=P3^2; void delay(unsigned char i) {     unsigned char j,k;     for(j=i;j 0;j--)
[Microcontroller]
Software Design of Single Chip Microcomputer Based on RTX51
1 Overview Many MCU applications need to execute many tasks at the same time. For such applications, we can use real-time operating systems to flexibly arrange system resources. RTX51 is a small real-time multitasking operating system developed by Keil in the United States for MCS51 series MCUs. It can work
[Microcontroller]
The most basic flow lamp Proteus simulation diagram including code based on at89c51 microcontroller
#include reg51.h #include stdio.h void delay(void) {    unsigned int i,j;    for(i=500;i 0;i--)       for(j=115;j 0;j--);       } void main(void)  {     // Write your code here     unsigned char led ,a ,b ;//Define three variables     led=0xfe; //led=1111 1110, P0. 0 is on    while (1)    {       P0=led; //Assign th
[Microcontroller]
The most basic flow lamp Proteus simulation diagram including code based on at89c51 microcontroller
51 MCU Function Module Study Notes - Light Emitting Diode
1. Introduction to Light Emitting Diodes   A light emitting diode is a type of semiconductor diode that can convert electrical energy into light energy. It is often abbreviated as LED (light emitting diode).   Like ordinary diodes, light-emitting diodes also have unidirectional conductivity. When a forward voltage (gr
[Microcontroller]
Method of Decoding SAA3010T Encoding Chip Using 51 Single Chip Microcomputer
  The schematic diagram of the microcontroller circuit is shown in the figure below. A buzzer can also be added to the hardware for indication.      //The function realizes the control of the eight light-emitting diodes of port p1. The serial port is set in the program to view the IRCode sent by the remote control
[Microcontroller]
Method of Decoding SAA3010T Encoding Chip Using 51 Single Chip Microcomputer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号