Timer Capture - Precision Without Interrupt Overload: How Hardware Takes the Strain Off the CPU |
Subtitle: A Deep Dive into Timer Modules, Capture Registers, and Interrupt Management - with Real-World Designs from Symbol, Zebra, Honeywell, Datalogic, Microchip, NXP, and STMicroelectronics |

|
Opening Summary |
In the previous chapter, we saw how the microcontroller's interrupt service routine handles each edge of the digitised waveform. But there is a problem: every edge triggers an interrupt, and each interrupt requires the CPU to stop what it is doing, save its state, execute the interrupt handler, and then restore its state. This overhead can be significant, especially at high scanning speeds where edges occur every few microseconds. The CPU can become overloaded, missing edges or failing to perform other essential tasks. The solution is timer capture - a hardware mechanism that automatically records the timer value when an edge occurs, without requiring an interrupt for every single edge. |
This article is dedicated to timer capture - the hardware feature that offloads the edge measurement from the CPU. We will explore how the timer capture module works, how it is configured, and how it is used in barcode scanners. We will look at the different types of capture modes, the management of the capture buffer, and the use of interrupts for buffer management rather than for every edge. We will examine how major companies have implemented timer capture in their products. We will see how Symbol (now Zebra) used timer capture in the LS2208, and how they balanced the interrupt load. We will explore Honeywell's use of timer capture with a DMA engine. We will examine Datalogic's use of timer capture in their custom ASIC. We will also look at reference designs from Microchip, NXP, and STMicroelectronics, which showcase the latest timer capture technologies. |
By the end of this journey, you will understand that timer capture is not just a convenient feature but an essential technique for achieving high-performance barcode decoding without overloading the CPU. |

|
Full Article |
Section 1: The Timer Capture Module - A Hardware Assistant |
The timer capture module is a hardware peripheral that automatically records the value of a free-running timer when a specific event occurs on an input pin. The event is typically a rising edge, a falling edge, or either edge of the digitised waveform. The captured timer value is stored in a capture register. The CPU can read the capture register at its leisure, without having to respond immediately to every edge. |
The timer capture module is an essential component of a barcode scanner. It offloads the edge measurement from the CPU, reducing the interrupt load and freeing the CPU for other tasks. The timer capture module is the key to achieving high-performance decoding without overloading the CPU. |
Section 2: The Timer - The Free-Running Counter |
The timer is a free-running counter that increments at a fixed frequency. The timer is typically a 16-bit or 32-bit counter. The timer's frequency determines the resolution of the edge measurement. A higher timer frequency gives a higher resolution. |
The timer is the heart of the timer capture module. The timer's value is recorded when an edge occurs. The timer continues to run, independent of the capture events. |

|
Section 3: The Capture Register - The Storage Location |
The capture register is a register that stores the timer's value when an edge occurs. The capture register is updated automatically by the hardware. The CPU reads the capture register to get the edge time. |
The capture register is typically a 16-bit or 32-bit register, matching the timer's size. The capture register is read by the CPU in the interrupt service routine or by the DMA engine. |
Section 4: The Input Pin - The Signal Source |
The comparator's output is connected to a dedicated input pin of the microcontroller. The input pin is configured as a capture input. The pin's input voltage must match the comparator's output voltage. |
The input pin typically has a Schmitt trigger input. The Schmitt trigger provides hysteresis, which helps to clean up the comparator's output. |

|
Section 5: The Capture Mode - Rising Edge, Falling Edge, or Both |
The timer capture module can be configured to capture on a rising edge, a falling edge, or both edges. For a barcode scanner, the module is typically configured to capture on both edges. This allows the module to measure both the high pulses (white spaces) and the low pulses (black bars). |
The capture mode is set by the firmware. |
Section 6: The Capture Interrupt - A Buffer Management Tool |
The timer capture module can generate an interrupt when a capture occurs. The interrupt is typically used to manage the capture buffer, not to read the capture value. The interrupt signals the CPU that a capture has occurred and that the capture buffer needs to be updated. |
The interrupt is not triggered on every edge. It is triggered on a buffer-full condition or on a timeout. This reduces the interrupt load. |

|
Section 7: Symbol's LS2208 - The Classic Timer Capture |
Symbol's LS2208 uses a timer capture module for edge measurement. The timer is a 16-bit timer with a 1-MHz clock. The capture module is configured to capture on both edges. The capture register is read by the interrupt service routine (ISR). |
The LS2208's ISR is triggered on every edge. The ISR reads the capture value, calculates the pulse width, and stores the pulse width in a circular buffer. The ISR is fast and efficient. |
Section 8: The Interrupt Load - A Potential Bottleneck |
The interrupt load is the amount of CPU time spent handling interrupts. In a barcode scanner, the edge capture interrupts can be a significant source of interrupt load. If the edges occur at a high rate, the CPU may spend a large fraction of its time handling interrupts. |
The interrupt load can be reduced by using a timer capture module with a DMA engine. The DMA engine can transfer the captured values to memory without CPU intervention. |

