The use of STC microcontroller timer can be said to be very simple, as long as you understand the principle and have a basic understanding of C language. The key points are as follows:
1. You must know the original form of the English abbreviation, so you don't have to remember the name of the register.
Understanding is the best way to remember. A good textbook will give the original forms of all English abbreviations.
2. Try to use visual methods to remember
For example, the functions of each bit of the two registers TCON and TMOD are usually shown in a diagram in the tutorial. You can keep recalling the image of the diagram while studying.
TMOD: Timer/Counter Mode Control Register (TIMER/COUNTER MODE CONTROL REGISTER) The
timer/counter mode control register TMOD is an 8-bit register defined bit by bit, but can only be addressed using bytes, and its byte address is 89H.
Its format is:
the lower four bits define the timer/counter C/T0, and the upper four bits define the timer/counter C/T1. Explanation of each bit:
GATE - gate control.
When GATE=1, the external interrupt pins INT0 and INT1 start timers T0 and T1.
When the INT0 pin is high, TR0 is set to start timer T0;
when the INT1 pin is high, TR1 is set to start timer T1.
When GATE=0, only TR0 and TR1 are set to start timers T0 and T1 respectively.
C/T - function selection bit
C/T=0 is the timing function, and C/T=1 is the counting function.
When set, select the counting function, and when cleared, select the timing function.
M0, M1——Mode selection function
Since there are 2 bits, there are 4 working modes:
M1M0 Working mode Counter mode TMOD (set timer mode)
0 0 Mode 0 13-bit counter TMOD=0x00
0 1 Mode 1 16-bit counter TMOD=0x01
1 0 Mode 2 Automatic reload 8-bit counter TMOD=0x02
1 1 Mode 3 T0 is divided into 2 8-bit independent counters, T1 is non-interrupt reload 8-bit counter TMOD=0x03
MCU timer 0 is set to working mode 1 as TMOD=0x01
Here we must know that the T of TMOD means TIMER/COUNTER, and MOD means MODE. As for the function of each bit, you only need to remember the chart and know the prototype of each English abbreviation. When
using TMOD in the program, first recall the chart immediately, and sort out the meaning of each bit according to the original form of the abbreviation. If the meaning is not very clear, check the manual. After a few times, the chart of TMOD will be in your mind.
8-bit GATE bit, which means gate.
7-bit C/T Counter/Timer
6-bit M1 Mode 1
5-bit M0 Mode 0
TCON: Timer/Counter Control Register (TIMER/COUNTER CONTROL REGISTER)
TMOD is divided into 2 sections, TCON control is more precise, divided into four sections, in this article only the upper four sections are used.
TF0 (TF1) - count overflow flag, when the counter count overflows, this position is 1.
TR0 (TR1) - timer operation control bit
When TR0 (TR1) = 0, stop the timer/counter
When TR0 (TR1) = 1, start the timer/counter
IE0 (IE1) - external interrupt request flag
When the CPU samples P3.2 (P3.3) and a valid interrupt request appears, this bit is set to 1 by hardware. After the interrupt response is completed and the interrupt service is turned to, it is automatically cleared to 0 by hardware.
IT0 (IT1) - external interrupt request signal mode control bit
When IT0 (IT1) = 1 pulse mode (negative jump on the trailing edge is valid)
When IT0 (IT1) = 0 level mode (low level is valid) This bit is set to 1 or cleared to 0 by software.
TF0 (TF1) - Count overflow flag
When the counter overflows, this bit is set to 1 by hardware. When the interrupt service is turned, the hardware automatically clears it to 0. There are two cases for the use of the count overflow flag: when the interrupt mode is used, it is used as an interrupt request flag; when the query mode is used, it is used as a query status bit.
Pay attention to the memory method and understand the original form of the word, so that you will never confuse TF and TR. The F in TF is the F in Overflow. There is a famous IT knowledge Q&A community abroad, the website is http://stackoverflow.com/
The R in TR is Run. The default is 0 not to run, of course it must be set to 1 to run.
Calculation of the delay time of the STC microcontroller STC89C52RC timer
The delay time should be calculated according to the crystal frequency, and different boards may be different.
Clock cycle:
1/clock source. On my current board, the crystal frequency is 11.0592M, which means the clock cycle is 1/11059200 seconds.
Machine cycle:
Generally, the 51 microcontroller has 12 clock cycles, and my board is 12/11059200 seconds .
The longest time for a single timing:
If it is a 16-bit counter, the maximum value of 16 bits is 65535, and a total of 65536 times can be counted. The basic constants must be remembered, and the maximum value of 8 bits is 255, which can be counted 256 times. Also remember the value represented by each bit on the 8-bit.
12 * 65536/11059200 = 0.0711 s, that is, the timing within 71 ms can be completed with a single timing. If the timing time exceeds 71 ms, it will be looped.
How many machine cycles are needed for one timing:
Calculation formula: Timing seconds/machine cycle
For example, if I want to time 1 second, 1/(12/11059200) = 921600 times. The maximum count of a 16-bit counter is 65536 times, and 921600 times has long been exceeded. We can time 10 ms each time, and we can time 1 second by looping 100 times. 1 s is reduced by 100 times to 10 ms, which means we need to count 9216 times each time.
The initial value of the counter:
When timing 10 ms, if the counter starts counting from 0, we don’t know when it reaches 9216 times. So it should count 9216 times. The maximum count of a 16-bit counter is 95536 times, and then it overflows. Once it overflows, the TF bit of TCON will be set to 1. As long as we frequently check the TF bit, we can know when the 10ms timing is completed.
Calculation formula: Counter initial value = maximum count times - required count times
If the timing is 10 ms, the initial value of the counter is 65536 - 9216.
Calculate the high and low bits of the counter:
A 16-bit counter is composed of two 8-bit bits, and the maximum count times of 8 bits is 256. So:
Counter high bit = initial value/256
Counter low bit = initial value%256
Previous article:51 microcontroller timer interrupt control light 1S program
Next article:Hardware expansion method of 51 single chip microcomputer timer range
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- Why Japan failed to kill South Korea's semiconductor industry
- How to select all device bit numbers or nominal values in the SCH file for AD1904 version
- Protocols and services required for the WiFi authentication process
- Testing methods and diagnostic analysis for DSP-containing circuit boards
- Fundamentals of Circuits and Analog Electronics Technology
- LAUNCHXL-F28379D comprehensive routine
- 【DWIN Serial Port Screen】One of the Nucleic Acid Sampling and Registration Systems
- Live broadcast at 10 am today [TI's new generation MSP430 helps low-cost ultrasonic water meter]
- Playing with Zynq Serial 24 - Porting of User-Defined IP Cores
- Running PyBasic with CircuitPython on PyPortal