1. Timing of MCU
When the microcontroller is working, it is controlled by a unified clock pulse. Due to the different number of bytes of instructions, the time required to fetch these instructions is also different. Even if the number of bytes of instructions is the same, the execution time of different instructions may not be the same due to the large difference in execution operations, that is, the number of beats required is different.
Clock cycle: The clock cycle is also called the oscillation cycle, which is defined as the reciprocal of the clock pulse (it can be understood that the clock cycle is the reciprocal of the crystal oscillator connected to the microcontroller. For example, the time cycle of a 12M crystal oscillator is 1/12 us). It is the most basic and smallest time unit in the computer. In one clock cycle, the CPU only completes one basic action. In the 8051 microcontroller, one clock cycle is defined as one beat (represented by P), and two beats are defined as one state cycle (represented by S).
Machine cycle: In computers, for ease of management, the execution process of an instruction is often divided into several stages, each of which completes a task. A machine cycle of the 8051 series microcontroller consists of 6 S cycles (state cycles). As mentioned earlier, a clock cycle is defined as a beat (represented by P), and two beats are defined as a state cycle (represented by S). The machine cycle of the 8051 microcontroller consists of 6 state cycles, that is, one machine cycle = 6 state cycles = 12 clock cycles.
Instruction cycle: The instruction cycle is the time required to execute an instruction, which is generally composed of several machine cycles. Different instructions require different numbers of machine cycles. The relationship between the clock cycle, machine cycle, and instruction cycle is shown below.
2. Concept of timer/counter of 51 MCU
In a single-chip microcomputer, the relationship between pulse counting and time is very close. Every time a pulse is input, the value of the counter will automatically accumulate by 1. As long as the time interval between two adjacent counting pulses is equal, the count value represents the passage of time. Therefore, the timer and counter in the single-chip microcomputer are actually the same physical electronic component, but the counter records what happens outside the single-chip microcomputer (it receives external pulses), while the timer is a very stable counter provided by the single-chip microcomputer itself. This stable counter is the crystal oscillator component connected to the single-chip microcomputer; the crystal oscillator of the MCS-51 single-chip microcomputer is divided by 12 and provides stable pulses to the single-chip microcomputer; the frequency of the crystal oscillator is very accurate, so the time interval between the counting pulses of the single-chip microcomputer is also very accurate.
3. Working principle of timer/counter of 51 single chip microcomputer
The counting pulses input to the add-1 counter have two sources: one is the output pulse of the system clock oscillator divided by 12; the other is the external pulse source input from the T0 or T1 pin.
When used as a timer, the timer counts the number of pulses after the 12-division of the 8051 microcontroller's internal oscillator output, that is, each machine cycle automatically adds 1 to the register value of the timer T0/T1 until it overflows, and then continues to count from 0 after the overflow; therefore, the resolution of the timer is 1/12 of the clock oscillation frequency;
When used as a counter, the external pulse signal is counted through pin T0 (P3.4) or T1 (P3.5). When the input external pulse signal changes from 1 to 0, the counter value automatically increases by 1. Since it takes 2 machine cycles to detect a falling edge from 1 to 0, the sampled level must be maintained for at least one machine cycle. When the crystal frequency is 12MHz, the maximum counting frequency does not exceed 1/2MHz, that is, the period of the counting pulse must be greater than 2 microseconds. The maximum frequency of the counter is generally 1/24 of the clock oscillation frequency.
From this, we can see that no matter whether it is the timer or counter working mode, timer T0 and T1 do not occupy CPU time, unless timer/counter T0 and T1 overflow, it may cause CPU interruption and execute interrupt processing program instead. Therefore, timer/counter is a highly efficient and flexible component in the microcontroller.
51 MCU timer working principle diagram:
From the above figure, we can see that the registers related to the timer are mainly the following: TMOD, TCON, TL0, TH0, TL1, TH1. The following is an introduction to these registers.
16-bit up counter:
It is the core of the timer counter. TL0 and TH0 are the bottom eight bits and the upper eight bits of timer counter 0; TL1 and TH1 are the bottom eight bits and the upper eight bits of timer counter 1; and the upper eight bits and the bottom eight bits can be used separately. The 16-bit adder counter is mainly used when setting the initial value of the timer counter.
TMOD timer working mode and mode register
GATE: Timing operation switch control bit. When GATE=1, the INT0 or INT1 pin is at a high level, and the TR0 or TR1 control bit in TCON is 1, the timer/counter 0 or 1 starts to work. If GATE=0, as long as the TR0 or TR1 control bit is set to 1, the timer/counter 0 or 1 starts to work.
C/T: Selection bit for timer or counter function. When C/T=1, it is a counter, and the counting pulse is input through the external pin T0 or T1. When C/T=0, it is a timer, and the timing pulse is provided by the internal system clock.
M1 M0: T0, T1 working mode selection bit
TCON Timer Control Register
TF1: Timer T1 overflow flag, which can be queried and cleared by the program. TF1 is also an interrupt request source and is cleared by hardware when the CPU responds to the T1 interrupt.
TF0: Timer T0 overflow flag, which can be queried and cleared by the program. TF0 is also an interrupt request source and is cleared by hardware when the CPU responds to the T0 interrupt.
TR1: T1 count enable control bit, when it is 1, T1 count is enabled (timing).
TR0: T0 count enable control bit, when it is 1, T0 count is enabled (timing).
IE1: External interrupt 1 request source (INT1, P3.3) flag. IE1 = 1, external interrupt 1 is requesting an interrupt from the CPU, and is cleared to "0" by hardware when the CPU responds to the interrupt.
IT1: External interrupt source 1 trigger mode control bit. If this bit is 1, it is set to bottom level trigger, and if it is 0, it is set to falling edge trigger.
IE0: External interrupt 0 request source (INT0, P3.2) flag. IE0 = 1, external interrupt 1 is requesting an interrupt from the CPU, and is cleared to "0" by hardware when the CPU responds to the interrupt.
IT0: External interrupt source 0 trigger mode control bit. If this bit is 1, it is set to bottom level trigger, and if it is 0, it is set to falling edge trigger.
51 MCU timer has 4 working modes:
Working mode 0:
The lower 5 bits of TL0 and all 8 bits of TH0 together form a 13-bit timer/counter. After the timer/counter is started, the number of timing or counting pulses is added to TL0, starting from the preset initial value (time constant), and continuously increasing by 1. When TL0 is full, it is carried to TH0 until the 13-bit register is full and overflows. When TH0 overflows, the TF0 flag in TCON is set and an interrupt request is sent to the CPU. The timer/counter hardware will automatically clear the 13-bit register value to 0. If further timing/counting is required, the relevant instructions need to be used to reset the time constant and set the interrupt flag TF0 of the timer/counter to 0.
Working mode 1: the most commonly used timer working mode
Mode 1 is almost identical to Mode 0, the only difference is that registers TH0 and TL0 in Mode 1 together form a 16-bit timer/counter to participate in the operation, so the timing/counting range is larger than that in Mode 0.
Working mode 2: Working mode 2 is particularly suitable for use as a more accurate pulse signal generator.
This mode is also called the automatic reload preset number mode. Sometimes, our timing/counting operation needs to be repeated for many times. If no processing is done when the overflow occurs, then the timing/counting will start from 0 in the second round of timing/counting, which is not what we want. Therefore, to ensure that the timing/counting operation is restarted after each overflow, we need to reload the preset number (time constant) into a certain place. The operation of reloading the preset number is automatically completed by the hardware device and does not require manual intervention. Therefore, this working mode is called the automatic reload preset number mode. In working mode 2, the automatically reloaded preset number is stored in the upper 8 bits of the timer/counter register, that is, stored in TH0, leaving only TL0 to participate in the timing/counting operation.
This working mode is often used in baud rate generator (serial communication), T1 works in serial mode 2; when used in this way, the timer is to provide a time base; after the count overflows, there is no need to do too much, just one thing is to reload the preset number and start counting again, and no delay is required in between.
Working mode 3:
Mode 3 is only applicable to timer/counter T0. When timer T1 is in mode 3, it is equivalent to TR1=0 and counting stops. Since timer/counter T1 does not have working mode 3, if timer/counter T0 is set to working mode 3, TL0 and TH0 will be divided into two independent 8-bit timer/counters.
3. Application of timer/counter of 51 single chip microcomputer
Build a simulation environment under protues:
Here is an introduction to the setting of the initial value of the timer:
Working mode 0: 13-bit timer/counter working mode, can count up to 2 to the 13th power, that is: 8192 times
Working mode 1: 16-bit timer/counter working mode, can count up to 2 to the 16th power, that is: 65536 times
Previous article:Study Notes - 51 MCU Special Function Registers
Next article:Study Notes - 51 MCU Interrupt
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- 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
- [AutoChips AC7801x motor demo board review] + unboxing and GPIO lighting
- About the master: zhang2018yi
- Qorvo has a few things to tell you about WiFi 6
- What kind of switch is this? Where can I buy one?
- Chapter 3: Use of GPIO
- Software Installation
- If you don’t lose weight in spring, you will regret it in summer. Let me share some tips on how to lose weight and keep in shape!
- [Embedded Linux] RK3399 Android7.1 CPU is overheating
- 【Chuanglong TL570x-EVM】Button control LED light
- TI Automotive SPD-SmartGlass Driver Reference Design