From the previous section, we have learned that the timer/counter in the microcontroller can have multiple uses, so how can I make them work for the purpose I need? This requires setting the timer/counter control word.
There are two special function registers related to timing/counting in the microcontroller, which are TMOD and TCON. By the way, TMOD and TCON are names. We can directly use these names to specify them when writing programs. Of course, we can also directly use their addresses 89H and 88H to specify them (in fact, using names means directly using addresses, and the assembly software will help you translate).
As we can see from Figure 1, TMOD is divided into two parts, each with 4 bits, which are used to control T1 and T0 respectively. As for what this means, we will introduce it below. As we can see from Figure 2, TCON is also divided into two parts. The upper 4 bits are used for the timer/counter, and the lower 4 bits are used for interrupts (we will not discuss this for now). We mentioned TF1(0) in the last class. When the count overflows, TF1(0) changes from 0 to 1. So TF1(0) is here! So what are TR0 and TR1? Look at the picture in the last class. It is not difficult to get the counting pulse into the counter. There are many levels to pass. At the very least, TR0 (1) must be 1, the switch can be closed, and the pulse can pass. Therefore, TR0 (1) is called the operation control bit. You can use the instruction SETB to set it to start the counter/timer operation, and use the instruction CLR to stop the timer/counter operation. Everything is under your control. Four working modes of timer/counter Working mode 0 The working mode 0 of the timer/counter is called the 13-bit timer/counter mode. It is composed of the lower 5 bits of TL (1/0) and the 8 bits of TH (0/1) to form a 13-bit counter. At this time, the upper 3 bits of TL (1/0) are not used. We use this diagram to discuss several issues: M1M0: The timer/counter has four working modes, which are controlled by M1M0. Two bits have exactly four combinations. C/T: As we said before, the timer/counter can be used for timing or counting. It is up to us to decide what to use it for, or we, the programmer, can decide. If C/T is 0, it is used as a timer (the switch is turned up), and if C/T is 1, it is used as a counter (the switch is turned down). By the way, a timer/counter can be used for either timing or counting at the same time, but not both. This is common sense, and almost no textbooks will mention this, but many beginners will be confused by this. GATE: Look at the picture. When we select the timing or counting working mode, the timing/counting pulse may not reach the counter end. There is a switch in the middle. Obviously, if the switch is not closed, the counting pulse cannot pass. So when does the switch pass? There are two situations. GATE=0. Analyze the logic. GATE is 1 after being negated and enters the OR gate. The OR gate always outputs 1, which has nothing to do with the other input terminal INT1 of the OR gate. In this case, the opening and closing of the switch only depends on TR1. As long as TR1 is 1, the switch is closed and the counting pulse can pass smoothly. If TR1 is equal to 0, the switch is open and the counting pulse cannot pass. Therefore, whether the timing/counting works depends only on TR1. GATE=1, in this case, the switch on the counting pulse path is not only controlled by TR1, but also by the INT1 pin. Only when TR1 is 1 and the INT1 pin is also high, the switch is closed and the counting pulse can pass. This feature can be used to measure the width of the high level of a signal. Think about it, how to measure it? Why only use 13 bits in this mode? Why not use 16 bits? This is a working mode designed for compatibility with the 48 series, the predecessor of the 51 machine. If you feel uncomfortable using it, just use the second working mode. Working method 1 Working mode 1 is a 16-bit timing/counting mode. Just set M1M0 to 01. Other characteristics are the same as working mode 0. Working method 2 Before introducing this method, let's think about a question: In the last class, we mentioned the problem of arbitrary counting and arbitrary timing. For example, I want to count 1000 numbers, but the 16-bit counter will not be full until it reaches 65536. What should I do? After discussion, we came up with a solution to use a preset number. First, put 64536 in the counter, and then 1000 pulses. Isn't that enough? Yes, but what should we do after the counter is full? You know, counting is always repeated. After the count is full on the pipeline, the next count will start immediately. Will the next count still be 1000? When the count is full and overflows, the value in the counter becomes 0 (why, you can refer to the explanation of the previous course), so the next time it will overflow after the count is full of 65536, which does not meet the requirements. What should I do? Of course, the solution is very simple, that is, execute a program every time there is an overflow (this is often necessary, otherwise why overflow?) In this program, you can do the thing of sending the preset number 64536 into the counter. Therefore, whether using working mode 0 or 1, a preset number must be reset after the overflow. Of course, this work takes time. Generally speaking, this amount of time is nothing, but in some situations we still have to care about it, so there is a third working mode - the working mode of automatically reloading the preset number. Since the preset number needs to be automatically loaded, the preset number must be placed in a place, otherwise what should be loaded? So where is the preset number placed? It is placed in the upper 8 bits of T (0/1), so the upper 8 bits cannot participate in the counting? Yes, in working mode 2, only the lower 8 bits participate in the counting, while the upper 8 bits do not participate in the counting and are used to store the preset number. In this way, the counting range is much smaller. Of course, there is always a price to pay for doing anything. The key is to see whether it is worth it. If I don’t need to count so many numbers at all, then I can use this method. Look at Figure 4. Whenever the count overflows, the switch between the upper and lower 8 bits of T (0/1) will be turned on, and the preset number will be counted into the lower 8 bits. This is done automatically by hardware and does not require manual intervention. This mode of operation is often used in baud rate generators (we will explain this in the serial interface). When used for this purpose, the timer is used to provide a time base. There is no need to do anything after the count overflows. The only thing to do is to reload the preset number and start counting again without any delay in between. It can be seen that this task is best accomplished using mode 2. Working method 3 In this operation mode, timer/counter 0 is split into two independent timers/counters. Among them, TL0 can form an 8-bit timer or counter, while TH0 can only be used as a timer. We know that when used as a timer or counter, it needs control, and overflow after full count requires overflow mark. T0 is divided into two, so there must be two sets of control and overflow marks. Where do they come from? TL0 still uses the original T0 mark, while TH0 borrows the mark of T1. In this way, T1 is unmarked and control is available, right? Yes. Normally, T0 is allowed to work in mode 3 only when T1 is running in mode 2 (when the baud rate generator is used). Timing/Counting Range of Timer/Counter Working mode 0: 13-bit timing/counting mode, therefore, the maximum that can be counted is 2 to the 13th power, which is 8192 times. Working mode 1: 16-bit timing/counting mode, therefore, it can count up to 2 to the 16th power, which is 65536 times. Working mode 2 and working mode 3 are both 8-bit timing/counting modes. Therefore, the maximum value that can be counted is 2 to the power of 8, or 256 times. Preset value calculation: subtract the required number of counts from the maximum number of counts. For example, there are 12 boxes on the assembly line. It is required that an action be taken when 12 boxes are reached. If the single-chip microcomputer is used in working mode 0 to control it, what value should be preset? Yes, it is 8192-12=8180. The above is counting. If you understand this principle, the same is true for timing. This has been mentioned in the previous course, so we will not repeat it here. Please refer to the previous routine.
Previous article:Lesson 16: MCU Timer and Counter
Next article:Lesson 18: MCU interrupt system
Recommended ReadingLatest update time:2024-11-16 15:59
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
- Detailed explanation of Zigbee networking principle
- National Undergraduate Electronic Design Competition Commonly Used Modules and Related Devices Data Album
- TOP223 chip
- Effects of Improper Use of Derived Clocks on Logic Timing
- Consulting on segment LCD issues
- Oscilloscope measurement of automotive LIN bus signal and waveform analysis
- Southchip SC8905 evaluation board free evaluation, supports charging and discharging bidirectional operation
- Analysis of the design steps of RS-485 bus interface circuit
-
[NXP Rapid IoT Review] +
NXP Rapid IoT Online IDE Air Quality Test - How to remotely control a two-wheeled balancing vehicle via the Internet?