Study Notes - 51 MCU Timing Counter

Publisher:码梦创想Latest update time:2022-05-17 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Timing of MCU

When the microcontroller is working, it is controlled by a unified clock pulse. Due to the different number of bytes of instructions, the time required to fetch these instructions is also different. Even if the number of bytes of instructions is the same, the execution time of different instructions may not be the same due to the large difference in execution operations, that is, the number of beats required is different.


Clock cycle: The clock cycle is also called the oscillation cycle, which is defined as the reciprocal of the clock pulse (it can be understood that the clock cycle is the reciprocal of the crystal oscillator connected to the microcontroller. For example, the time cycle of a 12M crystal oscillator is 1/12 us). It is the most basic and smallest time unit in the computer. In one clock cycle, the CPU only completes one basic action. In the 8051 microcontroller, one clock cycle is defined as one beat (represented by P), and two beats are defined as one state cycle (represented by S).


Machine cycle: In computers, for ease of management, the execution process of an instruction is often divided into several stages, each of which completes a task. A machine cycle of the 8051 series microcontroller consists of 6 S cycles (state cycles). As mentioned earlier, a clock cycle is defined as a beat (represented by P), and two beats are defined as a state cycle (represented by S). The machine cycle of the 8051 microcontroller consists of 6 state cycles, that is, one machine cycle = 6 state cycles = 12 clock cycles.


Instruction cycle: The instruction cycle is the time required to execute an instruction, which is generally composed of several machine cycles. Different instructions require different numbers of machine cycles. The relationship between the clock cycle, machine cycle, and instruction cycle is shown below.


Timing relationship diagram.png

2. Concept of timer/counter of 51 MCU

In a single-chip microcomputer, the relationship between pulse counting and time is very close. Every time a pulse is input, the value of the counter will automatically accumulate by 1. As long as the time interval between two adjacent counting pulses is equal, the count value represents the passage of time. Therefore, the timer and counter in the single-chip microcomputer are actually the same physical electronic component, but the counter records what happens outside the single-chip microcomputer (it receives external pulses), while the timer is a very stable counter provided by the single-chip microcomputer itself. This stable counter is the crystal oscillator component connected to the single-chip microcomputer; the crystal oscillator of the MCS-51 single-chip microcomputer is divided by 12 and provides stable pulses to the single-chip microcomputer; the frequency of the crystal oscillator is very accurate, so the time interval between the counting pulses of the single-chip microcomputer is also very accurate.


3. Working principle of timer/counter of 51 single chip microcomputer

The counting pulses input to the add-1 counter have two sources: one is the output pulse of the system clock oscillator divided by 12; the other is the external pulse source input from the T0 or T1 pin.


When used as a timer, the timer counts the number of pulses after the 12-division of the 8051 microcontroller's internal oscillator output, that is, each machine cycle automatically adds 1 to the register value of the timer T0/T1 until it overflows, and then continues to count from 0 after the overflow; therefore, the resolution of the timer is 1/12 of the clock oscillation frequency;


When used as a counter, the external pulse signal is counted through pin T0 (P3.4) or T1 (P3.5). When the input external pulse signal changes from 1 to 0, the counter value automatically increases by 1. Since it takes 2 machine cycles to detect a falling edge from 1 to 0, the sampled level must be maintained for at least one machine cycle. When the crystal frequency is 12MHz, the maximum counting frequency does not exceed 1/2MHz, that is, the period of the counting pulse must be greater than 2 microseconds. The maximum frequency of the counter is generally 1/24 of the clock oscillation frequency.

From this, we can see that no matter whether it is the timer or counter working mode, timer T0 and T1 do not occupy CPU time, unless timer/counter T0 and T1 overflow, it may cause CPU interruption and execute interrupt processing program instead. Therefore, timer/counter is a highly efficient and flexible component in the microcontroller.


51 MCU timer working principle diagram:

Write the picture description here

From the above figure, we can see that the registers related to the timer are mainly the following: TMOD, TCON, TL0, TH0, TL1, TH1. The following is an introduction to these registers.


16-bit up counter:

Write the picture description here

It is the core of the timer counter. TL0 and TH0 are the bottom eight bits and the upper eight bits of timer counter 0; TL1 and TH1 are the bottom eight bits and the upper eight bits of timer counter 1; and the upper eight bits and the bottom eight bits can be used separately. The 16-bit adder counter is mainly used when setting the initial value of the timer counter.


TMOD timer working mode and mode register

TMOD

