51 MCU Learning——8.2--Timer Counter

Publisher:RadiantExplorerLatest update time:2021-07-23 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Timing counter principle


CPU Timing

Oscillation period: The period of the oscillation source that provides the timing signal for the microcontroller (crystal oscillator period or external oscillation period)

State cycle: 2 oscillation cycles are 1 state cycle, represented by S. The oscillation cycle is also called the S cycle or clock cycle.

Machine cycle: 1 machine cycle contains 6 state cycles and 12 oscillation cycles.

Instruction cycle: The total time taken to complete one instruction, which is measured in machine cycles.


For example: When the external crystal oscillator is 12MHz, the specific value of the relevant cycle of the 51 microcontroller is:


Oscillation period = 1/12us;

Status cycle = 1/6us;

Machine cycle = 1us;

Instruction cycle = 1~4us;

Timing Counter Introduction

Basic Features

The 51 single-chip microcomputer has two sets of timers/counters, because they can be used for timing,

It can also count, so it is called a timer/counter.


The timer/counter and the microcontroller's CPU are independent of each other. The timer/counter works automatically without the CPU's involvement.


The timer/counter in the 51 single-chip microcomputer adds 1 to the data in the register based on the internal clock of the machine or the external pulse signal.


With the timer/counter, the efficiency of the microcontroller can be increased. Some simple repeated additions can be handled by the timer/counter. The CPU can handle more complex tasks. At the same time, precise timing can be achieved.


working principle

The timer/counter is essentially an add-1 counter. It increments by 1 as the input pulse of the counter, that is, the counter automatically increments by 1 for each pulse. When the counter is full of 1, another pulse will return the counter to zero, and the overflow of the counter will set the corresponding interrupt flag to 1, sending an interrupt request to the CPU (when the timer/counter interrupt is allowed). If the timer/counter works in the timing mode, it means that the timing time has expired; if it works in the counting mode, it means that the count value is full.


It can be seen that the count value of the add-1 counter is obtained by subtracting the initial count value from the value of the counter when it overflows.


51 single chip microcomputer timing counter structure

The essence of the timer/counter is a 16-bit add-on counter, which consists of two registers, THx and TLx, with the upper 8 bits and the lower 8 bits, respectively. TMOD is the working mode register of the timer/counter, which determines the working mode and function; TCON is the control register, which controls the start and stop of T0 and T1 and sets the overflow flag.

insert image description here

Timing counter control

The operation of the 51 MCU timer/counter is controlled by two special function registers: TMOD is used to set its working mode; TCON is used to control its startup and interrupt request.


Working mode register TMOD

The working mode register TMOD is used to set the working mode of the timer/counter. The lower four bits are used for T0 and the upper four bits are used for T1. Its format is as follows:

insert image description here

GATE is the gate bit. When GATE=0, it is used to control whether the start of the timer is affected by the external interrupt source signal. As long as TR0 or TR1 in TCON is 1 by software, the timer/counter can be started; when GATA=1, TR0 or TR1 must be 1 by software, and the external interrupt pin INT0/1 must also be high to start the timer/counter. That is, the start condition of the timer at this time adds the condition that the INT0/1 pin is high.


C/T: Timing/counting mode selection bit. C/T = 0 is timing mode; C/T = 1 is counting mode.

M1M0: Working mode setting bit. The timer/counter has four working modes.

insert image description here

Control register TCON

The lower 4 bits of TCON are used to control external interrupts, which have been introduced earlier.

Set the start and interrupt request of the timer/counter. The format is as follows:

insert image description here

TF1 (TCON.7): T1 overflow interrupt request flag. When T1 counts overflow, TF1 is automatically set to 1 by hardware. After the CPU responds to the interrupt, TF1 is automatically cleared to 0 by hardware. When T1 is working, the CPU can query the status of TF1 at any time. Therefore, TF1 can be used as a flag for query testing. TF1 can also be set to 1 or cleared to 0 by software, which has the same effect as setting 1 or clearing 0 by hardware.


TR1 (TCON.6): T1 operation control bit. When TR1 is set to 1, T1 starts to work; when TR1 is set to 0, T1 stops working. TR1 is set to 1 or cleared to 0 by software. Therefore, the start and stop of the timer/counter can be controlled by software.


