The timer/counter of the 51 microcontroller is a programmable component. Its working mode, initial count value and start-stop operation should be initialized before the timer/counter works, that is, write the corresponding control word to the corresponding register.

There are two timers T0 and T1 inside the standard 51 single-chip microcomputer. The special function registers related to these two timers are mainly composed of timing value storage register, timer control register and timer mode register.

When using a timer, you need to complete the timer configuration. The configuration steps are as follows:

First: Set the special function register TMOD and configure the working mode.

Second: Set the initial value of count registers TH0 and TL0.

Third: Set TCON and let the timer start counting by setting TRX to 1.

Fourth: Determine the TFX bit of the TCON register and monitor the timer overflow

Here X in TRX and TFX refers to 0 or 1.

The following program uses timer 0, working mode 1, realizes the timing function of 20ms, and controls the P1.0 of the microcontroller to output a square wave signal with a period of 40ms.

1. First set the special function register TMOD, and configure timer 0 to work in mode 1. The bits of the TMOD register are shown in the following table. According to the requirements, timer 0 is used, and the upper four bits are the control bits of timer 1. Therefore, the upper four bits of TMOD are 0, and the lower four bits of TMOD are the control bits of timing 0. According to the requirements, timer 0 works in timer mode, so the C/T (T0) bit should be set to 0, indicating that it is working in timer mode. The start of this timer is not controlled by the external signal of the MCU pin INT0, so GATA (T0) can be cleared. If timer 0 is required to work in mode 1, the M1 (T0) position is 0, and the M0 (T0) position is 1. Therefore, through the above analysis, it can be known that the bits of TMOD are as follows: TMOD=0B00000001, that is, TMOD=0X01.

2. Set the initial values ​​of the counting registers TH0 and TL0, and determine the initial values ​​of TH0 and TL0 according to the timing time of 20ms. The crystal oscillator of the single-chip microcomputer is 11.0592M, the clock cycle is 1/11059200, the machine cycle is 12/11059200 seconds, and the fixed time is 20ms, which means that it takes X machine cycles to get 0.02 seconds, that is, X* 12/11059200=0.02, and X=18432.16 The overflow value of the bit timer is 65536 (because 65535 plus 1 is the overflow), so first give TH0 and TL0 an initial value, let them just reach 65536 overflow after 18432 machine cycles, after the overflow, TF0 is set to 1, and TF0 is passed. It can be known that 0.02 seconds is up. Then the initial value y=65536-18432=47104, converted to hexadecimal is 0XB800; that is, TH0=0XB8; TL0=0X00;

3. Set TCON and start the timer by setting TR0 to 1, that is, start the timer by TR0=1.
 

4. Determine the TF0 bit of the TCON register and monitor the timer overflow. If TF0 is 1, the timer overflows. When the timer expires, you can do the corresponding processing, such as changing the level state of the P10 pin to achieve a 40ms square wave signal.

The process of initializing and setting the above timer is encapsulated into a function as follows:

void TImer0_init()

{

TMOD=0X01;

TH0=0XB8;

TL0=0X00;

     TR0=1;
 

}

Then call the TImer0_init() function in the main function to initialize the timer. And in the main function to query whether the timer overflows, the overflow indicates that the time is up, and the response is processed. The main function is as follows

void main() //main function
{
TImer0_init();//Call the timer initialization function
    while(1)
     {
if(1==TF0)//Determine whether the timer overflows
         {
          TH0=0XB8;//
TL0=0X00;//For timer 0 to work after 1 needs to overflow, reload the initial value
P10=~P10;//P10 level state inversion, to achieve high and low level switching
         }
    
     }
 }
 

The simulation results are shown in the figure, one grid is 10ms, and one square wave period is 4 grids, which is exactly 40ms.

This is the simplest example of a timer. Through this example, you can master the use of the timer, and with a little modification, you can achieve some functions, such as: stopwatch used in events, intelligent timing power-off socket, through the introduction of the microcontroller port The “trigger pulse signal” generated by the external event input on the pin is counted, so as to realize the corresponding function control according to the counting result, such as frequency counter.


Reviewing Editor: Liu Qing

Leave a Reply

Your email address will not be published.