Reading and Writing Continuous Signals

Background

Many of the signals that a computer must sense and control are continuous -- they can take on any value in a given range. Continuous signals are usually used to represent physical quantities like sound or temperature. The signals are not the actual physical quantities, but rather voltages that are analogs of those quantities. For example, a temperature might be represented by a voltage that rises when the temperature rises and falls when the temperature falls. Thus the behavior of the voltage is analogous to the behavior of the temperature.

A computer cannot represent the arbitrary values taken on by a continuous signal, nor can it deal with arbitrary points in time. Therefore the computer approximates the continuous signal with a sequence of samples. This process is similar to that by which a movie is made: The movie is really a sequence of still pictures, each representing a sample of the scene being depicted. When we are shown the sequence of samples, at the same rate that they were taken, our brain perceives that sequence as showing continuous motion.

Continuous signals are input to the computer via an analog-to-digital (A/D) converter, and output via a digital-to-analog (D/A) converter. When the computer reads from the A/D converter, the converter samples the continuous input voltage and delivers its value as an integer. When the computer writes an integer to the D/A converter, the converter sets the continuous output voltage to that value. Thus, although the continuous input voltage to an A/D converter may vary smoothly, the continuous output voltage from a D/A converter varies in discrete steps.

The quality of the whole process depends on the accuracy of each sample and the frequency with which we sample a signal. Accuracy is determined by the number of bits in the integer representation of the voltage. The converters in the MB5 use an 8-bit representation. Assuming that the signal is within the range of the converter, an 8-bit representation means that the maximum error in a sample is one part in 512 (about 0.2%).

Conversion of an integer to a voltage is essentially instantaneous. Thus the maximum rate at which samples can be output by a program is determined by the length of the shortest loop that can repeatedly execute the write instruction with the appropriate samples. Conversion of a voltage to an integer, on the other hand, involves a successive approximation process in which the A/D converter searches for the closest representation. In the worst case, the A/D converters in the laboratory require 2.5 microseconds to deliver a sample. (Experience has shown that the conversion is usually faster, around 0.7 microseconds.) This places a limit on the input sampling frequency.

Digital-to-Analog Conversion

Each MB5 has a single D/A converter, mapped to the address $400001. Writing the 8-bit value 0 to $400001 causes the D/A converter to set its output voltage to 0; writing the value 255 causes the output voltage to be set to 4.9805. Each increase of 1 in the value written increases the output voltage by 0.0195 volts. (Click here for the data sheet describing the D/A converter chip used on the MB5.)

The output of the D/A converter is connected to an audio driver built on a ``piggy-back'' board on the MB5. The output of the audio driver is designed to be compatible with a standard headset or a PC's speakers. In order to produce a pure tone at the output of the audio driver, we need to approximate a sine wave. To do this, we need to think about the characteristics of the sine function:

  1. Its value ranges from -1 to 1
  2. Its slope at 0 is 1
  3. It is periodic with period 2π
  4. Within one period, the portion from π to 2π is a reflection about the horizontal of the portion from 0 to π, and the portion from π/2 to π is a reflection about the vertical of the portion from 0 to π/2.

The MB5's audio driver shifts the level of the signal coming out of the D/A converter so that it is (approximately) symmetric about 0. Thus writing the value 0 to location $400001 results in the most negative value out of the audio driver, writing the value 128 results in a 0 value, and writing 255 results in the most positive value. As usual, there is one more negative voltage value than positive value. We often take advantage of this asymmetry, using the 0 value to indicate the end of a sequence of values to be output.

How accurately can we represent the sine function with our D/A converter? Since the sine function takes on values from -1 to 1, and the D/A converter can produce 127 equally-spaced negative and 127 equally-spaced positive voltages, we can regard each voltage step as being 1/127. (If we used all 256 possible values, we would have an asymmetric signal with a larger negative peak than positive peak.)

