First of all,
the timer/
counter is very important. It is the heart of the microcontroller.
First of all, let's talk about some concepts.
Clock frequency: fosc (provided by the crystal oscillator, 51 is generally 12Mhz and 11.0592Mhz)
12 division: fosc/12 (I set it to fT here)
Machine cycle: T=1/fT
There are two configurations for the timer.
The first is the working mode: timer/counter.
The second is the four working modes.
Then there are two registers
TMOD&TCON
working mode register and control register
working mode register TMOD
(1) GATE - When the gate bit
GATE=0, only the control bit TRX (X=0, 1) starts the timer/counter.
When GATE=1, the high level on TRX (X=0, 1) and the interrupt pin (INT0 and INT1) jointly starts the timer/counter.
(2) M0, M1 -
There are four encodings for the working mode selection bits M0 and M1, corresponding to the four working modes. Corresponding relationship
M1 M0 Working mode
0 0 Mode 0, 13-bit timer/counter
0 1 Mode 1, 16-bit timer/counter
1 0 Mode 2, 8-bit timer/counter 1 with 8-bit initial value automatically reloaded
1 Mode 3, only applicable to T0, divided into two 8-bit counters, T1 stops counting
(3) C/T——Counter mode and timer mode selection bit
C/T=0, timer mode
C/T=1, counter mode, the counter counts the external pulse (negative jump) of the external input pin T0 (P3.4 pin) or T1 (P3.5 pin).
Timer/counter control register TCON
The byte address of TCON is 88H, which can be bit-addressed, and the bit address is 88H~8FH.
The lower four bits are related to interrupts, and the upper four bits
(1) TF0, TF1——T0, T1 count overflow flag bit
When the counter counts overflow, this position is "1". When using the query mode, this bit is used as a status bit for the CPU to query, but it should be noted that after the query is valid, the bit should be cleared to 0 in a timely manner by software. When using the interrupt mode, this bit is used as an interrupt request flag bit and is automatically cleared to 0 by hardware after entering the interrupt service program .
(2) TR0, TR1 - counting operation control bit
TR1 bit (or TR0 bit) = 1, a necessary condition for starting the timer/counter, and is also related to the GATE bit.
TR1 bit (or TR0 bit) = 0, turns off the timer/counter.
Regarding the initial value calculation of several working modes,
the first working mode is a 13-bit timer.
Its maximum value is 213=81922
13
=8192.
The initial value calculation is,
for example, to time 1ms.
1ms is the time, which corresponds to the number of steps taken by the microcontroller.
First, calculate how much time one step takes, which is the machine cycle 12/fosc.
You can calculate a total of fosc/(12*1000) steps.
Subtract this number of steps from the maximum value of the timer to get the initial value of the timer.
The second working mode is similar.
The third is automatic reload. The values of its high and low registers are the same.
Previous article:C51 universal serial port data transmission and reception C language program
Next article:Single chip microcomputer controls stepper motor, LCD1602 displays
Recommended ReadingLatest update time:2024-11-17 03:05
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!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- 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
- EEWORLD University - Brushless Motor Tutorial
- Introduction to FPGA design process and tool software
- How to deal with the inductor spike at the back end of the switching power supply
- The development of 3G in China
- EEWORLD University----[High Precision Laboratory] Interface: RS-485
- Newbie, please help me with CPLD frequency multiplication
- Are there any netizens working in the field of motors?
- Evaluation report summary: Mir MYS-8MMX
- Zigbee Technology Exchange
- ST's new 400W power board