Timing/Counter for MCS-51

Source: Internet
Author: User

From: http://www.zymcu.com/8051_file/timer01.htm

MCS-51 Microcontroller has two 16-bit Programmable Timing/counter, they have four ways of work, their control word and status are in the corresponding special function registers, by programming the control register, you can easily select an appropriate working method. The following describes their features.

Scheduled/Counter Operation Mode
MCS-51 microcontroller internal timing/Counter structure 1 shows that the timer t0 function register tl0 (low 8 bits) and th0 (high 8 bits) constitute, the timer T1 consists of the feature function register TL1 (8-bit low) and Th1 (8-bit high. The special function register tmod controls the operation mode of the timer register. tcon controls the start and stop counting of the timer T0 and T1, and manages the overflow signs of the timer T0 and T1 at the same time. At the beginning of the program, you must initialize tl0, th0, TL1, and Th1 to define how they work and control the Count of T0 and T1.

The formats of the tmod and tcon registers are as follows:

[1]. Control the tmod in the Timer/Counter mode. The Byte address is 89 h. The format is shown in Table 1:

 
Table 1 tmod register structure
D7 D6 D5 D4 D3 D2 D1 D0
GATA M1 M0 GATA M1 M0
Required T1 Mode Field → T0 t0 Method Field →

[2]. The timer controls the tcon. The Byte address is 88 h and the bit address is 88h-8fh. The 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 purpose of tmod and tcon are described in the following sections. It should be noted that the D0-D3 bit of tcon is related to the interruption, we will explain in the interrupt content that the timing/Counter of the MCS-51 works in four ways and we will discuss them one by one.

Working Mode 0
The time/Counter 0 Operation Mode 0 the logical structure of the circuit is shown in figure 2 (timing/Counter 1 is exactly the same as it), and the working mode 0 is the 13-bit counting structure, the counter consists of all eight bits of th and five low bits of TL. The three bits of Tl are not used. When the value is 0, multiple switches are connected to the 12-frequency output of the oscillating pulse, and the 13-bit counter is counted in one count. This is the method of timed work. When the value is 1, the Count pin (to) is switched on and the external Count pulse is input by yinnan foot. When the counting pulse is changed to a negative hop, the counter is incremented by 1, which is commonly referred to as the counting method.

No matter which method of work, when the TL's low 5-bit overflow, it will carry to th, and when all 13-bit counters overflow, it will overflow to the counter's tf0 carry.

We will discuss the function of the gate position GATA. The GATA bit status determines whether the timer operation control depends on one condition of tr0 or the two conditions of tr0 and int0 pins. When GATA = 1, The int0 signal of the pin is invalid because the GATA signal is blocked and the door. In this case, if tr0 = 1, the analog switch is enabled to enable the counter to perform addition counting, that is, timing/counting. If tr0 is set to 0, the analog switch is disconnected and the count is stopped. The timer/count cannot work.

When GATA = 0, the output end of the gate is determined by the status of the tr0 and int0 levels. If tr0 = 1, int0 = 1, and the output of the door is 1, the timer/Counter count is allowed, in this case, the operation control is jointly controlled by the tr0 and int0 conditions. The tr0 is to determine the operation control bit of the timer/counter, and is set by the software or the "0 ".

As described above, tf0 indicates the overflow status of the timer/counter. When the overflow occurs, it is set by the hardware. When tf0 overflow is interrupted by the CPU, the hardware is "0" when the switch is interrupted ", tf0 can also be queried and cleared by the program ".

In working mode, the counter's counter value range is:

1 To 8192 (213)

The formula for calculating the scheduled time is as follows:

(213-initial counting value) ╳ 12

Or (213-initial count value) Renew the machine cycle

The time unit is the same as the crystal cycle or machine cycle.

If the single-chip crystal oscillator is set to 6.000 MHz, the minimum timing time is:

[213-(213-1)] Listen 1/6 then 10-6 then 12 = 2 then 10-6 (S) = 2 (US)

(213-0) Then 1/6 then 10-6 then 12 = 16384 then 10-6 (S) = 16384 (us ).

Work Mode 1
When M1, M0 = 01, the timer/Counter is in the working mode 1. At this time, the equivalent circuit 3 of the timer/and counter shows that the timer 0 is still used as an example, the timer 1 is exactly the same as the timer 1.

It can be seen that the difference between mode 0 and Mode 1 is only that the number of digits of the counter is different. Mode 0 is 13 BITs, and Mode 1 is 16 bits, and th0 is taken as the 8 bits in height, tl0 is an 8-bit low, and the control status characters (Gata, tf0, and tr0) are the same as those in method 0.

In method 1, the counter's counter value range is:

1 To 65536 (216)

When the scheduled operation mode is 1, the formula for calculating the scheduled time is:

(216-initial counting value) ╳ 12

Or (216-initial count value) Renew the machine cycle

The time unit is the same as the crystal cycle or machine cycle.

If the single-chip crystal oscillator is set to 6.000 MHz, the minimum timing time is:

[213-(216-1)] Listen 1/6 then 10-6 then 12 = 2 then 10-6 (S) = 2 (US)

(216-0) Then 1/6 then 10-6 then 12 = 131072 then 10-6 (S) = 131072 (us ).

Working Method 2
When m1m0 = 10, the timer/Counter is in the working mode. 2. The equivalent resistance of the timer is 4. We still take the Timer/Counter 0 as an example. The timer/Counter 1 is exactly the same as it.
The biggest feature of working mode 0 and Working Mode 1 is that after counting overflow, the counter is all 0, so there is a problem of setting the initial value repeatedly during cyclic timing or cyclic counting applications, this causes a lot of inconvenience to the program design and affects the timing accuracy. Work Mode 2 is set to address this problem. It has the automatic re-loading function, that is, the initial value of the automatic loading count, therefore, some documents call it auto-reload. In this way, a 16-bit counter is divided into two parts, namely, using tl0 as the counter and th0 as the preset register. during initialization, the initial counting values are loaded into tl0 and th0 respectively, when a Counter Overflow occurs, manual intervention is not required as in method 0 and method 1. The software re-assigns the value. Instead, the preset register th automatically reloads the counter tl0 in hardware mode.

During program initialization, the initial values are assigned to tl0 and th0 at the same time. When the tl0 count overflows, the initial values in the preset register th0 are loaded to tl0 at the same time, and tl0 is re-counted. This saves the trouble of constantly assigning values to Counters by programs, and increases the counting accuracy. However, this method also has a disadvantage, that is, the counting structure is only 8 bits, the counting value is limited, and the maximum value is only 255. Therefore, this method is suitable for repeated counting applications. For example, we can use this counting method to generate an interruption and generate a fixed frequency pulse. It can also be used as a baud rate transmitter for serial data communication.

Work Mode 3
When m1m0 = 11, the timer/Counter is in the working mode 3. At this time, the equivalent circuit 3 of the timer/and counter is still taking the timer 0 as an example. It is worth noting that, in work mode 3, the work mode of Timer/Counter 1 is different. Next we will discuss it separately.

In working mode 3, the timer/Counter 0 is split into two independent 8-bit counters tl0 and th0. Tl0 can be used either as a counter or as a timer. Each control bit and pin signal of the timer/Counter 0 are all used by tl0. Its functions and operations are exactly the same as method 0 or method 1. Th0 does not have so many "Resources" to be used. It can only be used as a simple timer, and the control bit of Timer/Counter 0 has been occupied by tl0, therefore, you can only borrow the tr1 and TF1 control bits of the timer/Counter 1, that is, the TF1 is replaced by the count overflow, and tr1 is responsible for controlling the start and stop of th0 timing. For the equivalent circuit, see Figure 6. Because tl0 can be used as both a timer and a counter, th0 can only be used as a timer instead of a counter. Therefore, in Mode 3, the timer/Counter 0 can constitute two timers, one timer and one counter.

If the Timer/Counter 0 is used in work mode 3, the timer/Counter 1 is inevitably restricted because some of its control spaces have been borrowed by the Timer/counter, the circuit can only work in Mode 0, Mode 1, or Mode 2. For the equivalent circuit, see Figure 6.

In this case, Timer/Counter 1 is usually used as the baud rate generator of the serial port to determine the serial communication rate, because no TF1 has been borrowed by Timer/Counter 0, only the overflow of the count can be sent directly to the serial port. When used as a baud rate generator, you only need to set the working mode to run automatically. To stop its operation, you need to input a control word that sets it to Mode 3. This is because the Timer/counter itself cannot work in Mode 3, if you set it to method 3, it will naturally stop working.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.