1. Know before you know
1. Knowledge about CPU timing
Oscillation period: The period of the oscillation source that provides the timing signal for the microcontroller (crystal oscillator period or external oscillation period)
State cycle: 2 oscillation cycles are 1 state cycle, represented by S. The oscillation cycle is also called the S cycle or clock cycle.
Machine cycle: 1 machine cycle = 6 state cycles = 12 oscillation cycles.
Instruction cycle: The total time taken to complete one instruction, which is measured in machine cycles.
1.1. Cycle Conversion
2. What you need to know before learning the timer
(1) The 51 MCU has two sets of timers/counters;
Because it can both time and count, it is called a timer/counter.
(2) The timer/counter and the microcontroller's CPU are independent of each other.
The timer/counter operation process is completed automatically and does not require the participation of the CPU.
(3) The timer/counter in the 51 single-chip microcomputer adds 1 to the data in the register according to the internal clock of the machine or the external pulse signal;
(4) With a timer/counter, the efficiency of the microcontroller can be increased.
Some simple repeated additions can be handled by the timer/counter, and the CPU can handle more complex tasks. At the same time, precise timing can be achieved.
3. Working principle of timer/counter
4.51 MCU timer structure
The essence of the timer/counter is a 16-bit add-on counter, which consists of two registers, THx and TLx, with high and low 8 bits. TMOD is the working mode register of the timer/counter, which determines the working mode and function; TCON is the control register, which controls the start and stop of T0 and T1 and sets the overflow flag.
2. Timing/Counter Control
The operation of the 51 MCU timer/counter is controlled by two special function registers: TMOD is used to set its working mode; TCON is used to control its startup and interrupt request.
1. Working mode register TMOD
The working mode register TMOD is used to set the working mode of the timer/counter. The lower four bits are used for T0 and the upper four bits are used for T1.
Its format is as follows:
GATE is the gate bit. When GATE=0, it is used to control whether the start of the timer is affected by the external interrupt source signal. As long as TR0 or TR1 in TCON is 1 by software, the timer/counter can be started.
When GATE=1, you need to use software to make TR0 or TR1 in TCON 1, and the external interrupt pin INTO/1 is also at a high level, then the timer/counter can be started. [Compared with the above method, this method has one more external high level (so the above method is often used)]
C/T: Timing/counting mode selection bit. C/T = 0 is timing mode; C/T = 1 is counting mode.
M1M0: Working mode setting bit. The timer/counter has four working modes.
2. Control register TCON
The lower 4 bits of TCON are used to control external interrupts, which have been introduced in the [Interrupts] section. The upper 4 bits of TCON are used to control the start of the timer/counter and the interrupt request.
Its format is as follows:
TF1 (TCON.7): T1 overflow interrupt request flag. When T1 counts overflow, TF1 is automatically set to 1 by hardware. After the CPU responds to the interrupt, TF1 is automatically cleared to 0 by hardware. When T1 is working, the CPU can query the status of TF1 at any time. Therefore, TF1 can be used as a flag for query testing. TF1 can also be set to 1 or cleared to 0 by software, which has the same effect as setting 1 or clearing 0 by hardware.
TR1 (TCON.6): T1 operation control bit. When TR1 is set to 1, T1 starts to work; when TR1 is set to 0, T1 stops working. TR1 is set to 1 or cleared to 0 by software. Therefore, the start and stop of the timer/counter can be controlled by software.
TF0 (TCON.5): T0 overflow interrupt request flag, its function is similar to TF1.
TR0 (TCON.4): T0 operation control bit, its function is similar to TR1.
3. Working mode of timer/counter
Mode 0: 13-bit counter (TR0 control type)
When GATE=0 passes through the inverter, it is 1, and it is still 1 after passing through the OR gate (don't worry about the INT0 pin at this time, because it is an OR gate), and then passes through the AND gate (the value at this time is controlled by TR0). When TR0=1, it is turned on and starts counting, otherwise it is not turned on. [Controlled by TR0]
When GATE=1 passes through the inverter and is 0, we still need to look at the INT0 pin. If it is 1, it will pass through the OR gate to be 1 (otherwise it will be 0 and the timer will not start), and then pass through the AND gate (the result is controlled by TR0 at this time). When TR0=1, it is turned on and starts counting. [Controlled by TR0]
Mode 1: 16-bit plus 1 counter
Method 2: 8-bit automatic reload (reload initial value)
Mode 3: Only applicable to timer/counter T0
Usually use method 1 and method 2
What to do when using a timer
The initialization program should complete the following tasks: [Writing the program is also done in the following steps]
Assign values to TMOD to determine the working mode of T0 and T1;
Calculate the initial value and write it into TH0, TL0 or TH1, TL1;
Turn on timer 0 interrupt switch ET0;
Interrupt mode, assign a value to EA (general interrupt switch) to turn on the timer interrupt;
Set TR0 or TR1 to start the timer/counter timing or counting.
Previous article:51 MCU timer and counter
Next article:[MCU Learning] 51 MCU [Serial Port], detailed introduction
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
- Does anyone have a PDF of Analog Filter and Circuit Design Handbook (US)?
- [Xianji HPM6750 Review IX] Detailed Optimization Methods for Performance Improvement
- Undergraduate students from the University of Science and Technology of China designed a processor chip in 9 months! Decoding the story behind it
- The Raspberry Pi discount quota is full. Check out other e-selected products to participate in the event
- Go your own way and let the SI engineer decide
- protues arm2124 simulation snake program
- BlueNRG-1 chip
- Bluetooth 5 New Features and Applications
- GD32F130 will immediately enter the serial port interrupt after the serial port idle interrupt is turned on
- LIS2MDL array PCB board for magnetic nail navigation AGV car