Research on Timer/Counter 2 in 52 Single Chip Microcomputer

Publisher:平章大人Latest update time:2016-12-31 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The graduation project required external interrupts and timers, and both timers and external interrupts required three. I was confused at the time. I could have three timers, but it was difficult to have external interrupts. Moreover, the external interrupts were falling edge interrupts, and the three interrupt signals had a non-fixed relationship. I thought about interrupt expansion, but I couldn't come up with a solution after a long time. Later, according to the actual situation, I found that 52 timer 2 could be used as an external interrupt, especially the falling edge interrupt. Because what I was doing happened to be the kind that starts timing after an interrupt, so timing and interrupts did not conflict.

      There is not much information about 52 MCU timer/counter 2 on the Internet, because it is rarely used. Later, I could only study it from simple examples based on the information on the Internet and my continuous attempts, and then I understood the use of timer/counter 2 and realized the multiplexing of interrupts and timing. The following is some information about timer 2 registers that I found, which is relatively reliable.

Timer T2 is more powerful than T1 and T0, but there is not much information describing it, probably because it is rarely used. It is a 16-bit timer/counter with automatic reload and capture capabilities. Its counting clock source can be the internal machine cycle or the external clock pulse input by P1.0. The function description of T2's control register is as follows:

T2CON (T2 control register), byte address 0C8H:

Bit address 0CFH 0CEH 0CDH 0CCH 0CBH 0CAH 0C9H 0C8H

Symbol TF2 EXF2 RCLK TCLK EXEN2 TR2 C/T2 CP/RT2

The definitions of each are as follows:

TF2: Timer/Counter 2 overflow flag, set when T2 overflows and request an interrupt. It can only be cleared by software, but when T2 is used as a baud rate generator (i.e. RCLK=1 or TCLK=1), TF2 is not set when T2 overflows.

EXF2: When EXEN2 = 1, and a negative transition occurs on the T2EX pin (P1.0) causing T2 to be captured or reloaded, EXF2 is set and an interrupt is requested. EXF2 can only be cleared by software.

RCLK: Serial receive clock flag, which can only be set or cleared by software; used to select T1 (RCLK=0) or T2 (RCLK=1) as the serial receive baud rate generator

TCLK: serial transmission clock flag, which can only be set or cleared by software; used to select T1 (TCLK=0) or T2 (TCLK=1) as the baud rate generator for serial transmission

EXEN2: External enable flag of T2, which can only be set or cleared by software; EXEN2=0: Disable external clock to trigger T2; EXEN2=1: When T2 is not used as a serial baud rate generator, allow external clock to trigger T2. When a negative transition is input to the T2EX pin, it will cause T2 to be captured or reloaded, and set EXF2 to request an interrupt.

TR2: T2 start control flag; TR2=0: stop T2; TR2=1: start T2

C/T2: T2 timing mode or counting mode selection bit. Can only be set or cleared by software; C/T2=0: Select T2 as timer mode; C/T2=1: Select T2 as counter mode, falling edge triggered.

CP/RT2: Capture/reload flag, which can only be set or cleared by software. When CP/RT2=0, the reload mode is selected. At this time, if T2 overflows (EXEN2=0) or the T2EX pin (P1.0) has a negative transition (EXEN2=1), T2 will be reloaded; when CP/RT2=1, the capture mode is selected. At this time, if the T2EX pin (P1.0) has a negative transition (EXEN2=1), T2 capture operation will be caused. However, if RCLK=1 or TCLK=1, the CP/RT2 control bit does not work and is forced to work in the timer overflow automatic reload mode.

T2MOD (mode register), byte address 0C9H:

 

D7 D6 D5 D4 D3 D2 D1 D0

-- -- -- -- -- -- T2OE DCEN

T2OE: T2 output enable bit. When T2OE=1, the clock is allowed to output to P1.0. (Only valid for 80C54/80C58)

DCEN: Down-counting enable bit. DCEN=1 allows T2 to count down, otherwise it counts up.

 

The usage of T2's data registers TH2 and TL2 is the same as that of T0 and T1, while the capture registers RCAP2H and RCAP2L only automatically save the values ​​of TH2 and TL2 when a capture operation occurs in the capture mode.

    The above is the description of the relevant registers of T2. In fact, the usage is similar to T0 and T1, but the functions are enhanced and there are more settings.


