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.
Previous article:Ordinary MCU Teaching Lecture 9 MCU Instructions (Part 3)
Next article:Ordinary MCU Teaching Lesson 16: Timer/Counter Mode Control Word
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 【ST NUCLEO-G071RB Review】+ My review process
- Share a learning material: Comic electronic circuit
- Help: Current-type voltage transformer detects AC voltage circuit
- Share two PPT lectures on MOS tubes, very good
- The application circuit of the operational amplifier as a battery pack indicator light is shown in the figure
- Countdown 4 days: NXP i.MX processor quiz prize draw (including facial recognition demo source code)
- Sharing of experience in single board circuit design (1) - power supply filtering
- Reference circuit design
- Exclusive violent disassembly of active noise reduction neck-worn voice smart headphones
- Introduction to TI_DSP link command file (*.cmd)