Objective: Understanding of timers and counters (I)
1. Timer/Counter Introduction
Timer/Counter (T/C for short) is one of the most basic interfaces in a single-chip microcomputer. It can be used for both timing and counting. It is often used for counting, delaying, measuring cycle/pulse width/frequency, providing timing pulse signals, etc.
The 8051 series microcontrollers contain at least two 16-bit timers, and the STC89C51/52 contains three timers. Two of them are basic timers/counters T/C0 and T/C1, and the other is T/C2. They can be configured as timer functions or counter functions through software.
How does the timer/counter work?
The timer/counter is essentially an add-1 counter. When working in the timer mode, it counts the pulses of the oscillator source (crystal oscillator) divided by 12 (12 division is the oscillation period, the unit is us), that is, each machine cycle (12 oscillation periods is 1/12us12=1us) count value is added by 1 (explained the delay function above), when the counter is added to all 1 (the timer counter is 16 bits, then 2*16=65539, minus 1 [because it starts at 0], 65535 is the maximum value of the timer count), then input a pulse to make the counter return to zero.
When working in counter mode, the counting pulse comes from the external pulse input pins P3.4 (T0) and P3.5 (T1). When the T0 or T1 pin has a negative transition (i.e., falling edge, positive transition == rising edge), the count value increases by 1. It takes 2 machine cycles (2us), i.e., 24 oscillation cycles, to identify the negative transition of the pin. Therefore, the maximum frequency of the countable external pulse input to the T0 or T1 pin is 1/24fosc (1/24*12=500KHz). When the crystal oscillator is 12MHZ, the maximum counting frequency is 500KHz. If the frequency is higher than this, the counting will be wrong.
2. Timer/Counter Registers
Timer/Counter Registers
TH1 and TL1 are two registers of timer/counter 1, both are 8 bits, totaling 16 bits. TH1 is the high 8 bits, TL1 is the low 8 bits
TH0 and TL0 are two registers of timer/counter 0, both are 8 bits, totaling 16 bits. TH0 is the high 8 bits, TL0 is the low 8 bits
The T0 pin is P3.4, the T1 pin is P3.5, configured in counter mode to detect external pulse signals
Timer control register TCON,
working mode selection register TMOD (used to determine the working mode and function of the timer/counter)
TCOD see MCU Novice Learning Road (Twelve) ----> https://www.jianshu.com/p/4de17b66a15b
Working mode selection register TMOD
Working mode selection register TMOD
0~3 are the 4 bits of timer/counter 0. M1 and M0 are used to select the working mode. The working modes are 0, 1, 2, and 3. C/T is the selection bit of the counter or timer. If C/T is set to 0, it is timing mode, and if it is set to 1, it is counting mode.
GATE is the gate signal. When GATE=1, the T/C (timer/counter) starts and receives dual control requirements TR0/TR1 and INT0/INT1 are high at the same time. When GATE=0, the T/C starts and receives only the control of TR0/TR1. Usually, GATE is set to 0.
Working mode selection
Mode 0: The maximum count value is 2**13-1=8191
Method 1: The maximum count value is 2**16-1=65535
Method 2: TH stores the initial value, and TL is used for counting. Since it is 8 bits, the maximum value is 255. TL keeps counting, and when it reaches 255, it overflows and clears to 0. Then TH gives the initial value to TL and starts counting again. This is the meaning of automatically reloading the initial value.
Mode 3: T0 and T1 are slightly different. Only T0 can be set to mode 3. If it is set to mode 3, TL0 and TH0 are divided into two independent 8-bit counters. TL0 is an 8-bit counter with the same function as mode 0/1. It can be timed and counted.
TH0 only performs a simple internal timing function. It occupies the control bit TR1 and interrupt flag bit TF1 of timer T1. The start and stop are only controlled by TR1 (this mode is not commonly used)
Previous article:The Path to Learning MCU for Beginners (XV) --- Understanding Timers and Counters (I)
Next article:MCU Novice Learning Road (Thirteen) --- External interrupt 0 to achieve LED flashing
Recommended ReadingLatest update time:2024-11-17 00:52
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
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
- Application strategy of memory chip
- Notes on commonly used modules inside MSP430 microcontroller
- Share: CCS dual-core chip DSP end development
- I would like to ask how the parameters of the bandpass filter are derived.
- How to use Wireshark to capture Zigbee packets
- 02. Anlu SparkRoad domestic FPGA evaluation [Learning] DIP switch controls LED
- COCOFLY Tutorial - Crazy Shell Drone Series Flight Control Development Basics [2] Timer (LED flight status light, indicator light flashing)
- esp32 flash micropython firmware plug in computer no such file directory
- Upgrade the water heater to add remote wifi control function, esp8266 remote infrared control water heater start
- Album of Common Functional Module Routines for the National Undergraduate Electronic Design Competition