Basic principles of microcontroller timer/counter

Publisher:RadiantGlowLatest update time:2015-03-23 Source: diangonKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This article mainly studies the basic concepts of timers/counters. First, we start with the concepts of counting and timing in daily life to understand the implementation logic of timer/counter functions. An important knowledge point is overflow. When the timer/counter is full, an "overflow" action will occur, which will generate an interrupt. At the same time, we can further understand the three main methods of implementing timing or counting.
1. Basic concepts of timing/counting

Timing and counting are the most common and widespread problems in daily life and production.

The timer and counter functions are implemented using essentially the same logic, and both functions involve counting input signals, essentially counting pulses. Counters count and indicate the number of input signals (events) in any interval, while timers count the number of input signals in a specified interval to indicate the elapsed time.

In a single-chip microcomputer, when the timer/counter is used for timing, it counts the machine cycle (obtained by dividing the crystal oscillator of the single-chip microcomputer by 12). Since the cycle of each count is fixed, the counting time can be easily calculated based on the number of counts. As shown in Figure 1.

 


Figure 1 Counting and timing
2. Basic Concepts of Overflow

Let's start with a routine in life: a basin is under a faucet, the faucet is not closed tightly, and water drips into the basin. The capacity of the basin is limited, the water drops continue to fall, and the water in the basin continues to fill up. Eventually, a drop of water makes the basin full of water, which is "overflow".

If an empty basin needs 10,000 drops of water to be full, you can put some water in before starting to drip, which is called the initial count value. If you want to count 9,000 now, you can put 1,000 drops of water in first, that is, the initial count value is 1,000, and then count 9,000 to overflow and generate an interrupt.

The method of counting initial values ​​is usually used in single-chip microcomputers. If each pulse is 1 microsecond, it takes 256 microseconds to count 256 pulses. If you want to time 100 microseconds, just put 156 in the counter first, and then count 100 to generate an interrupt when the overflow occurs. As shown in Figure 2.

 

Figure 2 Timer/Counter Overflow

 

3. Main methods of timing/counting

There are three main ways to achieve timing or counting.

(1) Software delay

Software delay is achieved by using a microprocessor to execute a delay program segment. Because it takes a certain amount of time for the microprocessor to execute each instruction, software delay is achieved through the loop of instructions. Software timing has the characteristic of not using hardware, but it takes up a lot of CPU time. In addition, the accuracy of software timing is not high. Under different system clock frequencies, the time to execute an instruction is different, and the timing time of the same software delay program will also be different.

(2) Hardware timing

Hardware timing uses the frequency divider in the digital circuit to appropriately divide the system clock to generate the required timing signal. It can also use a monostable circuit or a simple timing circuit (such as the commonly used 555 timer) to control the timing time by an external RC (resistance, capacitance) circuit. Such a timing circuit is relatively simple, and the timing time can be changed within a certain range by using different frequency division multiples or changing the resistance value and capacitance value.

(3) Programmable hardware timing

The biggest feature of programmable timer/counter is that it can realize the change of timing time through software programming, and complete the timing function or counting function through interruption or query method. This circuit not only can change the timing value and timing range through program, but also has multiple working modes, can output multiple control signals, and has strong functions.

Keywords:MCU Reference address:Basic principles of microcontroller timer/counter

Previous article:Parallel communication and serial communication
Next article:The basic process of single chip microcomputer system interrupt

Recommended ReadingLatest update time:2024-11-16 19:48

Common mistakes made by junior engineers when developing MCU programs
    Here, I use an actual example to correct a small mistake that junior engineers often make, or a detour that they often have to take. I hope this helps everyone. If the writing is not good, please feel free to correct me if there are any unclear parts in the article.     In this article, I am not going to talk ab
[Microcontroller]
AVR MCU on-chip AD digital to analog converter program
/*AD converter program in AVR chip. This test program sets the internal AD converter to continuous conversion mode. For more modes, see pages 191-207 of the manual*/ #include iom16v.h #define uchar unsigned char #define uint unsigned int #define set_bit(a,b) a|=(1 b) #define clr_bit(a,b) a&=~(1 b) #define get_bit(a
[Microcontroller]
What kind of anti-interference technology does MCS51 MCU use?
Since the popular MCS-51 series microcontrollers (such as 8031 ​​and 8032) have no reset instructions and no integrated WATCHDOS circuit, what kind of anti-interference technology does the popular MCS-51 microcontroller use? If there is no external hardware WATCHDOG circuit, software anti-interference technology must b
[Microcontroller]
Implementation of container safety monitoring device and system design based on C8051F410 microcontroller
introduction With the rapid development of the current logistics industry. Container transportation is the main mode of logistics transportation. Its transportation efficiency and freight safety have attracted increasing attention. In recent years, with the development and promotion of RFID technology, RFID technology
[Microcontroller]
Implementation of container safety monitoring device and system design based on C8051F410 microcontroller
Design of radio remote control multi-channel switch system based on single chip control technology
With the rapid development of integrated circuit technology, new remote controls based on various chips are constantly emerging. The central control components of remote control devices have gradually evolved from early discrete components and integrated circuits to current single-chip microcomputers, and their intelli
[Analog Electronics]
Design of radio remote control multi-channel switch system based on single chip control technology
MCU Learning 10: Query Mode Button
1. Experimental Phenomenon      The rightmost digital tube initially displays 0. Each time the external button K1 is pressed, it increments by one and returns to zero when it counts to nine. 2. Purpose of the experiment:    Understand the matters that should be paid attention to when reading external buttons, maste
[Microcontroller]
MCU Learning 10: Query Mode Button
Design of intelligent vehicle control system based on MC9S12DG128 single chip microcomputer
In the smart car competition, the participating teams should make a smart car that can autonomously identify the route based on the car model platform, and then automatically identify the road and drive on the specially designed track. The smart car designed in this paper uses the 16-bit MC9S12DG128 microcontroller
[Microcontroller]
Design of intelligent vehicle control system based on MC9S12DG128 single chip microcomputer
Introduction to RAM testing methods based on several commonly used single-chip microcomputer systems
In various single-chip microcomputer application systems, the normality of the memory is directly related to the normal operation of the system. In order to improve the reliability of the system, it is necessary to test the reliability of the system . Through testing, the damage caused by memory failure to the system
[Microcontroller]
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号