[MCU Learning] 51 MCU [Timer/Counter], detailed introduction

Publisher:中原读书客Latest update time:2022-07-28 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Know before you know

1. Knowledge about 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 = 6 state cycles = 12 oscillation cycles.

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


1.1. Cycle Conversion

2. What you need to know before learning the timer

(1) The 51 MCU has two sets of timers/counters;


Because it can both time and count, it is called a timer/counter.


(2) The timer/counter and the microcontroller's CPU are independent of each other.


The timer/counter operation process is completed automatically and does not require the participation of the CPU.


(3) The timer/counter in the 51 single-chip microcomputer adds 1 to the data in the register according to the internal clock of the machine or the external pulse signal;


(4) With a timer/counter, the efficiency of the microcontroller can be increased.


Some simple repeated additions can be handled by the timer/counter, and the CPU can handle more complex tasks. At the same time, precise timing can be achieved.


3. Working principle of timer/counter

4.51 MCU timer structure

The essence of the timer/counter is a 16-bit add-on counter, which consists of two registers, THx and TLx, with high and low 8 bits. 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.

2. 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.


1. 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:

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 GATE=1, you need to use software to make TR0 or TR1 in TCON 1, and the external interrupt pin INTO/1 is also at a high level, then the timer/counter can be started. [Compared with the above method, this method has one more external high level (so the above method is often used)]


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.

2. Control register TCON

The lower 4 bits of TCON are used to control external interrupts, which have been introduced in the [Interrupts] section. The upper 4 bits of TCON are used to control the start of the timer/counter and the interrupt request.


Its format is as follows:

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.


3. Working mode of timer/counter

Mode 0: 13-bit counter (TR0 control type)

When GATE=0 passes through the inverter, it is 1, and it is still 1 after passing through the OR gate (don't worry about the INT0 pin at this time, because it is an OR gate), and then passes through the AND gate (the value at this time is controlled by TR0). When TR0=1, it is turned on and starts counting, otherwise it is not turned on. [Controlled by TR0]


When GATE=1 passes through the inverter and is 0, we still need to look at the INT0 pin. If it is 1, it will pass through the OR gate to be 1 (otherwise it will be 0 and the timer will not start), and then pass through the AND gate (the result is controlled by TR0 at this time). When TR0=1, it is turned on and starts counting. [Controlled by TR0]

Mode 1: 16-bit plus 1 counter

Method 2: 8-bit automatic reload (reload initial value)

Mode 3: Only applicable to timer/counter T0

Usually use method 1 and method 2


What to do when using a timer

The initialization program should complete the following tasks: [Writing the program is also done in the following steps]


Assign values ​​to TMOD to determine the working mode of T0 and T1;

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

Turn on timer 0 interrupt switch ET0;

Interrupt mode, assign a value to EA (general interrupt switch) to turn on the timer interrupt;

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

Reference address:[MCU Learning] 51 MCU [Timer/Counter], detailed introduction

Previous article:51 MCU timer and counter
Next article:[MCU Learning] 51 MCU [Serial Port], detailed introduction

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号