51 single chip microcomputer-the method of timing duration

Publisher:雅致人生Latest update time:2021-10-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Determination of “water volume”

We already know how to choose a "test tube", but how to fill it with "water" to determine the duration of the timing. First, let's take the "big test tube" as an example. Timer 0 has two registers, TH0 and TL0. Let's think of TH0 and TL0 as two 8-bit variables. Because we are using a "big test tube", these two 8-bit variables are equivalent to a 16-bit variable, TH0 is the high 8 bits (H: High), and TL0 is the low 8 bits (L: Low). If one more scale time passes, it will overflow.


Then at this time we will have "TH0 equals 255" and "TL0 equals 255", because the binary 11111111111111111 equals 65535 in decimal. 65535 plus 1 will overflow.


But we don't know how long it takes to increase one scale of water.


Here I will tell you the answer directly. Because we are using a 11.0592M crystal oscillator, the time it takes to increase the water level by one scale is (12/11059200) seconds.


If I want to explain all the principles of the above "(12/11059200)" knowledge point to everyone, it will take a lot of words. I suggest that you look for some related knowledge materials and video explanations such as machine cycles, clock cycles, etc. to have a deeper understanding of timers, or refer to Section 5.2 of the document "Hand-in-hand teaching you to learn 51 single-chip microcomputers".


However, we try to do more questions and gradually understand the results first. After you learn more related knowledge, you will naturally understand the reasons. This is also a teaching feature of this tutorial, which encourages everyone to find more related knowledge for mixed learning.

 

2. Timing milliseconds

If we use the "big test tube" to trigger an interrupt after 20 milliseconds, how can we achieve it?


First, we fill these two registers with values ​​(fill them with a certain amount of water)

8.2.png

It can be seen that the two "8-bit variables" are combined from high to low to become a 16-bit variable. As for why the 20ms timing is filled in this way, we will first use the reverse method to demonstrate it to you.


The hexadecimal value of 0XB800 converted to decimal is 47104, so the timing duration is (65536-47104) = 18432 scale unit time.

18432*(12/11059200) = 0.02 seconds = 20 milliseconds.

Let's go back to the previous example. How do we assign initial values ​​to TH0 and TL0 if we want a timing of 50 milliseconds?

Assume there are x scales left before overflow, x*(12/11059200)=0.05

The solution is x=46080.

Therefore, you need to fill the "large test tube" with 65536-46080=19456 scales of water in advance.

That is, the value of the "16-bit variable" filled in TH0 and TL0 is 19456.

19456 is converted to hexadecimal as 0x4C00.

So if you want a timing of 50 milliseconds, then "TH0=0x4C;", "TL0=0x00;".

 

3. Brief summary

We conclude that if the timing time is set to x seconds, then the initial value of the "16-bit variable" filled in the synthesis of TH0 and TL0 is 65536-(x/(12/11059200)).


Because (12/11059200) represents the time of one scale. If we want to time it to 0.05 seconds, then (0.05/(12/11059200)) represents the number of scales.


And 65536-(0.05/(12/11059200)) is the scale of water that we need to fill in advance.


If after reading this, the reader still cannot understand the principle of the timer after these three lectures, just put aside the theoretical understanding first, and use the following questions to vaguely understand the functions of this hardware module. Later, you can learn more about other people's comprehensive analysis of this knowledge point, so that you can thoroughly understand this knowledge.


I would like to express my deep emotion here: how important is the role of mathematics in our lives!

Reference address:51 single chip microcomputer-the method of timing duration

Previous article:51 MCU-Timer working mode
Next article:51 single chip microcomputer-simple application of timer

Latest Microcontroller Articles
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号