1 51 single chip microcomputer - timer use


 


4.1 timer

4.1.1 51 clock cycle introduction

Clock cycle: clock cycle T is the smallest time unit in timing. The specific calculation method is 1 / clock source frequency. The crystal oscillator commonly used on 89C51 MCU development board is 11.0592M. For this MCU system, clock cycle = 1 / 11059200 seconds.

Machine cycle: it is the shortest time for the single chip microcomputer to complete an operation.

Machine cycle is mainly for assembly language. In assembly language, the execution time of each statement of the program is an integral multiple of the machine cycle, and the time occupied by the statement can be calculated, while the time of a statement in C language is uncertain and affected by many factors.

51 single chip microcomputer series, in its standard architecture, the next machine cycle is 12 clock cycles, that is, 12 / 11059200 seconds.

At present, many enhanced 51 single chip computers have relatively high speed. Some one machine cycle is equal to four clock cycles, and some one machine cycle is equal to one clock cycle, that is, generally speaking, its speed can reach 3 or 12 times of the standard 51 architecture.

Timer and counter are the same module in MCU. Two different functions can be realized by configuring SFR (special function register). In most cases, timer function is used.

As the name suggests, the timer is used for timing. There is a register inside the timer. After it starts counting, the value of this register will automatically increase by 1 every machine cycle. The machine cycle can be understood as the counting cycle of the timer. Just like a clock, the number automatically increases by 1 every second, and this timer is the time of every machine cycle, that is, 12 / 11059200 seconds, the number automatically increases by 1.

4.1.2 timer function introduction

The standard 51 single chip microcomputer has two timers T0 and T1. T is the abbreviation of Timer.

Timer 0 and timer 1 of STC90C51RC/RD + series single chip microcomputer are fully compatible with the timer of traditional 8051. When timer 1 is used as baud rate generator, timer 0 can be used as two 8-bit timers.

Two 16 bit timers / counters t0 and T1 set inside STC90C51RC/RD + series single chip microcomputer have two working modes: counting mode and timing mode.   For each timer / counter (T0 and T1), there is a control bit - C/T in the special function register TMOD to select whether t0 or T1 is a timer or a counter.

The core component of timer / counter is an addition (also subtraction) counter, whose essence is to count pulses. Only the counting pulse sources are different: if the counting pulse comes from the system clock, it is the timing mode. At this time, the timer / counter obtains a counting pulse every 12 clocks or every 6 clocks, and the count value is added by 1; If the counting pulse comes from the external pin of the single chip microcomputer (T0 is P3.3,T1 is P3.3), it is the counting mode, and 1 is added for each pulse.

Timer / counter 0 has 4 working modes:

Mode 0 (13 bit timer / counter)

Mode 1 (16 bit timer / counter)

Mode 2 (8-bit automatic reload mode)

Mode 3 (two 8-bit timers / counters)

Except for mode 3, other working modes of timer / counter 1 are the same as timer / counter 0. T1 is invalid in mode 3 and stops counting.

Figure 4-1-1 timer register

Timer related registers are introduced in detail on page 137 of the official Manual of STC MCU.

Manual download address:  http://www.stcmcu.com

The above picture shows the registers to be configured when using the timer. TL0, TL1, TH0 and TH1 registers are used to store the reload value of the timer. They are divided into high and low registers.

4.1.3 TCON register

TCON register is used to control the timer, such as:   Timer start bit, timer overflow flag bit, etc.

Figure 4-1-2 TCON register

TCON register supports bit addressing. The function of each bit is also defined in the < reg51. H > header file. You can directly operate bits for assignment.

TF1 and TF0: overflow flag bits of timer 1 and timer 0. When the timer counts overflow (i.e. timer timeout), the flag position is 1.

TR1 and TR0: are the control bits of timer 1 and timer 0. When the value is assigned to 1, start the timer and start counting.

The other bits are the control related to external interrupt. At present, they are not used when learning the timer. They are ignored for the time being.

