Analog Input

Purpose

To introduce the sampling of analog input signals and to give you more practice with counter/timers and interrupt handling.

Due dates

You must hand in your answers to the design questions at the beginning of your laboratory session on Thursday, November 12. This part of the assignment is worth 20 points.

You must be prepared to discuss your work at the beginning of your laboratory session on Tuesday, November 17, and to demonstrate your program to your TA during that period. This part of the assignment is worth 30 points.

Background

In previous assignments, you have seen how a continuous voltage can be approximated by a sequence of samples, and how the sample interval can be controlled by a counter/timer. You have used a D/A converter to synthesize a pure tone from a set of computed values that were samples from a sine wave.

The computer could copy a voltage by sampling it with the A/D converter and writing the samples to the D/A converter. If the signal is to be reproduced accurately, it must be sampled at twice the highest frequency component. For speech, good quality can be obtained with about 8,000 samples per second; music could require up to 40,000 samples per second.

The A/D and D/A converters form an input/output pair in much the same way as the UART forms an input/output pair. Suppose that a program is reading a signal from the A/D converter, possibly processing it in some way, and then writing it to the D/A converter. In this case the same sampling rate should be used to control both the reading and the writing. The simplest implementation ties the two sampling rates by placing both operations -- reading from the A/D converter and writing to the D/A converter -- in the same interrupt handler.

This leads to the idea of an A/D converter module that is similar to the module used to encapsulate the UART in a previous homework: Two circular buffers provide communication between an interrupt handler in the module and the remainder of the program, and the module exports C-callable routines to

Operations that enable and disable counter interrupts are similar to those implemented in the previous assignment.

Obtaining a sample from the A/D converter is simply a matter of reading a byte from location $300001. (Use an EQU to map this address to an appropriate symbol.) The A/D converter works by successively approximating the analog signal, effectively searching for the byte value that best matches the voltage. This process takes time, so when the CPU reads a byte there is a delay before the value becomes available. The result is that a move.b instruction whose source operand is $300001 can take up to 2 microseconds to complete. This is in sharp contrast to the 0.75 microseconds required for the same instruction with a source operand in memory and data register destination.

When an interrupt occurs, the interrupt handler must obtain a sample from the A/D converter and add it to the A/D circular buffer. (Review the earlier discussion about using circular buffers for communication between interrupt handlers and other programs.) It must then remove a sample from the D/A circular buffer and write it to the D/A converter. Thus, for each interrupt, a sample is read and a sample is written. The sampling rates for input and output are therefore the same.

Equipment

Each group will need access to a portable radio, cassette tape player, CD player or an MP3 player with a pair of headphones. The plug attached to the MB5 is used to plug into the radio, cassette tape player, CD player or MP3 player. The headphones are plugged into the socket on the MB5 (as in the previous two labs).

If the sound coming from the headphones sounds distorted, turn down the volume on the sound source.

Task

Design and implement a program that provides a sound storage and playback facility. The overall behavior of your program can be described as follows.

The program samples an input signal by reading samples from the A/D converter at a rate of approximately 15,000 samples per second. Normally, these samples should be written to the D/A converter.

When the user sends the r to UART 2, the program begins to store the samples as well as writing them. Samples are stored until either:

Sending any character other than p or r simply causes storage of samples to cease; samples are still written to the D/A converter.

Sending r at any time (including the time during which samples are being stored) starts the storage of samples anew at the beginning of the storage area.

Sending p at any time (including the time during which samples are being played back from the storage area) starts the playback of samples anew at the beginning of the storage area. Stored samples are played back until either:

Sending any character other than p or r simply causes storing of samples or playback of stored samples to cease. The program resumes writing input samples to the D/A converter.

If the playback is terminated by exhaustion of stored samples, the output of the D/A converter should produce silence. (Thus no sound should reach the headphones.) Sending p will then play the current buffer again, sending r will start recording and copying, and sending any other character will start copying.

Your program must contain at least three source files (it may contain more):

After enabling the A/D conversion process, the main program must enter a loop that monitors the devices. It must copy samples from the A/D circular buffer to the D/A circular buffer when that is appropriate, and alter its behavior in response to characters it receives from the UART. The current behavior (recording, playing, etc.) determines what the program does with extracted samples, and which samples it inserts.

You may use a specific character (like Control-C) to terminate the program, or you may simply let the loop run forever.

No code is supplied in support of this assignment. Feel free to re-use any code that was given to you earlier or that you developed for earlier assignments.

Design Questions

  1. (5 points) One approach to this assignment is to write all the code with the expectation of all features working the first time. Briefly describe a step-by-step strategy to incrementally test and get modules and interface features working.

  2. (5 points) What command word and value would should you write to initialize the counter/timer that provides the converter interrupts?

  3. (10 points) Your main program contains a loop that deals with each device. It moves samples between buffers and storage, checks for character input, and modifies its behavior accordingly. What are the timing constraints on this loop? Explain carefully what strategies you will follow to ensure that no samples are lost.

Demonstration

You must demonstrate your program to your TA on one of the machines in the Microlab.
Instructor Revision $Revision: 1.38 $ ($Date: 2007/08/27 00:15:44 $)