In this lesson, you will learn how to use hardware measurement tools to visualize and quantify the invisible timing delays that cause your real-time software to stutter.
When we trigger an output based on an interrupt—such as toggling a GPIO pin inside an ADC conversion handler—we expect it to happen immediately. However, "real-time" is rarely instantaneous. Latency is the fixed delay between an event (like an ADC finishing) and the execution of your code, while jitter is the unpredictable variation in that delay caused by things like higher-priority interrupts or bus contention.
Visualizing the Delay
To debug this, connect a logic analyzer or oscilloscope to two pins:
- Probe A: The source signal (e.g., the signal triggering your ADC or the hardware interrupt request line).
- Probe B: The GPIO pin you toggle at the very first line of your Interrupt Service Routine (ISR).
By measuring the time difference (Δt) between the rising edge of Probe A and the rising edge of Probe B, you capture your interrupt latency. If you observe that this Δt changes slightly every time you trigger the event, you have discovered jitter.
Rectifying Timing Inaccuracies
Once you see the latency, you can identify why it occurs. Often, it is due to "register update delay"—the time the CPU takes to perform atomic operations or read peripheral registers. If your timing requirements are strict, you might see something like this:
void ADC_IRQHandler(void) {
// GPIO High at start of ISR to measure latency on scope
GPIO_PORT_PIN_SET();
// Perform essential register read/write
uint16_t data = ADC_RESULT_REG;
// Code execution here consumes time...
GPIO_PORT_PIN_CLEAR();
}
If your scope shows the SET command taking too long to trigger, you know your CPU is stuck in a higher-priority task or is busy clearing previous interrupt flags. You can "rectify" this by streamlining your ISR to do nothing but the absolute minimum required to acknowledge the hardware, moving heavy processing to your background task loop.
Key Takeaway: Use hardware probes to visualize the gap between event and action; if your "latency" fluctuates, you are witnessing jitter caused by task interference or inefficient interrupt handling.