4.1.4 TMOD register

The TMOD register is used to configure the mode of the timer.

Figure 4-1-3 TMOD register  

 

Figure 4-1-4 register mode selection

4.1.5 calculation method of timer reassembly value

The next machine cycle of 51 single chip microcomputer standard architecture is 12 clock cycles. If the crystal oscillator frequency is 11.059200MHZ, the time of a machine cycle is 12 / 11.059200   Microseconds.

In other words, the time of counter + 1 of timer is 12/11.059200=1.085069us.

If the timer works in 16 bit mode, the maximum value can be stored:   0 ~ 65535, then the maximum timing time can be known: 65535*1.085069=71109.996915us=71.109996915ms   , It's about 71 milliseconds.

If the timing is 1000us, the formula is: x*1.085069=1000, and the value of x is:    1000/1.085069=921 .

It takes + 921 times to calculate the timer and just get 1000us. However, when the single chip microcomputer works in 16 bit mode, it needs to fill up 65535 timer to overflow, so it is necessary to assign an initial value to the timer.

65535-921 = 64614, so the timer can count from 64614. When the count reaches 65535, the timer will overflow and TF0 will be set to 1. At this time, 1000us time has just passed.

Then the reload value register of the timer can be assigned as follows:

u16 t0_data=64614;

TH0=t0_ data>>8;  // high position

TL0=t0_data;     // Low position

4.1.6 configure timer 0 to work in 16 bit timer mode

In the following code, the timer 0 of 51 single chip microcomputer is configured to work in 16 bit timer mode. The program encapsulates the function to calculate the reload value, which is convenient to call. The program does not use interrupt. It uses polling to detect whether the timer times out. A counting variable is used in the main function to record the times of timer timeout, which is convenient to record longer time.

The function in the program is to change the state of LED once when the time reaches 1 second.

(description of hardware platform: CPU is STC90C516RD, crystal oscillator frequency is 12MHZ, working in 12T mode, and one machine cycle is 1us time)

Example code:

#include <reg51.h>
#define   LED   P0   // Define led pin
u16 T0_Update_data;//Initial value of timer 0
void Timer0_16bit_Init(u16 us)
{   
    //At present, the actual frequency of the crystal oscillator on the experimental board is:   11.956MHZ
    u16 val=us/(12/11.956); //Get the count time, as long as the integer part
    T0_Update_data=65535-val; //Get the reload value
    TMOD&=0xF0;     //Clear configuration
    TMOD|=0x01;     //Configure timer 0 to operate in 16 bit timer mode
    TH0=T0_Update_data>>8; //Timer 0 high reassembly value
    TL0=T0_Update_data;    //Timer 0 low reassembly value
    TR0=1;          //Start timer 0
}
//Reload value update function for timer 0
void Timer0_Update(void)
{
    TH0=T0_Update_data>>8; //Timer 0 high reassembly value
    TL0=T0_Update_data;    //Timer 0 low reassembly value
}
int main()
{
    u32 cnt=0;
    u8 i=0;
    Timer0_16bit_Init(1000);  //Configure the timer timeout time to 1000us
    LED=0x00; //Turn off all lights
    while(1)
    {
        if(TF0) //Judge whether the timer 0 timing time has arrived
        {
            cnt++;//Record timeout times
            if(cnt==1000)
            {
                cnt=0;
                LED=~LED; //Reverse LED lamp
            }
            TF0=0; //Clear flag bit
            Timer0_Update(); //Reassign the counter of the timer  
        }
    }
}

4.1.7 configure timer 1 to work in 16 bit timer mode

In the following code, the timer 1 of 51 single chip microcomputer is configured to work in 16 bit timer mode. The program encapsulates the function to calculate the reload value, which is convenient to call. The program does not use interrupt. It uses polling to detect whether the timer times out. A counting variable is used in the main function to record the times of timer timeout, which is convenient to record longer time.

