“There are two special function registers in the microcontroller that are related to timing/counting, which are TMOD and TCON.

“

There are two special function registers in the microcontroller that are related to timing/counting, which are TMOD and TCON.

By the way, TMOD and TCON are names, we can directly use this name to specify them when writing programs, and of course we can directly use their addresses 89H and 88H to specify them (the actual name is to use the address directly, assembly The software translates it for you).

From Figure 1, we can see that TMOD is divided into two parts, each part is 4 bits. They are used to control T1 and T0 respectively. As for what it means here, we will introduce it below.

From Figure 2, we can see that 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 leave it alone). As for TF1(0), we have already mentioned it in the previous lesson. When the count overflows, TF1(0) changes from 0 to 1. Turns out TF1(0) is here! So what are TR0 and TR1? Look at the picture from the previous lesson.

It is not difficult for the counting pulse to enter the counter. There are layers of switches to pass through. At the very least, TR0 (1) must be 1, the switch can be closed, and the pulse can come over. Therefore, TR0 (1) is called the running control bit, which can be set by the instruction SETB to start the counter/timer running, and the instruction CLR is used to close the work of the timer/counter.

Four working modes of timer/counter

work mode 0

When M1 and M2 in TMOD are set to 0,0, the timer/counter works in mode 0, which is a 13-bit timer/counter mode.

way of working 1

Working mode 1 is a 16-bit timer/counter whose structure and operation are basically the same as those of working mode 0. The only difference is that the counter of working mode 1 is composed of 8 bits of TL0 and 8 bits of TH0 to form a 16-bit counter. Its timing time for:

t=(216－T0 initial value)×clock cycle×12

way of working 2

Mode 2 is an 8-bit automatic loading time constant mode. If Mode 0 and Mode 1 are used for cyclic repetition of timing/counting (such as generating continuous pulse signals), when overflow occurs after each count is full, registers TL0 and TH0 are all 0. So the second count has to reload the initial value of the time. This is not only troublesome but also affects the accuracy. Mode 2 avoids the above defects and its timing is:

t=(28－T0 initial value)×clock cycle×12

way of working 3

Mode 3: The special working mode is only applicable to T0, except that the 8-bit register TL0 is used, its function and operation are exactly the same as mode 0 and mode 1, but another counter TH0 can only work in the internal timer mode. Working mode 3 adds an 8-bit timer to T0.

Timing/counting range of timer/counter

Working mode 0: 13-bit timing/counting mode, therefore, it can be counted up to the 13th power of 2, that is, 8192 times.

Working mode 1: 16-bit timing/counting mode, therefore, it can be counted up to the 16th power of 2, that is, 65536 times.

Working mode 2 and working mode 3 are both 8-bit timing/counting modes. Therefore, it can be counted up to the 8th power of 2, or 256 times.

Preset value calculation: subtract the required number of counts from the maximum count.

Example: One package on the assembly line is 12 boxes. It is required to generate an action every 12 boxes. It is controlled by the working mode 0 of the single-chip microcomputer. What value should be preset? By the way, it is 8192-12=8180.

The above is the count, understand the truth, the timing is the same.

The Links: **EP4CGX15BF14C8N** **KCS3224ASTT-X9**