Ordinary MCU Teaching Lesson 15 Counter and Timer

Publisher:HeavenlyWhisperLatest update time:2016-03-25 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Introduction of the concept of counting

Let's start with counting votes: draw a "positive". This is counting. Examples of counting can be found everywhere in life. For example: the counter on a tape recorder, the electric meter used at home, the odometer on a car, etc. Let's take another example from industrial production. The cable industry needs to count meters after the wires are produced, that is, measure the length. How to measure it? Measure with a ruler? It's unrealistic. Not to mention that it's too long, you have to measure while making it. What should you do? There is a very clever method in the industry. Use a wheel with a circumference of 1 meter, wrap the cable around it for one week, and let the wire belt rotate. In this way, one rotation of the wheel is 1 meter long, so as long as you record how many times the wheel rotates, you can know how long the wire has traveled.

 

2. Capacity of the counter

Let's start with an example from life: a basin is under a faucet, the faucet is not closed tightly, and water drips into the basin. The water drops continuously, and the capacity of the basin is limited. After a period of time, the water will gradually become full. The counter on the tape recorder can only count up to 999... So how much capacity does the counter in the microcontroller have? There are two counters in the 8031 ​​microcontroller, called T0 and T1. These two counters are composed of two 8-bit RAM units, that is, each counter is a 16-bit counter, and the maximum count is 65536.

 

3. Timing

The counter in 8031 ​​can be used not only for counting but also as a clock. The clock has many uses, such as ringing bells, timed shutdown of televisions, timed switches for air conditioners, etc. So how is the counter used as a timer?

 

I set an alarm clock to go off in 1 hour. In other words, the second hand has moved (3600) times, so the time is converted into the number of times the second hand has moved, which is the number of times counted. It can be seen that the number of times counted and time are indeed very related. So what is the relationship between them? That is, the time for each movement of the second hand is exactly 1 second.

 

 

figure 1

Conclusion: As long as the intervals between the counting pulses are equal, the count value represents the passage of time.

Therefore, the timer and counter in the microcontroller are the same thing, except that the counter records what happens in the outside world, while the timer is a very stable counting source provided by the microcontroller.

 

So what is the counting source for the group timer? As shown in Figure 1, it is a pulse source obtained by dividing the crystal oscillator of the microcontroller by 12. The frequency of the crystal oscillator is of course very accurate, so the time interval of the counting pulse is also very accurate. Question: What is the pulse time interval provided to the counter by a 12M crystal oscillator? Of course, this is easy, that is, 12M/12 equals 1M, which is 1 microsecond.

 

Conclusion: The interval between counting pulses is related to the crystal oscillator. For a 12M crystal oscillator, the interval between counting pulses is 1 microsecond.

 

4. Overflow

Let's look at the example of water drops again. As the water keeps falling, the water in the basin keeps filling up. Eventually, one drop of water fills up the basin. If another drop of water falls, what will happen? The water will overflow. In technical terms, this is called "overflow."

 

When water overflows, it flows to the ground. When the counter overflows, TF0 will become "1". We will talk about what TF0 is later. Once TF0 changes from 0 to 1, a change occurs, and a change will trigger an event, just like when the timer arrives, the alarm will ring. As for what event will be triggered, we will introduce it in the next class. Now let's study another question: how many counting pulses are needed to make TF0 change from 0 to 1.

 

5. Arbitrary Timing and Counting Methods

As we have just studied, the capacity of the counter is 16 bits, that is, the maximum count value is 65536, so the count will overflow when it reaches 65536. This is not a problem, but the problem is that in real life, there are often requirements for less than 65536 count values. For example, on the packaging line, a dozen is 12 bottles, and a bottle of tablets is 100 tablets. How to meet this requirement?

 

 

Tip: If it is an empty basin and it takes 10,000 drops of water to fill it up, and I put a spoonful of water in before I start dripping, do I still need 10,000 drops?

 

By the way, we use the preset number method. If I want to count 100, I will put in 65436 first, and then 100 pulses, then it will reach 65536, right?

 

The same is true for timing. Each pulse is 1 microsecond, so it takes 65.536 milliseconds to count 65536 pulses. But now I only need 10 milliseconds. What should I do?

 

 

10 milliseconds is 10,000 microseconds, so just put 55536 in the counter.

Reference address:Ordinary MCU Teaching Lesson 15 Counter and Timer

Previous article:Ordinary MCU Teaching Lecture 9 MCU Instructions (Part 3)
Next article:Ordinary MCU Teaching Lesson 16: Timer/Counter Mode Control Word

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号