There are two 16-bit programmable timers/counters in the MCS-51 microcontroller. They have four working modes. Their control words and states are in the corresponding special function registers. By programming the control registers, the appropriate working mode can be easily selected. Below we explain their characteristics.
Working mode of timer/counter The structure of the timer/counter inside the MCS-51 microcontroller is shown in Figure 1. Timer T0 is composed of characteristic function registers TL0 (low 8 bits) and TH0 (high 8 bits), and timer T1 is composed of characteristic function registers TL1 (low 8 bits) and TH1 (high 8 bits). Special function register TMOD controls the working mode of the timer register, and TCON is used to control the start and stop counting of timers T0 and T1, and manage the overflow flags of timers T0 and T1. At the beginning of the program, TL0, TH0, TL1 and TH1 need to be initialized and programmed to define their working mode and control the counting of T0 and T1.
The formats of the two special function registers TMOD and TCON are shown in the following table:
[1]. The timer/counter mode control word TMOD, the byte address is 89H, and its format is shown in Table 1:
Table 1 TMOD register structure
D7
D6
D5
D4
D3
D2
D1
D0
Snake
M1
M0
Snake
M1
M0
←T1 Mode Field→
←T0 mode field→
[2]. The timer control register TCON has a byte address of 88H and a bit address of 88H-8FH. Its format is shown in Table 2:
Table 2 TCON structure
D7
D6
D5
D4
D3
D2
D1
D0
TF1
TR1
TF0
TR0
IE1
IT1
IE0
IT0
The meaning and use of each bit of TMOD and TCON will be introduced in the following chapters. It should be noted that the D0-D3 bits of TCON are related to interrupts, which will be explained in the content of interrupts. The MCS-51 timer/counter has four working modes, which we will discuss one by one.
Working mode 0 The working mode 0 circuit logic structure of timer/counter 0 is shown in Figure 2 (timer/counter 1 is exactly the same as it). Working mode 0 is the working mode of 13-bit counting structure. Its counter is composed of all 8 bits of TH and the lower 5 bits of TL, and the upper 3 bits of TL are not used. When = 0, the multiplexer connects to the 12-frequency output of the oscillation pulse, and the 13-bit counter counts in turn. This is the timing working mode. When = 1, the multiplexer connects to the counting pin (To), and the external counting pulse is input from the silver south pin To. When the counting pulse has a negative jump, the counter increases by 1, which is what we often call the counting working mode.
Regardless of the working mode, when the lower 5 bits of TL overflow, they will carry to TH, and when all 13-bit counters overflow, they will carry to the counter overflow flag bit TF0.
Let's discuss the function of the gate bit GATA. The state of the GATA bit determines whether the timer operation control depends on one condition of TR0 or two conditions of TR0 and INT0 pins. When GATA=1, the GATA signal blocks the AND gate, making the pin INT0 signal invalid. At this time, if TR0=1, the analog switch is turned on, allowing the counter to perform addition counting, that is, timing/counting work. If TR0=0, the analog switch is disconnected, counting stops, and the timing/counting cannot work.
When GATA=0, the output of the AND gate is determined by the state of the TR0 and INT0 levels. At this time, if TR0=1, INT0=1, and the AND gate output is 1, allowing the timer/counter to count. In this case, the operation control is controlled by the two conditions of TR0 and INT0. TR0 is the operation control bit that determines the timer/counter, which is set or cleared to "0" by software.
As mentioned above, TF0 is the overflow status flag of the timer/counter. It is set by hardware when overflow occurs. When the TF0 overflow interrupt is responded by the CPU, the hardware clears it to "0" when the interrupt is entered. TF0 can also be queried and cleared to "0" by the program.
In the working mode, the counting value range of the counter is:
1-8192 (2 13 )
When it is in the timing working mode, the calculation formula of the timing time is:
Working mode 1 When M1, M0 = 01, the timer/counter is in working mode 1. At this time, the equivalent circuit of the timer/counter is shown in Figure 3. Still taking timer 0 as an example, timer 1 is exactly the same.
It can be seen that the difference between mode 0 and mode 1 is the number of bits of the counter. Mode 0 is 13 bits, while mode 1 is 16 bits, with TH0 as the high 8 bits and TL0 as the low 8 bits. The relevant control status words (GATA, , TF0, TR0) are the same as mode 0.
In working mode 1, the counting value range of the counter is:
1-65536 (2 16 )
When it is timing working mode 1, the calculation formula of the timing time is:
Working mode 2 When M1M0=10, the timer/counter is in working mode 2. At this time, the equivalent resistance of the timer is shown in Figure 4. We still take timer/counter 0 as an example, and timer/counter 1 is exactly the same.
The biggest feature of working mode 0 and working mode 1 is that after the count overflows, the counter is all 0, so there is a problem of repeatedly setting the initial value when the cyclic timing or cyclic counting is applied, which brings a lot of inconvenience to program design and also affects the timing accuracy. Working mode 2 is set to address this problem. It has an automatic reload function, that is, automatically loads the initial value of the count, so some literature also calls it the automatic reload working mode. In this working mode, the 16-bit counter is divided into two parts, that is, TL0 is used as the counter and TH0 is used as the preset register. When the count overflows, it no longer requires "manual intervention" like mode 0 and mode 1, and the software reassigns the value. Instead, the preset register TH automatically reloads the counter TL0 by hardware.
When the program is initialized, TL0 and TH0 are assigned initial values at the same time. When the TL0 count overflows, TF0 is set and the initial value in the preset register TH0 is loaded to TL0, and TL0 counts again. This is repeated, which saves the trouble of the program constantly assigning values to the counter, and the counting accuracy is also improved. But this method also has its disadvantages, that is, the counting structure is only 8 bits, the counting value is limited, and the maximum can only be 255. Therefore, this working mode is very suitable for those applications with repeated counting. For example, we can generate interrupts through this counting method to generate a fixed frequency pulse. It can also be used as a baud rate transmitter for serial data communication.
Working mode 3 When M1M0=11, the timer/counter is in working mode 3. At this time, the equivalent circuit of the timer/counter is shown in Figure 3. Still taking timer 0 as an example, it is worth noting that in working mode 3 mode, the working mode of timer/counter 1 is different from that of working mode 3. Let's discuss them separately below.
In working mode 3, timer/counter 0 is split into two independent 8-bit counters TL0 and TH0. TL0 can be used as a counter or a timer, and all control bits and pin signals of timer/counter 0 are used by it. Its function and operation are exactly the same as those of mode 0 or mode 1. TH0 does not have so many "resources" to use, and can only be used as a simple timer. Moreover, since the control bits of timer/counter 0 are occupied by TL0, it can only borrow the control bits TR1 and TF1 of timer/counter 1, that is, TF1 is set by counting overflow, and TR1 is responsible for controlling the start and stop of TH0 timing. See Figure 6 for the equivalent circuit. Since TL0 can be used as both a timer and a counter, and TH0 can only be used as a timer but not a counter, in mode 3, timer/counter 0 can form two timers or one timer and one counter.
If timer/counter 0 works in working mode 3, then the working mode of timer/counter 1 is inevitably subject to certain restrictions, because some of its control bits have been borrowed by the timer/counter and can only work in mode 0, mode 1 or mode 2. See Figure 6 for the equivalent circuit.
In this case, timer/counter 1 is usually used as the baud rate generator of the serial port to determine the rate of serial communication, because there is no TF1 borrowed by timer/counter 0, and the count overflow can only be sent directly to the serial port. When used as a baud rate generator, you only need to set the working mode and it will run automatically. If you want to stop it from working, you need to send a mode control word to set it to mode 3. This is because the timer/counter itself cannot work in mode 3. If you force it to be set to mode 3, it will naturally stop working.
"This is because the timer/counter itself cannot work in mode 3. If you force it to work in mode 3, it will naturally stop working." This is a good sentence. In fact, it doesn't matter whether it works or not at this time. You just need to disable serial port interrupts (CLR ES) or prohibit receiving data (CLR REN).