Regardless of the type of microcontroller, timer functionality is essential and relies on timers/counters, which are the most basic configurations of a microcontroller.
Timing is primarily accomplished by hardware, while timing control is mainly handled by software.
There are two methods for timing control: polling and interrupt methods. Each has its advantages and disadvantages, but in most cases, the latter is preferred.
The disadvantage of the polling method is that the timing accuracy is relatively low, due to the following reasons:
1. Uncertainty of software delay
In the polling method, the main program must continuously and actively check whether the timer flag is set, and the execution of the checking code itself consumes execution time (a few instruction cycles).
If the program has multiple tasks, the main program cannot immediately respond to timer overflow while executing other tasks. It will only detect the overflow when the program flow reaches the line of code that checks the timer flag again.This execution time of “other tasks” is uncertain and variable. It could be very short (a few microseconds) or very long (milliseconds, depending on the complexity of the tasks). Any timer overflow that occurs during this time will be detected with a delay.
Even if the timer overflows very precisely at a certain moment (hardware timing is usually high precision), due to the aforementioned software delay, the actual time point at which the program detects the overflow is always after the actual overflow time point. This delay can range from a few instruction cycles to the length of an entire main loop cycle, and it is unpredictable and uncontrollable. This is the main factor that reduces accuracy.
2. Cumulative error
Each detection of timer overflow may have a certain delay error.
If the timing is periodic (for example, resetting the timer after each overflow and starting the next timing), these small delay errors will accumulate, leading to an increasing deviation between the actual time interval and the target interval.
3. Influence of the main loop structure
The timing accuracy completely depends on the execution speed and structure of the main loop. Any changes that affect the execution time of the main loop (such as adding tasks, handling complex calculations, responding to other interrupts) will directly impact timing accuracy.
◆Result of polling method: Actual timing = Precise hardware timing + Variable software detection delay.
◆Applicable scenarios: Situations where timing accuracy is not critical, such as simple state machine polling, non-critical time delays (like button debouncing, where millisecond-level accuracy is sufficient), or as an auxiliary monitoring method.
◆Consider a timing width of several tens of microseconds, with software detection delays of up to several tens of microseconds; this timing error could be significant and may not be applicable in the system.
◆Consider a timing width of several milliseconds, with software detection delays of up to several tens of microseconds; this timing error is generally acceptable.
Thus, the size of the error is highly related to the application scenario. Whether to use the polling method needs to be weighed against various factors.
To improve timing accuracy, the interrupt method is typically used, which has the following characteristics:。
1.Hardware-triggered, immediate response: When the timer overflows, the hardware automatically sets the overflow flag and immediately (within the next instruction cycle or a few cycles) triggers an interrupt (if interrupts are enabled).
2.Minimized delay:The CPU will pause the current task (saving the context), jump to the preset timer interrupt service routine for execution. This response time is very short (determined by hardware, usually fixed at a few instruction cycles) and is predictable and controllable.
3.Independent of the main program: The occurrence of interrupts is not affected by the execution of the main program. As long as the execution time of the interrupt service routine is short and fixed, the timing accuracy will be very high.
4.No cumulative error: Precise interrupt response delays ensure that each timing cycle can start and end at almost precise moments, greatly reducing cumulative errors.
In summary, for applications that require precise timing (PWM generation, communication baud rate control, data acquisition synchronization, real-time control, etc.), it is essential to use the timer’s interrupt functionality. If resources are extremely limited and polling must be used, the following strategies can be employed to reduce delay errors.
1.Try to place the code that checks the timer flag in the highest priority position in the main loop, checking as frequently as possible. Ensure that the execution time of the main loop is as short and stable as possible. Avoid executing long and uncertain tasks in the main loop.
2.Using the timer’s auto-reload feature (handled by hardware) can reduce some delay errors.
3.Understand and utilize the timer’s prescaler and counting modes to better match the timer overflow period with the polling frequency.
4.Actual measurement: Use an oscilloscope or logic analyzer to measure the actual timing signal generated (for example, toggling an I/O pin on overflow) to visually understand the size and stability of the error.
#Microcontroller #Timer #Polling Method #Interrupt Method