51 single chip microcomputer programming often requires some operation of time, such as dynamic scanning display of nixie tube, delay, etc. for timing or delay, there are generally three methods:

① . software timing: such as the commonly used delay program delay (). During the timing process, the CPU cycles to test whether the delay variable meets the conditions. At this time, the CPU utilization is low;

② Hardware timing: for example, the external chip 555 time base circuit achieves a certain delay function through the external resistance capacitance. Changing the resistance capacitance can change the delay length. This timer depends on the hardware and cannot be changed by software after setting;

③ Timer: use the internal timer of MCU to realize the operation of time.

1. Timer function:

(1) Timing: generate a standard time interval: such as 20 ms, 100 ms, etc. in this way, the single chip microcomputer can generate a pulse square wave that meets a certain requirement;

(2) Count: count external events (pulses). It is obvious that the occurrence of external events is random. The single chip microcomputer cannot predict when external events will occur, but statistics can be carried out. When the required value is reached, the single chip microcomputer will carry out corresponding operations.

2. Four working modes:

Mode 0: 13 bit counter mode

Add 1 count on the basis of an initial value. When the counter is full of “1” and another counting pulse, a “overflow interrupt signal” TF1 = 1 will be generated.

Note: since the upper three digits of the lower 8-bit counter do not participate in the work, these three digits should be “avoided” when filling the initial value.

Mode 1: 16 bit timing / counting mode

Similar to mode 0, since the two counters are complete 8 bits, the calculated 16 bit initial value can be directly filled into th and TL.

Mode 2: 8-bit automatic reload initial value mode

TL is used as the counter and th is used as the initial value register. Before operation, TL1 and Th1 preset the same initial value respectively. When TL1 counts, the initial value in Th1 is automatically reloaded to TL1, and TL1 continues to count.

Mode 3: combined expansion mode (t0 only)

“Split and combine” t0 and T1 again, and change t0 into two independent 8-bit timing / counters composed of Th0 and tl0.

Supplement: comparison of four modes:

3. Timer parameters:

(1) The number of timers: the 51 single chip microcomputer tutorials seen on the Internet say that there are two timers: timer 0 and timer 1. In fact, some single chips have three timers, such as the single chip microcomputer stc89c54RD + on the development board bought by bloggers. Therefore, pay attention to several timers when programming

(2) , Tl, th: initial value register, which is the lower 8 bits and the higher 8 bits respectively, followed by a number 0 or 1, representing timer 0 or timer 1

(3) Tmod: mode control register, belonging to SFR special function register, address: 89h, determines four working modes of timing / counter, 8 bits

Gate gate: when gate = 0, the counter starts working as long as tr = 1

When gate = 1, only when int = tr = 1, the counter works (used to measure the width of high-level pulse of int pin)

C / T: counter input pulse selection switch, which determines the two working modes of “timing” or “counting” of the counter

C / T = 0: timing mode: the counter receives the internal clock signal FOSC

C / T = 1: counting mode: the counter receives the external signal on the pin

M1 and M0 mode selection: determine four working modes

(4) TCON: control register, belonging to SFR special function register, address: 88H, 8 bits

TF: timer overflow flag. When the counter overflows, the hardware will set automatically, that is, TF = 1. After entering the interrupt, it will be cleared automatically by the hardware (TF = 0)

Tr: control bit of counter

TR = 1: counter starts working

TR = 0: counter stops counting

Ie: request flag of external interrupt int

When the MCU detects that there is a falling edge on the int pin, ie = 1 applies for interrupt. When entering the interrupt service program, the hardware automatically clears the IE flag

It: external interrupt trigger type control

When it = 1, the falling edge of the external interrupt signal triggers the IE flag

When it = 0, the low level of the external interrupt signal triggers the IE flag

(5) . when used as a timer, the relationship between timing and initial value is as follows:

T = ( M – TC ) × t

Where: T: timing time

M: Modulus of counter

TC: initial count value

t: The counter counts the period of the pulse (t = FOSC / 12)

Leave a Reply

Your email address will not be published.