Lucky Resistor
Menu
  • Home
  • Learn
    • Learn C++
    • Product Photography for Electronics
      • Required Equipment and Software
    • Soldering for Show
  • Projects
  • Libraries
  • Applications
  • Shop
  • About
    • About Me
    • Contact
    • Stay Informed
  •  
Menu

How to Debug Time Critical Code using an Oscilloscope

Posted on 2014-11-132019-07-24 by Lucky Resistor

From time to time you have to debug time critical code. If you are using interrupts, you are interested in the actual performance on the code. This is especially true, if you are using timed interrupts.

In this cases, using the serial interface is impossible. If you write to the serial interface, the whole timing of your application changes and you will get no usable results.

If you own an oscilloscope, there is a really simple way to debug many applications. I used this technique many times, for example with projects featured on these pages: For the timing of the sound output of the cat protector, or the timing of the display interrupt of the Meggy Jr library.

This technique is obvious ans simple. Nevertheless I hope this article will give some inspiration to help solving timing problems.

Find an Output

First you need an output somewhere. Any output will do and sometimes you can alter the functions of your project just to create an output. For example you can use an attached buzzer, or change an input into an output. In the picture below, you can see the how I attached the probe of the oscilloscope to a handy connector which is actually meant as input.

Connect the Probe

Alter your Code

Next you simple add a signal manually to your code. Set the line you used to output and surround the critical code segment with a simple pulse signal. See below an example of the interrupt for the Meggy Jr library. I added the two lines PORTC |= _BV(5); and PORTC &= ~(_BV(5));.

SIGNAL(TIMER2_COMPA_vect)
{
    PORTC |= _BV(5);
    ledDriver();
    PORTC &= ~(_BV(5));
}

Start the device and setup the oscilloscope. In my case I trigger on a raising edge.

Think! Analyze the Results

Analyzing the results is the actual work which has to be done. First you have to think what you expect from the signal and how it should look like. If you do not get any signal at all, it could be possible your interrupt is never triggered — or you did not set the output and signal right. Always think about all possibilities why things went wrong.

If you are measuring the performance of code executed in a timed interrupt, you will probably get a signal as shown below.

Example Signal

First modern oscilloscopes show you the frequency of the signal. Here you can verify if your code is called in the right intervals. If your code is too slow, you will see a lower frequency as intended. Next you see the ratio: Signal high means interrupt code is executed, signal low means there is time for normal code to be executed.

Conclusion

Sometimes the simplest methods provide the fastest results. Using multiple signals you can for example compare the speed of two code parts or use one as synchronization and the second to mark the code.

Successful debugging,
Lucky Resistor

1 thought on “How to Debug Time Critical Code using an Oscilloscope”

  1. Pingback: Debugging(1): What Kind of Bug is This? – Arduino Craft Corner

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stay Updated

Join me on Mastodon!

Top Posts & Pages

  • Storage Boxes System for 3D Print
  • Event-based Firmware (Part 1/2)
  • Build a 3D Printer Enclosure
  • Yet Another Filament Filter
  • Circle Pattern Generator
  • Circle Pattern Generator
  • Real Time Counter and Integer Overflow
  • Projects
  • Logic Gates Puzzle 11
  • Units of Measurements for Safe C++ Variables

Latest Posts

  • Better Bridging with Slicer Guides2023-02-04
  • Stronger 3D Printed Parts with Vertical Perimeter Linking2023-02-02
  • Logic Gates Puzzle 1012023-02-02
  • Candlelight Emulation – Complexity with Layering2023-02-01
  • Three Ways to Integrate LED Light Into the Modular Lantern2023-01-29
  • The 3D Printed Modular Lantern2023-01-17
  • Rail Grid Alternatives and More Interesting Updates2022-12-09
  • Large Update to the Circle Pattern Generator2022-11-10

Categories

  • 3D Printing
  • Build
  • Common
  • Fail
  • Fun
  • Learn
  • Projects
  • Puzzle
  • Recommendations
  • Request for Comments
  • Review
  • Software
Copyright (c)2022 by Lucky Resistor. All rights reserved.
 

Loading Comments...