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 bell ringer, TV timer shutdown, air conditioner timer switch, 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.