The most used register of Timer/Counter 2 is T2CON, which sets the timing and counting mode. There are three working modes: capture, auto-reload, and baud rate generator. Here is some information I found in Baidu Encyclopedia:

Capture method:

  In the capture mode, two modes are selected through the T2CON control bit EXEN2. If EXEN2=0, timer 2 is a 16-bit timer or counter. When the count overflows, the overflow flag TF2 of T2CON is set and the interrupt is activated. If EXEN2=1, timer 2 completes the same operation, and when the external input signal of the T2EX pin changes from 1 to 0, the values ​​in TH2 and TL2 are captured into RCAP2H and RCAP2L respectively. In addition, the transition of the T2EX pin signal sets EXF2 in T2CON. Similar to TF2, EXF2 will also activate the interrupt. The capture mode is shown in Figure 4.

Automatic reload (up or down counter) mode:

  When Timer 2 works in 16-bit auto-reload mode, it can be programmed to count up or down. This function can be selected by the DCEN bit (enable down counting) of the special function register T2CON (see Table 5). At reset, the DCEN bit is "0", and Timer 2 is set to count up by default. When DCEN is set, Timer 2 can count up or down, depending on the value of the T2EX pin, see Figure 5. When DCEN=0, Timer 2 is automatically set to count up. In this mode, the EXEN2 control bit in T2CON has two options. If EXEN2=0, Timer 2 counts up to 0FFFFH overflow, sets TF2 to activate the interrupt, and reloads the 16-bit counting registers RCAP2H and RCAP2L at the same time. The values ​​of RCAP2H and RCAP2L can be preset by software. If EXEN2=1, the 16-bit reload of Timer 2 is triggered by overflow or the falling edge of the external input terminal T2EX from 1 to 0. This pulse sets EXF2 and also generates an interrupt if interrupts are enabled.

The interrupt entry address of timer 2 is: 002BH —— 0032H.

  When DCEN=1, Timer 2 is allowed to count up or down, as shown in Figure 6. In this mode, the T2EX pin controls the counter direction. When the T2EX pin is logic "1", the timer counts up. When the count overflows to 0FFFFH, TF2 is set, and the 16-bit count registers RCAP2H and RCAP2L are reloaded into TH2 and TL2. When the T2EX pin is logic "0", Timer 2 counts down. When the values ​​in TH2 and TL2 are equal to the values ​​in RCAP2H and RCAP2L, the count overflows, TF2 is set, and the 0FFFFH value is reloaded into the timing register.

When the Timer/Event Counter 2 overflows or underflows, the EXF2 bit is set.

Baud Rate Generator:

  When TCLK and RCLK in T2CON (Table 3) are set, Timer/Counter 2 is used as a baud rate generator. If Timer/Counter 2 is used as a transmitter or receiver, the baud rates for transmission and reception can be different, and Timer 1 is used for other functions, as shown in Figure 7. If RCLK and TCLK are set, Timer 2 works in baud rate generator mode.

  The baud rate generator operates in a similar manner to the auto-reload mode, where TH2 flips to reload the Timer 2 register with the 16-bit value in RCAP2H and RCAP2L, which is set by software.

  In Mode 1 and Mode 3, the baud rate is determined by the overflow rate of Timer 2 according to the following formula:

The baud rate of modes 1 and 3 = timer overflow rate/16

 The timer can work in both timing mode and counting mode. In most applications, it works in timing mode (C/T2=0). When Timer 2 is used as a baud rate generator, its operation is different from that of a timer. Usually, when used as a timer, the register value is increased by 1 in each machine cycle (1/12 oscillation frequency), while when used as a baud rate generator, the register value is increased by 1 in each state time (1/2 oscillation frequency). The baud rate calculation formula is as follows:

  The baud rate of modes 1 and 3 = oscillation frequency / {32*[65536-(RCP2H,RCP2L)]} where (RCAP2H, RCAP2L) is the 16-bit unsigned number in RCAP2H and RCAP2L.

  The circuit of Timer 2 used as a baud rate generator is shown in Figure 7. The baud rate working mode is valid only when RCLK or TCLK in T2CON=1. In the baud rate generator working mode, TH2 flip cannot set TF2, so no interruption occurs. However, if EXEN2 is set and a negative jump from 1 to 0 occurs at the T2EX terminal, EXF2 will be set. At this time, the content of (RCAP2H, RCAP2L) cannot be reloaded into TH2 and TL2. Therefore, when Timer 2 is used as a baud rate generator, T2EX can be used as an additional external interrupt source. It should be noted that when Timer 2 works in the baud rate generator, when it runs as a timer (TR2=1), TH2 and TL2 cannot be accessed. Because each state time timer will add 1 at this time, reading and writing it will result in an uncertain value.

  However, RCAP2 can be read but not written, because a write operation will be a reload, and a write operation may cause a write and/or reload error. Before accessing Timer 2 or the RCAP2 register, the timer should be turned off (clear TR2).

    Back to the topic, I actually want to study how to make the timer generate a falling edge interrupt first, and then start timing in the soft interrupt function. In fact, it is very simple, because as long as EXEN2=1 is set, and the timing function is not turned on, that is, after setting TR2=0, if there is an external falling edge pulse input to the T2EX (P1.1) pin, an interrupt will be generated, then TR2=1 can be set in the interrupt function to set the initial value of the timer, so that the timer is turned on and the timing function is realized. In short, this TR2 setting is very critical. In this way, the interrupt is realized first, and the soft timing is immediately realized. In fact, the T2EX port can also be used to completely interrupt the external falling edge pulse, as long as TR2=0 is always set. I just want to use the timing function here.

