September
1998, Issue 98
PIC'Spectrum
Audio
Spectrum Analyzer
OPTIMIZING
VIDEO INTERRUPTS
Writing the
interrupt routine was another interesting exercise.
One interrupt is generated by timer 0 each 31.77 µs.
This period equals the horizontal refresh period of
a VGA signal in mode 7 (640 × 350).
Because 31.77
µs translates into 254 instructions only (even at 32
MHz), the number of instructions used to do the analog
signal acquisition and the video generation must be
carefully optimized.
Figure 6 shows
the VGA horizontal timing specifications, the corresponding
number of instructions that the PIC17C756 could execute
at 32 MHz, and the operations done by the interrupt
routine in the corresponding time frame (more information
on VGA timings is available on the Web [4]).
|

Figure
6The
top part shows the timings of the video and horizontal
synchronization signals, while the bottom part
shows the conversion of these timings in number
of instructions as well as what the PIC is doing
during this time.
|
The interrupt
routine starts with housekeeping (register saving, timer
reload, etc.) and switches on the horizontal synchronization
signal. During the 3.7 µs needed by this synchronization,
the software manages the acquisition of an analog sample
(one signal sample is taken every two interrupts, giving
a 15.7-kHz digitization rate).
The current
video line number is then incremented and used as an
index into a table, giving the address of the routine
to use for displaying each scan line (vertical synchronization
virtual lines, blank lines, frequency display line,
scale or title display, etc.). Each routine manages
the switching on and off of the three RGB video signals
with good timing.
This brute-force
programming method is needed to achieve the result in
Photo 2. The longer the program takes between the interrupt
and the start of the video display, the more black areas
you have onscreen.
|

Photo
2This
video image was generated by PICSpectrum.
Here, we have a quite clean 1.6-kHz signal. The
third and fifth harmonics are visible under the
fundamental frequency.
|
Real-time
requirements have to be managed very carefully. In particular,
the execution time from the interrupt to the start of
the video display must be rigorously constant, whatever
the results of the if/then tests are.
If it isnt,
the video lines do not align. I was forced to add NOPs
everywherein particular, in the analog acquisition
routinesto ensure that the same number of instructions
is executed whatever the situation is.
To get the
best result, I implemented some strange programming
practices. For example, to get a specific delay with
125-ns resolution, I do a calculated jump in the middle
of a long sequence of NOPs. Its the only solution
Ive found, because managing a loop consumes several
instructions.