Since the function of the timer/ counter is determined by software programming, it is usually necessary to initialize the timer/counter before using it so that it works according to the set function. The steps for initialization are generally as follows:
1. Determine the working mode (i.e. assign a value to TMOD);
2. Preset the initial value of timing or counting (the initial value can be directly written into TH0, TL0 or TH1, TL1);
3. Enable the interrupt of the timer/counter as needed (directly assign a value to the IE bit);
4. Start the timer/counter (if it is specified to be started by software, TR0 or TR1 can be set to "1"; if it is specified to be started by the external interrupt pin level, the start level needs to be added to the external pin. When the startup requirements are met, the timer starts counting or timing according to the specified working mode and initial value).
The following is a specific method for determining the initial value of the timer/counter.
Because the number of counter bits is different in different working modes, the maximum count value is also different.
Now assume that the maximum count value is M, then the maximum value M under each mode is as follows:
Mode 0: M = 213 = 8 192
Method 1: M = 216 = 65 536
Method 2: M = 28 = 256
Method 3: Timer 0 is divided into two 8-bit counters, so both M are 256.
Because the timer/counter counts by "1" and generates an interrupt when the count overflows, the initial value X can be calculated as follows:
X = M - count value
The following example illustrates how to determine the initial value.
Example 1: Select T1 mode 0 for timing, and the output period of P1.1 is 1ms square wave, and the crystal oscillator fosc=6MHz.
Solution: According to the question, as long as P1.1 is inverted every 500us, a 1ms square wave can be obtained, so the timing time of T1 is 500us. Since the timing time is not long, mode 0 can be used. Then M1 M0=0; because it is a timer mode, C/T=0; T1 is started by software here, so GATE=0. T0 is not used, and the mode word can be set arbitrarily, as long as it does not enter mode 3, it is generally taken as 0, so TMOD=00H. After the system is reset, TMOD is 0, and TMOD does not need to be cleared to 0 again. Content from the home of single-chip microcomputers www.dpj100.com
The following is the calculation of the initial value of the 500us timing T1:
machine cycle T=12/fosc=12/(6×106)Hz=2μs
Assume the initial value is X, then:
(1013-X)×2×10-6s=500×10-6s
X=7942D=1111100000110B=1F06H
Because when used as a 13-bit counter, the upper 3 bits of TL1 are not used and should be filled with 0, and TH1 occupies the upper 8 bits, so the actual filling of X should be:
X=111100000000110B=F806H
Result: TH1=F8H, TL1=06H
The source program is as follows:
ORG 2000H
MOV TL1,#06H ; Initialize TL1
MOV TH1,#0F8H ; Initialize TH1
SETB TR1 ; Start T1
LP1:JBC TF1,LP2; Check whether the count overflows?
AJMP LP1
LP2:MOV TL1,#06H ; Reset the initial value of the count
MOV TH1,#0F8H
CPL P1.1 ; Output inversion
AJMP LP1 ; Repeat cycle
Previous article:Principle of timing counter Calculation of counting period
Next article:MCS51 single chip counter working mode 0 timing time calculation and programming example
Recommended ReadingLatest update time:2024-11-16 19:57
- Popular Resources
- Popular amplifiers
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
- Showing goods + NVIDIA AI small board
- Circuit diagram of two-wheeled electric vehicle
- [Domestic RISC-V Linux Board Fang·Starlight VisionFive Trial Report] Python output HELLOWORLD
- Solutions for control loop design
- Data size transferred after power failure
- About DMX dimming system
- What is UWB?
- Solution to blind area communication of intercom
- Xunwei i.MX8MM Development Board Linux Android 9.0 Crotex-M4 Data Update
- Commonly used algorithms for drones - Kalman filter (Part 3)