1. Basic Concepts
1. 89C51 has two counters T0 and T1, and 89C52 has one T2. Each counter is composed of two 8-bit RAM units, that is, each counter is a 16-bit counter, and the maximum counting capacity is 216=65536, remember that it is from 0-65535.
2. Where does the counting source provided to the timer come from? It is a pulse source obtained by dividing the crystal oscillator of the microcontroller by 12. A 12M crystal oscillator provides a pulse time interval of 1us to the counter.
3. Preset number counting method. If each pulse is 1 microsecond, it takes 65.536 milliseconds to count 65536 pulses. But if only 10 milliseconds are needed, what should we do? Just put 55536 in the counter in advance, so we only need to count 65536-55536=10000 times, which is 10 milliseconds.
2. Related registers
Special function register TMOD (89H)
is used for T1 and T0.
GATE C/T M1 M0 GATE C/T M1 M0
GATE: 0 is for internal pulse; 1 is for external pulse.
C/T: 0 for timer; 1 for counter.
M1M0: How it works
M1,M0 Range Characteristics
0,0 working mode 0 13 bits, 8192 times For compatibility with 51's predecessor 48 series.
0,1 working mode 1 16 bits, 65536 times 16 bits, other characteristics are the same as working mode 0, more commonly used.
1,0 Working mode 2 8 bits, 256 times Automatically reload the preset number, the preset number is placed in T0 (or T1)
Of the high 8 bits, only the low 8 bits participate in counting, usually used for
Baud rate generator.
1,1 working mode 3 8 bits, 256 times TH0 can only be used as a timer, TL0 can be used as a timer or counter
Overflow flag: TL0 still uses the original T0 flag.
TH0 borrows the mark of T1, so it can only be used in T1.
Only when T0 is operating in mode 2, T0 can operate in mode 3.
Special function register TCON (88H)
For timer/counter For interrupt
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
TR0/TR1: Timer switch.
TF0/TF1: changes from 0 to 1 after the count overflows
IT0/IT1: Timer/Counter interrupt enable bit.
In today's sharing, we will learn how to use the microcontroller/counter. In the previous sharing, we have come into contact with the concepts of microcontroller clock cycle, machine cycle and instruction cycle. Let's review:
1 clock cycle = 1/crystal frequency M
1 machine cycle = 12 (51 series has 12 clock cycles, some improved MCUs have 1 clock cycle) × clock cycle = 12 (1) × (1/crystal frequency M) = 1us (1/12us, if a 12M crystal is used).
If the 12MHZ crystal oscillator works in the 12T mode of the 51 microcontroller, a timer is needed to perform a 50ms timing operation, and the timer works in mode 1, then how should we write the program?
we know:
A 12M crystal oscillator can generate 1M (106us) machine cycles per second (s).
50ms requires 50×1000us×1us (1 machine cycle) machine cycles = 50,000 machine cycles.
If the timer works in mode 1, it is a 16-bit counter with a maximum value of 65536 (216), which means it can count up to 65536 times. A 50ms timing operation requires 50,000 machine cycles, and 1 machine cycle = 1us, so 50,000 counting operations are required.
In order for the counter timer to work, the timer initial value needs to be pre-filled (initial value = maximum number of timer counts - required number of timer counts). Therefore, the timer initial value needs to be set to 15536 = 65536-50000, that is, 3CB0H (decimal 15536 converted to hexadecimal 3CB0), so TH0 = 0x3c, TL0 = 0xb0, the high bit is the value of TH0, and the low bit is the value of TL0.
If a 11.0592MHZ crystal is used, and other conditions remain unchanged, a 11.0592M crystal can generate 0.9216M machine cycles per second, and 50ms requires 46080 machine cycles. The timer works in mode 1 and is a 16-bit counter with a maximum value of 65536, so the initial value needs to be set to 19456=65536-46080, that is, 4C00H, so TH0=0x4c, TL0=0x00.
From the above analysis, we can derive the formula for setting the initial value of the 16-bit timer/counter, the high 8 bits TH0 and the low TL0:
TH0=(65536-required number of counts)/256=initial value/256;
TL0=(65536-required number of techniques)%6=initial value%6;
What does 256 mean in the formula? Our timer is composed of two 8-bits, so we need to put the difference into these two 8-bits, and count from the lower 8-bit. The lower 8-bit can hold up to 256 machine cycles. When the 256 count is full, the higher 8-bit is used. That is, the higher 8-bit is added by 1 every time the lower 8-bit is full. The lower 8-bit is full as many times as the higher 8-bit is filled. The algorithm is to divide the difference by 256 and take the integer. The rest, which is the number of times less than 256, is placed in the lower 8-bit, which is the remainder of the difference.
Next, let's look at the routine for timer/counter 0 to generate a timer interrupt.
The crystal frequency is 12M, and the procedure is as follows:
#include void timer0_init() { TMOD = 0x01; //The timer works in mode 1, which is a 16-bit counter with a maximum value of 65536 TL0 = 0xb0; //Assign the initial value 0xb0 to TL0 TH0 = 0x3c; //Assign the initial value 0x3c to TH0 TR0 = 1; //Timer starts counting ET0 = 1; //Timer interrupt enabled EA = 1; // Enable general interrupt } void main() { timer0_init(); while(1); } void timer0() interrupt 1 { TH0=(65536-50000)/256; //In the interrupt processing function, TH0 needs to be re-initialized. TL0=(65536-50000)%6; //TL0 needs to be re-initialized in the interrupt handling function. Add the statement we want. } The above example shows how to use timer/counter 0 to implement interrupts. The microcontroller usually has a timer/counter 1, and its programming is similar to that of timer/counter 0. In addition, the use of microcontroller timers/counters is not only reflected in the timer interrupt, but their use is always the same. Once you master one usage, it will not be difficult to master other applications.
Previous article:Introduction to the basic knowledge of IO port operation of 51 single-chip microcomputer
Next article:51 single chip microcomputer digital tube dynamic cycle left shift display mobile phone number
Recommended ReadingLatest update time:2024-11-16 14:33
- 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
- Why is the American Fluke 15B more durable than domestic multimeters? Take a look and you will understand
- Electronic computing software
- MCO (PA8) does not output waveform during DEBUG. What is the reason?
- Patch reverse check
- EEWORLD University ---- Control system of 100 quantum bits
- The difference between USART, USI, USCI and eUSCI serial communication modules in MSP430 microcontroller (updated on March 4, 2019)
- Polarity of EPWMA pulse
- RF Sampling DAC Spurious Troubleshooting and Optimization
- CH549EVT development board test - solution to the problem of data segment exceeding the limit
- Direct code generation under simulink