|
Section 9: The DMA Engine - The Data Mover |
The DMA engine is a hardware block that can transfer data between peripherals and memory without CPU intervention. The DMA engine can be configured to transfer the captured values from the capture register to a buffer in memory. The DMA engine is triggered on each capture. |
The DMA engine offloads the data transfer from the CPU. The CPU is free to perform other tasks, such as decoding and communication. |
Section 10: Honeywell's DMA-Based Timer Capture |
Honeywell's imagers use a timer capture module with a DMA engine. The DMA engine transfers the captured values from the capture register to a buffer in memory. The CPU is not involved in the edge capture. The CPU only reads the buffer when the entire barcode has been captured. |
The DMA-based timer capture reduces the CPU load significantly. The CPU is free to perform other tasks, such as image processing and decoding. |
Section 11: The Double Buffering - A Smooth Data Flow |
Double buffering is a technique that uses two buffers to ensure a smooth data flow. One buffer is being filled by the DMA engine, while the other buffer is being processed by the CPU. The buffers are swapped when one buffer is full. |
Double buffering is used in high-performance scanners to ensure that the data flow is not interrupted. |

|
Section 12: The Capture Buffer - A Circular Buffer |
The capture buffer is a memory area where the captured values are stored. The buffer is typically a circular buffer. The DMA engine writes the captured values to the buffer. The CPU reads the captured values from the buffer. |
The circular buffer decouples the DMA engine from the CPU. The DMA engine writes to the buffer, and the CPU reads from the buffer. |
Section 13: The Buffer Size - A Data Capacity Trade-Off |
The buffer size must be large enough to hold a complete barcode. A typical barcode has 30-50 elements. The buffer is sized to hold 100-200 captured values. |
The buffer size is a trade-off. A larger buffer provides more capacity but consumes more memory. A smaller buffer consumes less memory but may overflow. |
Section 14: The DMA Transfer Complete Interrupt |
The DMA engine can generate an interrupt when the transfer is complete. The interrupt signals the CPU that the buffer is full and ready for processing. |
The DMA transfer complete interrupt is used to trigger the decoder. The decoder reads the buffer and processes the captured values. |

|
Section 15: Datalogic's Timer Capture - A High-Speed Implementation |
Datalogic's industrial scanners use a high-speed timer capture implementation. The timer capture module is integrated into the custom ASIC. The ASIC includes a dedicated timer and capture register. |
The high-speed timer capture is used in Datalogic's PowerScan series, which is designed for high-speed conveyor-belt applications. The timer capture module provides the high resolution and low latency needed for these applications. |
Section 16: The Timer Resolution - A Critical Parameter |
The timer resolution is a critical parameter for timer capture. The timer resolution is the smallest time interval that can be measured. The timer resolution is determined by the timer's frequency. |
A higher timer frequency gives a higher resolution. A 1-MHz timer gives a resolution of 1 microsecond. A 10-MHz timer gives a resolution of 0.1 microsecond. |
Section 17: The Timer Size - Avoiding Overflow |
The timer must be large enough to avoid overflow. The timer overflow occurs when the timer reaches its maximum value and rolls over to zero. The timer overflow causes an error in the pulse width measurement. |
The timer size is typically 16 bits or 32 bits. A 16-bit timer with a 1-MHz clock has a period of 65.5 milliseconds. A 32-bit timer has a much longer period. |

|
Section 18: The Capture Register Overflow - A Data Loss Event |
The capture register can overflow. The capture register overflow occurs when a new capture occurs before the previous capture value has been read. The capture register overflow causes a loss of data. |
To avoid capture register overflow, the DMA engine or the ISR must read the capture register before the next capture occurs. |
Section 19: The Input Filter - A Noise Suppressor |
The timer capture module may include an input filter. The input filter is a digital filter that suppresses noise on the input pin. The input filter prevents false captures caused by noise. |
The input filter is configured by the firmware. The filter's parameters determine the noise suppression. |
Section 20: The Edge Detection - A Clean Transition |
The timer capture module detects the edges of the input signal. The edge detection is done by comparing the current input state to the previous input state. A rising edge is detected when the input changes from low to high. A falling edge is detected when the input changes from high to low. |
The edge detection is a critical part of the timer capture module. The edge detection must be accurate and robust. |

|
Section 21: The Timer Capture in Microchip's Reference Design |
Microchip's reference design for a barcode scanner uses a timer capture module. The design uses a PIC microcontroller with a 16-bit timer and a capture module. The reference design includes a complete code example for Code 39, UPC, and Code 128. |
The Microchip reference design is a useful starting point for engineers developing barcode scanners. |
Section 22: The Timer Capture in NXP's Reference Design |
NXP's reference design uses a timer capture module with a DMA engine. The design uses an LPC microcontroller with a 32-bit timer and a DMA engine. The reference design includes a complete code example for Code 39, UPC, and Code 128. |
The NXP reference design demonstrates the use of a DMA engine for high-performance decoding. |
Section 23: The Timer Capture in STMicroelectronics' Reference Design |
STMicroelectronics' reference design uses a timer capture module. The design uses an STM32 microcontroller with a 16-bit timer and a capture module. The reference design includes a complete code example for Code 39, UPC, and Code 128. |
The STMicroelectronics reference design is a useful starting point for engineers developing barcode scanners. |

