The timer is a very important part of the MCS-51 microcontroller. Since its application is related to other hardware of the microcontroller, there is a certain complexity [1]. The timer is the most effective method to solve certain types of complex problems in microcontroller applications, and it is widely used. With the improvement of timing requirements, the errors caused in the timing processing process need to be corrected [2]. This article discusses the use of MCS-51 microcontroller and error correction methods, and provides general algorithms and programs. 2. Timer working mode and mode setting MCS-51 microcontroller has two independent timers. Each timer has 4 working modes (0~3). Mode 0 is a 13-bit counting structure. The counter consists of all 8 bits of TH and It consists of the lower 5 bits of TL; Mode 1 is a 16-bit counting structure, and the counter is composed of all 8 bits of TH and TL, a total of 16 bits; Mode 2 is an 8-bit counting structure, and the counter is composed of 8 bits of TL. Different from other modes, When the timer overflows, the hardware automatically loads the value of TH into TL, and has an automatic loading function. In the first three working modes, the setting and use of the two timers are exactly the same, but in working mode 3, the two timers are very different. Only the timer 0 can work in mode 3, and the timer 1 can only work in mode 3. It can work in modes 0 to 2. In working mode 3, the timer 0 is split into two independent 8-bit counters TL0 and TH1. The control bits and pin signals of the timer 0 are all assigned to TL0. Therefore, TH0 can only be used as a simple timer, and its control bit occupies the control bit of timer 1. If timer 0 works in mode 3, timer 1 usually gives up its control bit. As a serial port baud rate generator [3]. The selection of the timer's working mode mainly depends on setting the value of the TMOD register. Its bits are defined as follows:
order |
B7 |
B6 |
B5 |
B4 |
B3 |
B2 |
B1 |
B0 |
bit sign |
GATE |
C/T |
M1 |
M0 |
GATE |
C/T |
M1 |
M0 |
Among them, B0~B3 are used to control timer 0, and B4~B7 are used to control timer 1.
The GATE bit is a gate control bit, which mainly determines how the timer is started. If GATE=0, use the TR0 (TR1) bit to start/stop the timer. If GATE=1, use an external interrupt request signal (INT0 or INT1) to start the timer. device.
The C/T bit is the timing and counting selection bit. C/T=0 is the timing mode, and C/T=1 is the counting mode.
M0 and M1 are the working mode selection bits.
M1M0=00 mode 0M1M0=10 mode 2
M1M0=01 mode 1M1M0=11 mode 3
selects the timer working mode. The corresponding number should be written to the register TMOD according to the above regulations, such as setting 0 channel timing. If the timer works in timing mode 0 and the 1-channel timer works in mode 1, the following instructions should be used to set it:
MOVTMOD, #10H
3 Timer processing method and calculation of initial counting value
The timer of the MCS-51 microcontroller uses increment counting method, in the timer working mode, after starting the timer, the CPU hardware will automatically add 1 to the corresponding counter every machine cycle until it exceeds the maximum value that the corresponding counter can represent, a timer overflow occurs, and the hardware automatically sets TF0 (TF1) , the user can determine whether the timing overflows by querying the TF0 (TF1) bit. If the timing interrupt is open at this time, the corresponding timing interrupt will be triggered. MCS-51 responds to the interrupt according to the corresponding interrupt address (the interrupt entry address of the 0 timing interrupt is 000B, channel 1 is 001B) execute the interrupt handler.
There are two basic ways of timing programming. One way is the query mode, which turns off the corresponding timed interrupt when starting the timer, and then cycles through TF0 (TF1) for corresponding processing; the other way is the interrupt mode, which opens the corresponding timer interrupt when the timer is started. , and write the timer overflow handler in the form of an interrupt handler and transfer to the timer overflow handler by placing a jump instruction at the corresponding interrupt entry address (000B, 001B).
After the timer working mode is selected, the timer overflow time only depends on the initial value of the counter. How to determine the initial value of the counter according to the application needs is also an important issue in timer application. Assume that the machine cycle of the microcontroller is K0, the selected timing mode counter is n bits, and the realized timing time length is T, then:
(2n-x) K0=T(1)
where x is the initial value of the counter, according to Formula 1 can calculate the initial value x as:
x=2n-(T/K0)(2)
4 Timer programming steps and general procedures for each step
Timer application programming is mainly divided into the following steps. 4.1 Step 1 Calculate the initial
counting value
The machine cycle k0=2μs or 1μs, then determine the number of counter digits n according to the selected working mode, substitute the obtained k0 and n into equation 2, find the initial counting value x, and divide the value of x into TH0 (TH1) and TL0 (TL1). 4.2 Step 2: Write the initialization program. The initialization program is related to the timing programming mode. The interrupt mode initialization program is as follows (taking the 0-channel timer as an example): movTMOD, #n (specifies the timing working mode) movTH0, #n1 (sets the low bit of the counter) movTL1, #n2 (Set the counter high bit) setBEA (Open interrupt) setBET0 (Open timed interrupt 0) setBTR0 (Start timing) The query mode initialization program is as follows: movTMOD, #n (Specify the timing working mode) movTH0, #n1 (Set the counter low bit) movTL1 , #n2 (set the high bit of the counter) clrET0 (open timed interrupt 0) setBTR0 (start timer) where #n1 and #n2 are related to the initial counting value x. If mode 2 is used, #n1=#n2, all initialization procedures are Take channel 0 timer as an example. If you use channel 1 timer, change the subscript 1 of the register and control bit in the program to 0. 4.3 Step 3: Prepare the timing processing program. The interrupt mode processing program is as follows: org000B 1jmpb1 b1:clrEA movTH0, #n1 movTH1, #n2 timing processing statement segment reti . Among them, the 0-way timer is 000B, if it is 1-way timer, it is 001B, if When the timer works in mode 2, there is no need to reassign TH0 and TH1. The query processing program is as follows: L1:jbcTF0,LO1 sjmpL1 LO1:movTH0,#n1 movTH1,#n2 Timing processing statement segment sjmpL1 5 Timing error calibration Since the machine cycle of the microcontroller is 1μs~2μs, the timing error should generally be 0μs~20μs Within, for general applications, this error can be ignored, but for applications with higher accuracy requirements, this error must be corrected. The timing error is the time it takes to transfer to the execution of the timing processing statement segment after the timing overflow. This time is mainly caused by the instructions or hardware processes that must be executed before the timing overflow transfers to the timing processing statement segment. The simple principle of timing error calibration is: after the timing overflow response, stop the timer counting, read the current count value (which reflects the delay time of the timing response), and then take into account the execution time of the program segment to complete this task, as The correction factor corrects the timing initial value. The following program segment takes the interrupt processing method as an example: clrEA clrTR1 clrc movA,#n0 clrTR0 subbA,TL0 subbA,#08 movTL0,A movA,#n1 subbA,TH0 movTH0,A setbTR0 timing processing statement segment reti
Since it takes 8 machine cycles to execute instructions from instruction clrTR0 (stop counting) to instruction setbTR0 (restart counting), this consumption should be taken into account. The above program reduces the timing error to within 1 machine cycle.
Previous article:Design of time/displacement commutation controller based on microcontroller AT89C51
Next article:Application and error correction of microcontroller timer
Recommended ReadingLatest update time:2024-11-16 22:19
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
- [MM32 eMiniBoard Review] Building a development environment - lighting up the onboard LED
- EEWORLD University ---- Embedded Development Data Structure
- How to use the assembly language delay subroutine to realize the MCU P2 port to light up 8 LED lights in a loop
- Show off the Feike razor I exchanged with E-coins.
- Loto practical tips (3) Measuring CAN bus communication data
- Installing Android Studio on Xunwei 4412 Development Board (Part 1)
- [Synopsys IP Resources] Meeting China's Evolving Cloud Call Center Technology Needs with IP
- Hard-core black technology is coming! | RIGOL 2020 New Product Launch and Industry Forum Countdown on October 28!
- Processing of default settings after GPIO power-on of HUADA MCU
- CC3200 - TCP Communication