In the previous lecture, we introduced the working principle and development process of single-chip microcomputers by describing an experiment of using a single-chip microcomputer to control an external LED flashing. This lecture will introduce two very important resources inside the single-chip microcomputer - the timer/counter and the interrupt system. Through this lecture, readers can understand the working principle of the timer and the interrupt system of the single-chip microcomputer.
Therefore, the timer counting program and interrupt service program are designed.
1. Principle Introduction
First, let's take an alarm clock as an example. If we set it to go off in one minute, the second hand needs to move one circle (60 times). That is, one minute is converted into the number of times the second hand moves, which is the number of counts. When it reaches 60 times, the alarm goes off, and each count lasts for 1 second.
The timer/counter inside the microcontroller is similar to an alarm clock. You can set the time to be timed through programming, and perform corresponding operations when the timed time is reached. So how long does it take to count once inside the microcontroller? The clock pulse input by the 51 microcontroller is obtained by dividing the output of the crystal oscillator by 12, so the timer can also be regarded as a counter for the computer machine cycle. Because each machine cycle contains 12 oscillation cycles, the timer adds 1 for each machine cycle, and the input clock pulse can be regarded as a machine cycle signal. Therefore, its frequency is 1/12 of the crystal frequency. If the crystal frequency is 12MHz, the time for the timer to receive an input pulse is exactly 1μs. In this experimental kit, a 11.0592M crystal oscillator is used, so the time for each input pulse to be received is about 1.085μs. Achieving precise timing is very important in actual project applications, because it is often necessary to use precise timing for a period of time, and then perform the corresponding task when the timing time is reached.
So how do you program to achieve timing? First, let's briefly introduce the timer resources in the microcontroller (STC89C52) on this experimental board. STC89C52 has three timers/counters, namely T0, T1 and T2. T0 and T1 work in the same way, so they are introduced together.
The working mode of T2 is slightly different, so I won't introduce it here. There is a practical application in the experiment kit CD. At the same time, the timer and counter in the microcontroller are multiplexed. The counter records the number of external pulses, while the timer is a very stable counting source provided by the internal clock of the microcontroller. In this lecture, T0 and T1 are used as timers for example.
After understanding the timer resources in the microcontroller, let's introduce the timer registers in detail. TMOD (see Table 1), TCON (see Table 3) are connected to timer T0 and timer T1 through internal buses and logic circuits. TMOD is used to set the working mode of the timer, and TCON is used to control the start and stop of the timer, and mark the overflow and interruption of the timer. After the working mode of the timer is set and the timer is started, the timer will work independently according to the set working mode and no longer occupy the CPU's operating time. The current operation of the CPU can only be interrupted when the counter is full and overflows.
Table 1 TMOD register
The meaning of each bit in the table (from left to right, from high to low) is as follows.
The lower 4 bits of TMOD are the mode field of timer 0, and the upper 4 bits are the mode field of timer 1. Their meanings are exactly the same.
M1 and M0: working mode control bits, their definitions are shown in Table 2 (where i=0, 1).
Table 2 Timer working mode control bits
: Function selection bit.
[page]
It is worth noting that the TMOD register cannot be bit-addressed, and can only be set with byte instructions to define the working mode of timer 1 in the upper 4 bits or the working mode of timer 0 in the lower 4 bits. Moreover, all bits of TMOD are set to 0 when reset.
Table 3 TCON register
The meaning of each bit in the table (from left to right, from high to low) is as follows.
(1) TF1: Timer 1 overflow flag. When Timer 1 overflows, the hardware automatically sets TF1=1 and sends a Timer 1 interrupt request to the CPU. The CPU responds when the interrupt is enabled. After entering the interrupt service routine, the hardware automatically clears it to 0. When the interrupt is masked, TF1 can be used for query testing. At this time, it can only be cleared to 0 by software.
(2) TR1: Timer 1 operation control bit. Set to 1 or clear to 0 by software to start or stop Timer 1.
When GATE=1 and is high, TRI is set to 1 to start timer 1; when GATE=0, TR1 is set to 1 to start timer 1.
(3) TF0: Timer 0 overflow flag. Its function and operation are the same as TF1.
(4) TR0: Timer 0 operation control bit. Its function and operation are the same as TR1.
(5) IE1: External interrupt 1 ( ) request flag.
(6) IT1: External interrupt 1 trigger mode selection bit.
(7) IE0: External interrupt 0 ( ) request flag.
(8) IT0: External interrupt 0 trigger mode selection bit.
It is worth noting that the lower 4 bits in TCON are used to control external interrupts and have nothing to do with the timer/counter, which will be mentioned in later lectures. When the system is reset, all bits in TCON are cleared to 0.
In the above text, we mentioned timer overflow and interrupt. What is timer overflow? We can understand it this way: drip water into a basin, the water drops continue to fall, the water in the basin continues to fill up, and eventually there will be a drop of water that fills the basin (this is equivalent to counting to the maximum value). At this time, if another drop of water falls, the water will overflow, which is "overflow". Of course, water overflows and flows to the ground, and the timer overflow will make TF0 become "1". Once TF0 changes from 0 to 1, an interrupt will be generated. An interrupt is a process in which the CPU pauses the currently executing program due to the occurrence of a certain event and executes a program to handle the event instead. After the execution of the program is completed, the CPU continues to execute the paused program. This is just like when we are doing something, someone comes to ask for help, we stop what we are doing to help, and come back to do the original thing after the work is done. According to the different causes of interrupts, or the different conditions under which the CPU responds to interrupts, interrupts can also be divided into maskable interrupts (that is, we can refuse to help others and continue to do our own things) and non-maskable interrupts (we are tired of doing things and must rest).
After understanding the principle of interruption, let's look at the interrupt enable control register IE (see Table 4) and the interrupt priority register IP (see Table 5).
[page]
Table 4 IE register
EA: Interrupt enable control bit. EA=0, disable all interrupts; EA=1, enable all interrupts, but whether to allow interrupt requests from each interrupt source depends on the state of the interrupt enable control bit of each interrupt source. Beginners often forget to enable all interrupts, which results in being unable to enter the interrupt source.
ET2: Interrupt enable bit for timer/counter T2.
ES: Serial port interrupt enable bit.
ET1: Interrupt enable bit for timer/counter T1.
EX1: Interrupt enable bit for external interrupt 1 (INT1).
ET0: Interrupt enable bit for timer/counter T0.
EX0: Interrupt enable bit for external interrupt 0 (INT0).
When the above 7 interrupt enable control bits are 0, interrupts are disabled, and when they are 1, interrupts are enabled.
Table 5 IP Register
PT2: Timer/Counter T1 interrupt priority control bit.
PS: Serial port interrupt priority control bit.
PT1: Timer/Counter T1 interrupt priority control bit.
PX1: External interrupt 1 priority control bit.
PT0: Timer/Counter T0 interrupt control bit.
PX0: External interrupt 0 interrupt priority control bit.
The above 6 interrupt priority control bits are low-level interrupts when "0" and high-level interrupts when "1". If several interrupt sources with the same priority level request interrupts from the CPU at the same time, the CPU will query the logic circuits in the internal sequence and determine which interrupt request to respond to according to the natural priority order. The natural priority is formed by hardware, and its priority level from high to low is external interrupt 0, timer/counter T0, external interrupt 1, timer/counter T1, serial port interrupt, timer/counter T2.
So far, the important registers related to timer control have been introduced. Next, we will verify the practice by writing a program.
2. Circuit Detail
This lecture uses the same circuit as the previous lecture, so I will not go into details here (see Figure 1).
Figure 1 Timer application experiment circuit diagram
[page]
3. Programming
Timer 0 application test program (control D1 flashing).
#include
#define led P0_0 (2)
unsigned char count=0; (3)
void main(void) (4)
{
TMOD=0X01; (5)
TH0=(65536-50000)/256; (6)
TL0=(65536-50000)%256; (7)
EA=1; (8)
ET0=1; (9)
TR0=1; (10)
PT0=1; (11)
while(1); (12)
}
void timer0 (void) interrupt 1 (13)
{
TH0=(65536-50000)/256; (14)
TL0=(65536-50000)%256;(15)
count++; (16)
if (count == 10) (17)
{
count = 0; (18)
led = !led; (19)
}
}
1. Detailed description of the procedure:
(1) Header file inclusion. The P0_0 called by the program next is a register address defined in the header file. Before operating the register inside the microcontroller, its source should be stated. Interested readers can look at the contents of the AT89X52.h file.
(2) Macro definition of led makes it easier to understand and modify the program. Name the P0_0 port as led, so that led can be used instead of P0_0 port to perform operations in the program.
(3) Define an 8-bit global variable.
(4) Main function entry. The main function does not pass parameters or return values.
(5) Set timer 0 to operate in mode 1, which is a 16-bit counter.
(6) Assign an initial value to the high 8 bits of the timer and round it to the integer of 256.
(7) Assign an initial value to the lower 8 bits of the timer and take the modulo 256.
(8) Turn on the general interrupt.
(9) Enable timer 0 interrupt.
(10) Timer 0 starts counting.
(11) Set the timer 0 interrupt as the priority interrupt.
(12) Infinite loop, waiting for interruption.
(13) Timer 0 interrupt service function entry.
(14) Assign an initial value to the high 8 bits of the timer and round it to the integer of 256.
(15) Assign an initial value to the lower 8 bits of the timer and take the modulo 256.
(16) Add 1 to the variable count.
(17) If count increases to 10.
(18) The count variable is cleared to 0.
(19) LED output is inverted.
[page]
2. Program flow chart and experimental phenomena
The program flow is shown in Figure 2. After the program is compiled and downloaded to the microcontroller and run, you can see that the LED light connected to the P0_0 port on the experimental board flashes regularly, as shown in Figure 3. The time of on and off can be calculated as 10×50000×1.085μs=542.5ms.
The actual time is a few milliseconds longer than this because the instruction time for conditional judgment and program call is not included. In situations where extreme precision is required, actual fine-tuning should be performed.
Figure 2 Flowchart of main program and interrupt service function
Figure 3 Experimental effect
IV. Conclusion
This lecture mainly introduces the internal timer and interrupt system of 51 MCU and writes the first simple timer experiment program. Through this lecture, you can find that the timer and interrupt system in the MCU are not difficult to learn. As long as you remember the relevant operation steps of the application and practice several times, you can master the use of these resources very skillfully. Since this lecture contains a lot of theoretical content, I hope readers will take more time to write it down or familiarize themselves with it. In the next lecture, I will introduce the serial port communication of the MCU and give examples. Please stay tuned.
Previous article:Minimum System Composition and I/O Output Control of Single Chip Microcomputer
Next article:Learn 51 MCU timer
Recommended ReadingLatest update time:2024-11-16 19:43
- 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
- 【Beetle ESP32-C3】4. Luat-OS environment deployment
- A brief discussion on the differences between x86, DSP and SPARC
- ESP32-C3 RISC-V microcontroller information leak
- Review summary: Flathead RVB2601
- EEWORLD University ---- Using lithium-ion batteries more safely and efficiently - Battery Management System (BMS) Solution
- Talk: Small capital investment
- Where does the EG8010 dead time go?
- Downloads|Selected Courseware & Latest Materials from Tektronix 2019 Semiconductor Seminar
- Voltage controlled current source composed of opa547
- [GUIX①, one of threadX components] guix text button