Below is a microcontroller program written in assembly language, which implements the function of external interrupt first and then timing.


  1. T2CON EQU 0C8H  

  2. T2MOD EQU 0C9H  

  3. CP EQU T2CON.0  

  4. TR2 EQU T2CON.2  

  5. EXEN2 EQU T2CON.3  

  6. EXF2 EQU T2CON.6  

  7. TF2 EQU T2CON.7   

  8. ET2 EQU IE.5  

  9. RCAP2L EQU 0CAH  

  10. RCAP2H EQU 0CBH  

  11. TL2 EQU 0CCH  

  12. TH2 EQU 0CDH  

  13.   

  14. ORG 0000H  

  15. LJMP MAIN  

  16. ORG 002BH ;T0 interrupt entry  

  17. LJMP T2; Go to T0 interrupt service routine  

  18. ORG 0030H  

  19. MAIN:  

  20.     SETB EA  

  21.     SETB ET2  

  22.     SETB EXEN2  

  23.     MOV R1,#00H  

  24. LP:  

  25.     SJMP LP  

  26.   

  27. T2:  

  28.     CLR TF2; Note that there is an external square wave signal input to the T2EX port, causing an interrupt  

  29.     CLR EXF2  

  30.     CJNE R1,#00H,TT2  

  31.     SETB TR2 ;Start the timing function  

  32.     MOV TH2,#0F2H  

  33.     MOV TL2,#0FAH    

  34.     INC R1  

  35.     RETI  

  36. TT2:  

  37.     CJNE R1,#01H,TTT3  

  38.     INC R1  

  39.     MOV TH2,#0D8H  

  40.     MOV TL2,#0EFH  

  41.     SETB P1.2 ; The first timing to output a pulse signal  

  42.     LCALL DELAY  

  43.     CLR P1.2  

  44.     RETI  

  45. TTT3:  

  46.     CJNE R1,#02H,ESC  

  47.     CLR TR2 ; turn off the timing and wait for the next falling edge interrupt  

  48.     MOV R1,#00H  

  49.     SETB P1.3; Output a pulse signal when the second timing is reached  

  50.     LCALL DELAY  

  51.     CLR P1.3  

  52. ESC:  

  53.     RETI  

  54.       

  55. DELAY: ;Trigger pulse width delay program  

  56.         MOV R0, #0FH  

  57. DL_M: DJNZ R0, DL_M  

  58.         RET   

  59. END  


 

 

The square wave can be input from the T2EX port as an external interrupt signal. When its falling edge arrives, timer 2 can trigger an interrupt, start the timing mode, and then timer 2 enters the timing mode. After a period of timing, a pulse signal is input, and then another pulse signal is output after a period of timing.

 

Summary: Based on my study in the past few days, I have not learned about other functions of timer/counter 2 in detail, but I know that 52's timer 2 is indeed more complex and powerful than timer 0 and 1. The first point is that it can be used as an external falling edge interrupt. Although external interrupt can also be realized by counting, its function is more powerful.


Reference address:Research on Timer/Counter 2 in 52 Single Chip Microcomputer

Previous article:Display of digital tube 0~9, delay 1s (without delay through counter)
Next article:The highest frequency that 8052 can measure

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号