Introduction to C52 MCU Timer 2

Publisher:雅致书香Latest update time:2020-01-13 Source: eefocusKeywords:C52 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Timer 2 is a 16-bit timer/counter. It can be set as a timer or counter by setting the C/T2 bit in the special function register T2CON. Timer 2 can be set to three operating modes by setting the operating mode selection bits in T2CON, namely capture, automatic reload (incremental or decremental counting) and baud rate generator.


Knowledge point 1: Timer 2 control register T2CON (bit addressable)*

D7 bit --TF2: Timer 2 overflow flag. Used to request an interrupt (must be cleared to 0 by software)

D6 bit --EXF2: Timer external flag. When the external signal is enabled, an external negative transition occurs and the interrupt request is set (must be cleared to 0 by software)

D5 bit -- RCLK: Receive clock flag. By default, the clock for mode 1 and mode 3 of the serial port is provided by the overflow rate of timer 1. If this bit is set, it is provided by timer 2.

D4 bit -- TCLK: Send clock flag bit. The principle is the same as above.

D3 bit --EXEN2: External enable flag of timer 2. When timer 2 is not used as the serial port clock, if this bit is set, it will allow the negative transition of T2EX to generate capture or reload

D2 bit--TR2: Timer 2 start/stop control bit.

D1 bit -- C/T2: Timer/counter selection bit for timer 2 (defined as C_T2 in the reg52 header file, please note that the same applies below)

D0 bit--CP/RL2: capture/reload flag.


Knowledge point 2 Timer 2 mode control register T2MOD (not bit addressable)

This register may not be defined in the microcontroller's header file reg2.h, define it yourself!

D1 bit—T2OE: Timer 2 output enable bit

D0 bit - DCEN: Down count enable bit


Knowledge point 3: Three modes of timer 2*

*1. Capture mode*

When "CP/RL2=1" && "TR2=1" it enters the capture mode. By setting or clearing the external enable flag EXEN2 of the control register T2CON, it can be divided into the following two working modes:

<1>EXEN2=0:

At this time, timer 2 is used as a 16-bit timer/counter (selected by the timer/counter selection bit C/T2), and the experiment has passed.

<2>EXEN2=1:

At this time, the timer adds a feature based on the former, that is, it allows to accept the negative jump of the external input T2CON. This negative jump makes the values ​​of TH2 and TL2 in timer 2 stored in the trap registers RCAP2H and RCAP2L, and sets the external flag bit EXF2 to cause an interrupt. This interrupt exists at the same time as the interrupt in the former and shares the same interrupt program (TF2 and T2EX bits can be detected in the interrupt to determine which one caused the interrupt). In addition, in this mode, when the interrupt is caused by the T2EX bit, although the interrupt is triggered, it is not an overflow and the counter does not stop counting. Therefore, TH2 and TL2 do not need to be reloaded at this time.


Experiments have shown that this mode can be achieved: use overflow interrupt to output square wave, connect to T2EX (P1^1) with a line, and make the buzzer sound in the T2EX interrupt. The theory is the same as the practice. The frequency of the buzzer is twice that of the square wave (because it only captures negative transitions).


2. Automatic reload mode (up/down counter)*

In 16-bit auto-reload mode, Timer 2 can be configured as a timer/counter through the C/T2 bit. Depending on the setting or clearing of the external enable flag EXEN2, there are two cases:

<1> When EXEN2=0

Timer 2 is a 16-bit normal timer with automatic reload. The reload value is provided by the trap register and only needs to be preset. It can be used in situations where high timing accuracy is required and the timing time is long (16 bits).

<2> When EXEN2 = 1, there are two cases according to the setting and clearing of the down count enable bit DCEN:

A1, T2MOD = 0x00 (DCEN = 0; default);

Compared with the previous case, the 16-bit automatic reload can be triggered by either a negative transition of the external T2EX or an overflow, and an interrupt can be generated.

A2, T2MOD = 0x01 (DCEN = 1);

At this time, T2EX is allowed to control the direction of counting; when T2EX=0, the reload value is 0FF and 0FF, and when the count is equal to the value stored in the trap register, TF2 is set to generate an interrupt. When T2EX=1; the automatic reload value is the value in the trap register, and TF2 is set to generate an interrupt when overflow occurs.


3. Baud rate generator mode When the TCLK and RCLK bits of T2CON are 0 (default), the baud rate of the serial port is provided by timer 1; when set to 1, it is provided by timer 2. One can pass through timer 1 and the other through timer 2, so that different baud rates can be obtained when sending and receiving.


Note: When Timer 2 is used as a timer, the increment frequency is 12 times the crystal frequency, and when Timer 2 is used as a baud rate generator, its increment frequency is 2 times the crystal frequency.


Baud rate for mode 1 and mode 3 = (oscillator frequency/32) * (65535-N)

When timer 2 is in counting mode, the external clock signal enters through the T2 (P1^0) pin.


When Timer 2 is used as a baud rate generator, TH2 overflow will not set TF2, so there is no need to disable Timer 2 interrupt. If the EXEN2 bit is set, T2EX can be used as an additional external interrupt.


When Timer 2 is used as a baud rate generator, do not read or write TH2 and TL2. You can read the trap register, but do not write it. When accessing the trap register of Timer 2, the timer should be turned off (TR2 is cleared to 0).


4. Programmable clock output

52 series MCU, timer/counter 2 can be set to output clock through T2 (p1^0) pin.

In addition to being a normal I/O port, P1^0 can also be used as the external count input and clock signal output of timer 2.

When C/T2=0 and the T2OE bit of T2MOD is 1, timer 2 can be selected as the clock signal generator and automatically loaded with the initial value. Setting formula:

Clock signal output frequency = (oscillator frequency/4) * (65535-N)

In clock output mode, the counter overflow will not generate an interrupt request. This function is equivalent to Timer 2 being able to be used as a baud rate generator and a clock generator at the same time.


**Because the external interrupt is not temporarily used at this time, if there is no conflict in the settings, it may also be able to respond to the external signal introduced by T2EX at the same time. This is just a guess and has not been proven by experiments, haha***


Knowledge point three, the microcontroller has the function of counting external pulse signals, but there are requirements:

The maximum frequency of the counting pulse = the frequency of the oscillator / 24

And to ensure that a given level can be sampled once before the level changes, this level must be maintained for at least one machine cycle.

Keywords:C52 Reference address:Introduction to C52 MCU Timer 2

Previous article:Detailed Analysis of C51 MCU Timer/Counter Interrupt Function (Part 2)
Next article:The growth path of single-chip microcomputer (51 basics) - 020 51 single-chip microcomputer register function list

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号