What are the essential differences and application methods of 51 MCU counters and timers?

Last Update Time: 2019-07-17 10:37:28


In the learning process of 51 single-chip microcomputer, we often find that the interrupt, counter/timer and serial port are the difficulties in learning the single-chip microcomputer. For beginners, the contents of these parts are difficult to understand. However, these parts are the key points of MCU learning. If you don't understand these parts, it means that you haven't mastered 51 MCUs yet, let alone the development of MCUs. We all know that there are many MCU projects in the finished products. Based on these parts, the perpetual calendar is based on timers, the alarms are mainly interrupts, and the online communication is based on serial ports.


 

 image.png

In these parts, counters/timers are easy to confuse for beginners. Here are a few points to discuss in this regard.

 

The essence of the counter and the timer are the same. They all count the pulses generated in the MCU, except that the counter is the externally triggered pulse of the MCU. The timer is the pulse generated by the internal oscillator under the trigger of the crystal oscillator. Counters and timers are a concept when their pulse intervals are the same.

 

There is an overflow concept in both the timer and the counter, so what is overflowing. We can get the answer from a small common sense of life. When a bowl is placed under the tap to pick up the water, after a while, the bowl of water is full and overflows. By the same token, assuming that the water in the faucet is dripping into the bowl, there is always a drop of water that causes the water in the bowl to overflow. The water that overflows in the bowl is wasted, but overflowing in the timing counter of the microcontroller will result in an interruption.

 

In the timer counter, there is a concept called capacity, which is the maximum count.

Mode 0 is the 13 square of 2,

Mode 1 is the 16 square of 2,

Mode 2 is the 8 square of 2,

 

The water droplet is likened to a pulse, and the last drop of water that causes the water in the bowl to overflow is the last pulse of the overflow of the timer counter.

In the various MCU , when the timing counter is introduced, an initial value is counted. What is the initial value of the count? Here we still assume a drop bowl. Assuming that the first hundred drops of water can overflow the water in the bowl, we know that the bowl has a capacity of 100.

 

Example 1, how can I make the bowl overflow with 10 drops of water? I can imagine that if you take an empty bowl to pick up the water, you still have to drop 100 drops of water to overflow, but if we take a bowl that already has water, we don't need 100 drops. At this point we can figure out that if 10 drops of water are allowed to overflow the water in the bowl, then 90 drops of water must be placed in the bowl. In the timing counter, these 90 drops are what we call the initial value.

 

Example 2, how do we use a single-chip microcomputer to count pieces of 100 products in a workshop and carry out automatic packaging? We can use the counter to count 100 and perform an automatic wrapper in the interrupt.

There are 3 initial values counted here, assuming:

Mode 0: Count initial value = 8192 (13 squares of 2) - 100 = 8092.

Mode 1: Count initial value = 65536 (16 squares of 2) - 100 = 65436.