GATE: Timing operation switch control bit. When GATE=1, the INT0 or INT1 pin is at a high level, and the TR0 or TR1 control bit in TCON is 1, the timer/counter 0 or 1 starts to work. If GATE=0, as long as the TR0 or TR1 control bit is set to 1, the timer/counter 0 or 1 starts to work.

C/T: Selection bit for timer or counter function. When C/T=1, it is a counter, and the counting pulse is input through the external pin T0 or T1. When C/T=0, it is a timer, and the timing pulse is provided by the internal system clock.

M1 M0: T0, T1 working mode selection bit

  m0m1 

TCON Timer Control Register

Write the picture description here

TF1: Timer T1 overflow flag, which can be queried and cleared by the program. TF1 is also an interrupt request source and is cleared by hardware when the CPU responds to the T1 interrupt.

TF0: Timer T0 overflow flag, which can be queried and cleared by the program. TF0 is also an interrupt request source and is cleared by hardware when the CPU responds to the T0 interrupt.

TR1: T1 count enable control bit, when it is 1, T1 count is enabled (timing).

TR0: T0 count enable control bit, when it is 1, T0 count is enabled (timing).

IE1: External interrupt 1 request source (INT1, P3.3) flag. IE1 = 1, external interrupt 1 is requesting an interrupt from the CPU, and is cleared to "0" by hardware when the CPU responds to the interrupt.

IT1: External interrupt source 1 trigger mode control bit. If this bit is 1, it is set to bottom level trigger, and if it is 0, it is set to falling edge trigger.

IE0: External interrupt 0 request source (INT0, P3.2) flag. IE0 = 1, external interrupt 1 is requesting an interrupt from the CPU, and is cleared to "0" by hardware when the CPU responds to the interrupt.

IT0: External interrupt source 0 trigger mode control bit. If this bit is 1, it is set to bottom level trigger, and if it is 0, it is set to falling edge trigger.

51 MCU timer has 4 working modes:


Working mode 0:

The lower 5 bits of TL0 and all 8 bits of TH0 together form a 13-bit timer/counter. After the timer/counter is started, the number of timing or counting pulses is added to TL0, starting from the preset initial value (time constant), and continuously increasing by 1. When TL0 is full, it is carried to TH0 until the 13-bit register is full and overflows. When TH0 overflows, the TF0 flag in TCON is set and an interrupt request is sent to the CPU. The timer/counter hardware will automatically clear the 13-bit register value to 0. If further timing/counting is required, the relevant instructions need to be used to reset the time constant and set the interrupt flag TF0 of the timer/counter to 0.


Working mode 1: the most commonly used timer working mode

Mode 1 is almost identical to Mode 0, the only difference is that registers TH0 and TL0 in Mode 1 together form a 16-bit timer/counter to participate in the operation, so the timing/counting range is larger than that in Mode 0.

10

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


This mode is also called the automatic reload preset number mode. Sometimes, our timing/counting operation needs to be repeated for many times. If no processing is done when the overflow occurs, then the timing/counting will start from 0 in the second round of timing/counting, which is not what we want. Therefore, to ensure that the timing/counting operation is restarted after each overflow, we need to reload the preset number (time constant) into a certain place. The operation of reloading the preset number is automatically completed by the hardware device and does not require manual intervention. Therefore, this working mode is called the automatic reload preset number mode. In working mode 2, the automatically reloaded preset number is stored in the upper 8 bits of the timer/counter register, that is, stored in TH0, leaving only TL0 to participate in the timing/counting operation.


This working mode is often used in baud rate generator (serial communication), T1 works in serial mode 2; when used in this way, the timer is to provide a time base; after the count overflows, there is no need to do too much, just one thing is to reload the preset number and start counting again, and no delay is required in between.

2

Working mode 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. Since timer/counter T1 does not have working mode 3, if timer/counter T0 is set to working mode 3, TL0 and TH0 will be divided into two independent 8-bit timer/counters.

Write the picture description here

3. Application of timer/counter of 51 single chip microcomputer

Build a simulation environment under protues:

  Timer test schematic.png

Here is an introduction to the setting of the initial value of the timer:

  

Working mode 0: 13-bit timer/counter working mode, can count up to 2 to the 13th power, that is: 8192 times

Working mode 1: 16-bit timer/counter working mode, can count up to 2 to the 16th power, that is: 65536 times

[1] [2]
Reference address:Study Notes - 51 MCU Timing Counter

Previous article:Study Notes - 51 MCU Special Function Registers
Next article:Study Notes - 51 MCU Interrupt

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号