The function in the program is to change the state of LED once when the time reaches 1 second.

(description of hardware platform: CPU is STC90C516RD, crystal oscillator frequency is 12MHZ, working in 12T mode, and one machine cycle is 1us time)

Example code:

#include <reg51.h>
#include "delay.h"
#define   LED   P0   // Define led pin
u16 T1_Update_data;//Initial value of timer 1
void Timer1_16bit_Init(u16 us)
{   
    //At present, the actual frequency of the crystal oscillator on the experimental board is:   11.956MHZ
    u16 val=us/(12/11.956); //Get the count time, as long as the integer part
    T1_Update_data=65535-val; //Get the reload value
    TMOD&=0x0F;            //Clear configuration
    TMOD|=0x10;            //Configure timer 1 to operate in 16 bit timer mode
    TH1=T1_Update_data>>8; //Timer 1 high reassembly value
    TL1=T1_Update_data;    //Timer 1 low reassembly value
    TR1=1;                 //Start timer 1
}

//Reload value update function of timer 1
void Timer1_Update(void)
{
    TH1=T1_Update_data>>8; //Timer 1 high reassembly value
    TL1=T1_Update_data;    //Timer 1 low reassembly value
}
int main()
{
    u32 cnt=0;
    u8 i=0;
    Timer1_16bit_Init(1000);  //Configure the timer timeout time to 1000us
    LED=0x00; //Turn off all lights
    while(1)
    {
        if(TF1) //Judge whether the timer 0 timing time has arrived
        {
            cnt++;//Record timeout times
            if(cnt==1000)
            {
                cnt=0;
                LED=~LED;
            }
            TF1=0; //Clear flag bit
            Timer1_Update(); //Reassign the counter of the timer  
        }
    }
}

4.1.8 configure timer 0 to work in 8-bit automatic reload mode

In the following code, the timer 0 of 51 single chip microcomputer is configured to work in the 8-bit timer automatic reload mode. In the automatic reload mode, after each timer timeout, the process of manually assigning reload value is omitted, which is more convenient. However, the maximum timing time of each timer becomes shorter, and the counter will overflow when it reaches 255.

The program encapsulates the function to calculate the reload value, which is convenient to call. The program does not use interrupt. It uses polling to detect whether the timer times out. A counting variable is used in the main function to record the times of timer timeout, which is convenient to record longer time.

The function of the program is to change the state of the LED once the time reaches 500 milliseconds.

(description of hardware platform: CPU is STC90C516RD, crystal oscillator frequency is 12MHZ, working in 12T mode, and one machine cycle is 1us time)

Example code:  

#include <reg51.h>
/*
Configure timer 0 to operate in 8-bit automatic reload mode
 Note that the time cannot exceed the maximum time of the timer
255*(12/11.059200)=276us
*/
void Timer0_8bit_Init(u16 us)
{   
    //At present, the actual frequency of the crystal oscillator on the experimental board is:   11.956MHZ
    u16 val=us/(12/11.956); //Get the count time, as long as the integer part
    TMOD&=0xF0;     //Clear configuration
    TMOD|=0x02;     //Configure timer 0 to operate in 8-bit auto reload mode
    TL0=TH0=255-val;//Obtain the reload value;
    TR0=1;          //Start timer 0
}
#define   LED   P0   // Define led pin
int main()
{
    u32 cnt=0;
    u8 i=0;
    Timer0_8bit_Init(100);  //Configure the timer timeout time to 100us
    LED=0x00; //Turn off all lights
    while(1)
    {
        if(TF0) //Judge whether the timer 0 timing time has arrived
        {
            cnt++;//Record timeout times
            if(cnt==10*500) //500ms
            {
                cnt=0;
                LED=~LED;
            }
            TF0=0; //Clear flag bit
        }
    }
}

Tags: C Single-Chip Microcomputer stm32

Posted on Wed, 10 Nov 2021 18:34:15 -0500 by mrjonnytou