Timer
Overview
In detection and control, count function and timing function are used in many occasions. There are three main methods to realize these functions of timing / counting: software timing, hardware timing of digital circuits, and programmable timing / counters.
-
Software timing
Software timing is a cyclic program, and the time required to execute this program segment is the delay time. -
Digital circuit hardware timing
This kind of hardware timing uses small-scale integrated circuit devices, such as using a 555 timing chip to form a timing circuit, which does not occupy CPU time, but the timing of this circuit depends on the component parameters in the circuit. After the hardware circuit is connected, to change the timing time, it is necessary to change the electronic components in the circuit, which is very inconvenient to use. -
Programmable timer / counter
Programmable timer / counter is developed to facilitate the design and application of microcomputer systems. It is not only hardware timing, but also can easily determine and change the timing time through software The timing and counting requirements.
###structure
The timer / counter T0 consists of special function registers TH0, TL0,
The timer / counter T1 consists of special function registers TH1 and TL1.
There are 2 working modes of timer and counter , 4 working modes (mode 0, mode 1, mode 2 and mode 3).
- The counter mode is to count the external pulses added to the two pins T0 (P3.4) and T1 (P3.5).
- The timer working mode is to count the internal pulse signal of the microcontroller's clock oscillator signal after the on-chip frequency division of 12 .
### 4 working methods
M1M0 | the way | Features |
---|---|---|
0 0 | 0 | 13-bit timer / counter |
0 1 | 1 | 16-bit timer / counter |
1 0 | 2 | 8-bit timer / counter automatically loaded with time constant |
1 1 | 3 | Divide T0 into two 8-bit independent counters; stop working when setting mode 3 to T1 |
### Requirements for externally input count signals
-
When the timer / counter works in the counter mode, the count pulse comes from the external input pin T0 or T1.
-
When the input signal produces a negative transition, the value of the counter increases by one.
-
During S5P2 of each machine cycle, the external input pin T0 or T1 is sampled.
### Timer / Counter Programming and Application
step
(1) Determine the working mode, that is, write the control word to the mode control register TMO D.
(2) Calculate the initial value of the timer / counter, and write the initial value to the registers TL and TH.
(3) Set the initial value to the interrupt control register IE as needed to determine whether to open the timer interrupt.
(4) Set TRx in the operation control register TCON to start the timer / timer.
Exercise
\ 2. How many programmable timers / counters are there in AT89S51 microcontroller? What kinds of working modes can they have? What are the working methods? How to choose and set? What are the characteristics of each?
The four working modes of timer / counter are determined by M1 M0 two bits in TMOD, as shown in the following table.
M1M0 | the way | Features |
---|---|---|
0 0 | 0 | 13-bit timer / counter |
0 1 | 1 | 16-bit timer / counter |
1 0 | 2 | 8-bit timer / counter automatically loaded with time constant |
1 1 | 3 | Divide T0 into two 8-bit independent counters; stop working when setting mode 3 to T1 |
7. The crystal frequency of the AT89S51 MCU is 6MHz. If the timer value is required to be 0.1ms and 10ms respectively, and the timer 0 works in mode 0, mode 1 and mode 2, what should be the initial value of the timer?
**** Answer: **** (1) 0.1ms
Way 0:
0.1×10-3=(213-X)×12/(6×106)
So: X = 8142 = 1111111001110B
The lower 5 bits of T0 01110B = 0EH
The upper 8 bits of T0: 11111110B = FEH
Method 1: 0.1 × 10-3 = (216-X) × 12 / (6 × 106)
So: X = 65486 = FFCEH
Method 2: 0.1 × 10-3 = (28-X) × 12 / (6 × 106)
So: X = 206 = CEH
(2) 10ms
Method 0: 10 × 10-3 = (213-X) × 12 / (6 × 106)
So: X = 3192 = 110001111000B
T0 low 5 digits 11000B = 18H
The upper 8 bits of T0: 01100011B = 63H
Method 1: 10 × 10-3 = (216-X) × 12 / (6 × 106)
So: X = 60536 = EC78H
Method 2: In this case, the longest timing is 512μs, and the timing cannot be achieved for 10ms at a time, and the 0.1ms timing cycle can be used 100 times
11. How do I initialize the timer / counter as an external interrupt source? Take T0 as an example to explain through the program.
* Answer: * Initialization procedure:
```
MOV TMOD, # 06H
MOV TL0, #0FFH
MOV TH0, #0FEH
SETB TR0
SETB EA
SETB ET0
```
14. It is known that the MCU clock oscillation frequency is 6MHz, and the T0 timer is used to output a continuous square wave on the P1.1 pin. The waveform is shown in Figure 9-23.
Figure 9-23 Question 15 waveform
**** Solution: **** First calculate the timing constant:
100us 方式0 Tc=FE0EH; 方式1 Tc=FFCEH; 方式2 Tc=CEH
150us 方式0 Tc=FD15H; 方式1 Tc=FFB5H; 方式2 Tc=B5H
**** Method one ****: Use method one, timer interrupt.
flow chart:
ORG 0000H
LJMP START
ORG 000BH
LJMP TINT0
ORG 0100H
START: MOV TMOD, #01H
MOV TL0, #0CEH
MOV TH0, #0FFH
SETB TR0
SETB EA
SETB ET0
SETB 20H.0
SETB P1.1
SJMP $
TINT0: JNB 20H.0, NEXT
MOV TL0, #0B5H
MOV TH0, #0FFH
CLR P1.1
CPL 20H.0
SJMP LAST
NEXT: MOV TL0, #0CEH
MOV TH0, #0FFH
SETB P1.1
CPL 20H.0
LAST:RETI
**** Method 2 ****: Use method 2 timer interrupt plus delay program
flow chart:
ORG 0000H
LJMP START
ORG 000BH
LJMP TINT0
ORG 0100H
START: MOV TMOD, #02H
MOV TL0, #0CEH
MOV TH0, #0CEH
SETB TR0
SETB EA
SETB ET0
SETB 20H.0
SETB P1.1
SJMP $
TINT0: JNB 20H.0, NEXT
CLR TR0
CLR P1.1
LCALL DELAY
CPL 20H.0
SETB TR0
SJMP LAST
NEXT: SETB P1.1
CPL 20H.0
LAST: RETI
DELAY: MOV R7, #8
DELAY1: DJNZ R7, DELAY1
RET