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.
T2CON EQU 0C8H
T2MOD EQU 0C9H
CP EQU T2CON.0
TR2 EQU T2CON.2
EXEN2 EQU T2CON.3
EXF2 EQU T2CON.6
TF2 EQU T2CON.7
ET2 EQU IE.5
RCAP2L EQU 0CAH
RCAP2H EQU 0CBH
TL2 EQU 0CCH
TH2 EQU 0CDH
ORG 0000H
LJMP MAIN
ORG 002BH ;T0 interrupt entry
LJMP T2; Go to T0 interrupt service routine
ORG 0030H
MAIN:
SETB EA
SETB ET2
SETB EXEN2
MOV R1,#00H
LP:
SJMP LP
T2:
CLR TF2; Note that there is an external square wave signal input to the T2EX port, causing an interrupt
CLR EXF2
CJNE R1,#00H,TT2
SETB TR2 ;Start the timing function
MOV TH2,#0F2H
MOV TL2,#0FAH
INC R1
RETI
TT2:
CJNE R1,#01H,TTT3
INC R1
MOV TH2,#0D8H
MOV TL2,#0EFH
SETB P1.2 ; The first timing to output a pulse signal
LCALL DELAY
CLR P1.2
RETI
TTT3:
CJNE R1,#02H,ESC
CLR TR2 ; turn off the timing and wait for the next falling edge interrupt
MOV R1,#00H
SETB P1.3; Output a pulse signal when the second timing is reached
LCALL DELAY
CLR P1.3
ESC:
RETI
DELAY: ;Trigger pulse width delay program
MOV R0, #0FH
DL_M: DJNZ R0, DL_M
RET
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.
Previous article:Display of digital tube 0~9, delay 1s (without delay through counter)
Next article:The highest frequency that 8052 can measure
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!
- 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
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Op amp differential attenuation circuit problem
- Dismantling and Renovation of Old LED Bulbs
- Emitter follower principle and typical circuit
- STM32 A certain function is put into RAM and runs without any phenomenon
- I searched Baidu for half an hour but couldn't find the answer. I used a search engine to find the answer in seconds.
- EEWORLD University Hall ---- Disassembling the front controller module of Tesla Model 3 VCFRONT
- How to use JTAG to program UCD90120A
- Creating a Superior Smart Display Experience with DLP Pico Technology
- Previous test questions of the measurement and control category of the electric competition
- instaSPIN-FOC Motor Starting