|
Section 24: The Timer Capture and the CPU Load |
The timer capture module reduces the CPU load. The CPU does not have to handle every edge. The CPU only reads the captured values from the buffer. |
The CPU load is significantly reduced by using a timer capture module. The CPU is free to perform other tasks, such as decoding and communication. |
Section 25: The Timer Capture and the Interrupt Latency |
The timer capture module reduces the interrupt latency. The interrupt latency is the time between the edge and the execution of the interrupt service routine. The interrupt latency is reduced because the CPU does not have to handle every edge. |
The reduced interrupt latency improves the scanner's performance. |
Section 26: The Timer Capture and the Jitter |
The timer capture module reduces the jitter. The jitter is the uncertainty in the edge timing. The jitter is reduced because the edge is captured by the hardware, not by the software. |
The reduced jitter improves the accuracy of the pulse width measurement. |

|
Section 27: The Timer Capture and the Noise |
The timer capture module is not affected by noise. The noise can cause false edges, but the timer capture module captures the false edges as well. The noise must be filtered out by the input filter or by the decoder. |
Section 28: The Timer Capture and the Missed Edges |
The timer capture module reduces the risk of missed edges. The edge is captured by the hardware, regardless of the CPU's activity. The CPU cannot miss an edge. |
The reduced risk of missed edges improves the scanner's reliability. |
Section 29: The Timer Capture and the Timer Overflow |
The timer overflow must be handled by the firmware. The firmware must detect the timer overflow and adjust the pulse width measurements accordingly. |
The timer overflow handling is a critical part of the firmware. |

|
Section 30: The Timer Capture and the Capture Register Overflow |
The capture register overflow must be handled by the firmware. The firmware must detect the capture register overflow and take appropriate action. |
The capture register overflow handling is a critical part of the firmware. |
Section 31: The Timer Capture and the Buffer Overflow |
The buffer overflow must be handled by the firmware. The firmware must detect the buffer overflow and take appropriate action. |
The buffer overflow handling is a critical part of the firmware. |
Section 32: The Timer Capture and the Decoding |
The timer capture module is used to capture the edge timings. The captured timings are used by the decoder to measure the pulse widths. The decoder is the firmware that interprets the pulse widths and turns them into meaningful data. |

|
Section 33: The Timer Capture and the Communication |
The timer capture module is not directly related to the communication. The communication is handled by the communication interfaces (UART, USB, Bluetooth, etc.). The timer capture module captures the edge timings; the communication interfaces transfer the decoded data. |
Section 34: The Timer Capture and the Power Management |
The timer capture module can be used to reduce the power consumption. The timer capture module can be used to wake the CPU from a low-power sleep mode when an edge occurs. The wake-up time is typically a few microseconds. |
Section 35: The Timer Capture - A Key Enabler |
The timer capture is a key enabler of high-performance barcode decoding. It offloads the edge measurement from the CPU, reducing the interrupt load and freeing the CPU for other tasks. The timer capture module is the key to achieving high-performance decoding without overloading the CPU. |

|
Section 36: The Timer Capture - A Summary of Best Practices |
Based on our exploration, let us summarize the best practices for using timer capture in a barcode scanner: |
1. Use a Timer Capture Module: The timer capture module is essential for high-performance barcode decoding. |
2. Use a High Timer Frequency: The timer frequency must be high enough to resolve the narrowest pulses. |
3. Use a DMA Engine (if available): The DMA engine offloads the data transfer from the CPU. |
4. Use a Circular Buffer: The circular buffer decouples the DMA engine from the CPU. |
5. Use Double Buffering (if needed): Double buffering ensures a smooth data flow. |
6. Handle Overflows: The firmware must handle timer overflows, capture register overflows, and buffer overflows. |
7. Test the System: The timer capture system must be tested with a variety of barcodes, under a variety of conditions, to ensure it is working correctly. |

|
Final Summary |
Timer capture is a hardware mechanism that automatically records the timer value when an edge occurs. It offloads the edge measurement from the CPU, reducing the interrupt load and freeing the CPU for other tasks. The timer capture module is a key enabler of high-performance barcode decoding. |
We have seen how major companies have implemented timer capture in their products. Symbol's LS2208 uses a timer capture module with an interrupt service routine. Honeywell uses a timer capture module with a DMA engine. Datalogic uses a timer capture module in their custom ASIC. Microchip, NXP, and STMicroelectronics provide reference designs that showcase the latest timer capture technologies. |
Timer capture is a critical component of the barcode scanner. It allows the scanner to capture the edge timings with high precision and low CPU load. The timer capture module is the key to achieving high-performance barcode decoding without overloading the CPU. |