TF0 (TCON.5): T0 overflow interrupt request flag, its function is similar to TF1.


TR0 (TCON.4): T0 operation control bit, its function is similar to TR1.


How the timer counter works

Method 0

Mode 0 is a 13-bit count, consisting of the lower 5 bits of TL0 (the upper 3 bits are unused) and the 8 bits of TH0. When the lower 5 bits of TL0 overflow, they carry to TH0. When TH0 overflows, the TF0 flag in TCON is set and an interrupt request is sent to the CPU.

insert image description here

The calculation formula for the initial count value is: X = 213-N. N is the number of counts.

The initial value of the timer can also be obtained by directly complementing the number of counts.

In counting mode, the counting pulse is an external pulse on the T0 pin.


Method 1

The number of count bits in mode 1 is 16 bits, with TL0 as the lower 8 bits and TH0 as the lower 8 bits.

As the high 8 bits, a 16-bit plus 1 counter is formed.

insert image description here

The relationship between the number of counts and the initial value of the count is: X = 216-N, N is the number of counts


Method 2

Mode 2 is an 8-bit counting mode with automatic reload of initial value.

insert image description here

The relationship between the number of counts and the initial value of the count is: X = 28 - NN is the number of counts

Working mode 2 is particularly suitable for use as a more accurate pulse signal generator.


Method 3

Mode 3 is only applicable to timer/counter T0. When timer T1 is in mode 3, it is equivalent to TR1=0 and counting stops.

insert image description here

Working mode 3 divides T0 into two independent 8-bit counters TL0 and TH0


Timing counter usage

The initialization procedure should complete the following tasks:


Assign values ​​to TMOD to determine how T0 and T1 work.

Calculate the initial value and write it into TH0, TL0 or TH1, TL1.

How to calculate the initial value?

A machine cycle is the time it takes for the CPU to complete a basic operation.

Machine cycle = 1/microcontroller clock frequency.


The internal clock frequency of the 51 microcontroller is 12 times the external clock frequency. That is to say, when the frequency of the external crystal oscillator is input into the microcontroller, it must be divided by 12. For example, if you use a 12MHZ crystal oscillator, the internal clock frequency of the microcontroller is 12/12MHZ. When you use a 12MHZ external crystal oscillator, the machine cycle = 1/1M = 1us.


What is the initial value of our timing 1ms? 1ms/1us=1000. That is, to count 1000 numbers, the initial value = 65535-1000+1 (because in fact the counter overflows when it counts to 66636) = 64536 = FC18H

In interrupt mode, assign values ​​to EA and enable timer interrupt.

Set TR0 or TR1 to start the timer/counter timing or counting.


Interrupt service function

Because the timer counter overflow time interval is relatively short, we use the accumulated variable to count or time a relatively long time here, but it should be noted that the statements and functions here should be as few as possible to reduce errors. If it is not automatically reloaded, the timer needs to be re-initialized.


Timer Counter Programming

D1 small light flashes every 1s


#include


typedef unsigned char u8;

typedef unsigned int u16;


sbit led=P2^0;


static i=0; //i is a global variable


void time0()

{

   EA=1; //Turn on the interrupt master switch

   ET0=1; //Turn on the timer counter 0 switch

   TR0=1; //Turn on the timer/counter to start working

   TMOD|=0x01; //Set the timer/counter working mode GATE=0 C/T=0 Select working mode 01 16-bit timer/counter

   TL0=0x18; //Set the initial value. We have a 1ms timing and need to count 1000 times, so the initial value is 65536-1000=55536, which is converted to hexadecimal, that is FC18

   TH0=0xFC; //high four bits

}  


void main()

{

   time0();

   while(1)

   {

    if(i==1000)//timing 1s 

    {

    i=0; //initialize i and continue timing

led=~led; //Reverse the LED state to make the LED flash every 1s

      }

   }

}


void timezhongduan0() interrupt 1

{

TH0=0XFC; //Assign initial value to the timer and continue timing for 1ms

TL0=0X18;

i++; //accumulate i to achieve timing of 1s

}

Reference address:51 MCU Learning——8.2--Timer Counter

Previous article:51 MCU Learning——9--Temperature Sensor DS18B20
Next article:51 MCU Learning——8.1 External Interrupt 0 and 1

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号