The slope of the sine function is 1 at 0. That means that when we increase the voltage from 0 to 1/127 (along the vertical axis of a plot of the sine function), we should increase the time from 0 to 1/127 (along the horizontal axis of a plot of the sine function). Thus we write the following value to the D/A converter at time i/127 for i=0, 1, 2,...:

128 + 127 * sin(i/127)
200/127 is approximately π/2, so 200 samples constitutes a quarter of the cycle. Given these 200 values, we can use the symmetry properties quoted above to construct a table representing a full cycle. (Click here to see the actual sequence of values.)

We can construct a signal of any length by replicating the single-cycle table. To output the signal, we need only write a loop that outputs the elements of the table to the D/A converter in order. When we plug a pair of headphones into the MB5, we should hear a pure tone.

The constructed table for the sine wave provides no information about the frequency of the tone we will hear: Frequency depends on how many cycles are output in a second which, in turn, depends on the time that elapses between writing one sample to the D/A converter and writing the next. Suppose that we wrote one sample to the D/A converter every millisecond. Then we would write one thousand samples each second, and since it takes 800 samples to describe one cycle that means about 1.25 cycles each second.

It's always possible to reduce the frequency of the output signal by increasing the delay between writing one sample and writing the next. We cannot, however, reduce the delay between writing one sample and the next to zero. Thus there is a limit on the highest possible output frequency imposed by the efficiency of the program we write and the speed of the machine. We have no control over the speed of the machine, but we can try to make the program as efficient as possible.

Appendix IVD of the text specifies the number of clock cycles required by each instruction, and by the computation of each effective address. The MB5 drives the MC68000 with a 16MHz clock, and therefore one clock cycle is completed every 62.5 nanoseconds. Both memory read and memory write require 4 clock cycles, as assumed in Appendix IVD. Given a proposed loop to output samples, this information can be used to calculate the time that will elapse between samples and therefore the time needed to output one cycle.

It may be that even the most efficient program one can devise is not fast enough to produce the required frequency. In that case, the number of samples per cycle must be reduced. For example, we can double the frequency output by a given program by cutting the number of samples per cycle in half. This could be done by simply using every second sample from the 800-element table, rather than re-calculating all of the sample values.

Clearly there will come a point when the accuracy of the representation is sufficiently degraded that the sound is no longer a pure tone. If you take ECEN 4632, then you will learn how to predict the amount of degradation and how to compensate for it in other parts of the system. For our purposes, however, reducing the number of samples to 20 per cycle should give an adequate representation. Thus we should be able to obtain a reasonable tone at 40 times the highest frequency possible with the 800-element table.

Analog-to-Digital Conversion

Each MB5 has a single 8-channel A/D converter, mapped to odd addresses in the range $300001 through $30000F. Reading from address $300001 causes the A/D converter to sample its channel-1 input voltage, and deliver the result as an 8-bit value. Reading from address $300003 causes the A/D converter to sample its channel-2 input voltage, and so forth. An input voltage of 0 volts results in a 0 value, and input voltage of 4.9805 volts results in a 255 value. Between these limits, each increase of 0.0195 volts increases the value delivered by 1. (Click here for the data sheet describing the A/D converter chip used on the MB5.)

Channel 1 of the A/D converter is connected to the output of an audio amplifier on the MB5's ``piggy-back'' board. This amplifier shifts the level of the symmetric input signal so that the most negative input voltage is represented by an amplifier output of 0 volts and the most positive input voltage is represented by an amplifier output of 4.9805 volts. Notice that this behavior is exactly the reverse of the behavior of the audio driver discussed above. That means the computer can ``copy'' sound by reading samples from channel 1 of the A/D converter and writing them to the D/A converter: The input signal is shifted into the range 0-5 volts, and each sample is converted into an 8-bit value. These 8-bit values are then used to control a voltage in the range 0-5, which is shifted to become the desired output signal.


Instructor Revision 1.9 (2003/10/17 20:13:05)