CPR E 381x/382x - Lab14b

A/D Converter

 

1. Objectives

This lab has two key components. First, it extends the unit on I/O subsystems to yet another unit on the MPC555 microcontroller – i.e., the on-chip analog-to-digital converter (ADC).  ADCs are very common and useful on microcontrollers, since many embedded systems involve analog signals in their environments, such as from sensors, audio, etc. Second, you will apply your deepening understanding of I/O interfaces and programming to the specific interface for the QADC64, the ADC on the MPC555. You will once again see a set of registers, including data, control, and status, and you will use these registers to set up and operate the QADC.  This interface is somewhat more complicated, since there is greater functionality built into the QADC unit, and as a result, as programmers, we have more choices.  We can set up the QADC with a simple program (a set of commands words used inside the QADC) so that it does a series of conversions on its analog input channels under its own control (i.e., the PowerPC CPU does not need to execute instructions for each individual conversion).  Thus, this is an example of an I/O subsystem that is “smarter” in the sense that it can partially run on its own

 

Lab Evaluation Sheet

 

2. Prelab

Browse through the QADCR64.c and QADCR64.h files before starting the lab. Notice that there are functions to set up the ADC and read the ADC after a conversion is done. Polling is used to determine when a conversion is complete.

As usual, you should read through the lab instructions before your laboratory session as well.

3. Setup

As you did in previous labs, make sure you create the folder in your home directory U:\CPRE381\Labw14b to save all your work from this lab.

 

4. Background on the A/D Converter on the MPC555

The basic operation of an ADC is to read an analog input voltage from a source connected to an input pin of the microcontroller and then to convert the analog value to a digital value. Recall the PowerBox.  There are four knobs on the front-panel. Each knob is a potentiometer (variable resistor) connected to an analog input pin of the MPC555. Turning the knob supplies a varying voltage to the analog input.

More specifically, the QADC64 has the following features:

o       10-bit ADC resolution

§         10 bits are used to represent the digital value; thus there are 210 = 1024 possible digital values.

§         The ADC converts an analog input voltage to an integer value in the range 0-1023.

o       The ADC uses either polling or interrupt-driven I/O to indicate when a digital value is ready.

o       The ADC provides programmable channels, where each channel corresponds to an analog signal being read.

§         The channels are numbered AN0-ANx.

 

In the lab, we will use 4 ADC channels, numbered AN0 – AN3. The analog input signal ranges from 0 – 5 Volts.

There are four parts to the lab:

1.      Setup of the ADC

2.      Monitoring the digital results of the ADC

3.      Conversion of the digital results to real-world values in your program

4.      Application – using the real-world values

Chapter 13 of the Motorola MPC555 User's Manual (QADC64 Reference) covers the QADC64 in detail – much of which is beyond the scope of this course.

We will focus on certain parts and operations of the ADC I/O subsystem and programming interface.

After the ADC is initialized, a sequence of scans is set up as a “queue” in the CCW Table; see the diagram below. Each channel to be scanned is added to the queue at successive positions 0, 1, 2, etc. For example, to scan all 4 channels, each would be added to the queue as CCW0, CCW1, CCW2, CCW3. Then, an end-of-queue marker should be added at the next position. The ADC starts the scan and conversion when it is triggered by the enable bit. The ADC will first read the CCWs, one after another until end-of-queue is reached, and for each CCW, it will convert the signal on the specified channel. A conversion on a channel will store a result in the respective position of the Result Table, e.g., the result for CCW0 is stored at Result0, etc. When the scan and conversion is complete for all CCWs, then the ADC sets the completion flag to 1. Now all digital results are available to be read from the Result Table.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Selected details of the QADC64 subsystem are listed below.

 

§         QACR1 – QADC64 Control Register 1

o       16 bit register at 0x30480C

o       SSE1 – bit 2 – Single Scan enable (bit 0 is MSb)

o       MQ1 – bits 3-7

§         Set to binary 00001 to identify Queue 1

§         QASR0 – QADC64 Status Register 0

o       16 bit register at 0x304810

o       ADC sets a flag when the conversion is done

o       CF1 – bit 0 – Conversion Complete flag (bit 0 is MSb)

§         CCW Table

o       table of Conversion Command Words, where each command word specifies how to perform a scan/conversion operation for an input channel

o       CCW: 16 bit command word, starting at address 0x304A00

o       A queue is a scan sequence of one or more input channels.

o       A queue is started by a trigger event, which is a way to cause the QADC64 to begin executing the command words.

o       Each CCW requests the conversion of an analog channel to a digital result. The CCW specifies the analog channel number, the input sample time, and whether the queue is to pause after the current CCW.

§         Result Word Table

o       table of Result Words, where each result word is the digital result of a conversion

o       Results from a sequence of conversions are placed in the Result Word Table.

o       RW: 16 bit result word, starting at address 0x304A80

 

5. Set Up

To use the ADC, you will need to set up the A/D I/O subsystem of the MPC555.  This is done with the following commands:

·         Reset the ADC queue

·         Add (to the queue) each analog input channel to be scanned; e.g., four channels, 0 through 3 (AN0-AN3)

·         Add an end-of-queue marker to terminate the scan sequence

·         Start a conversion on the ADC, which begins reading each analog input and converting it to a digital value

Look through the predefined module, QADCR64.c and QADCR64.h, for functions that accomplish these commands.  Notice the functions in the “.h” header file are the ones you are expected to call from your main program.

Create a main program that calls functions to reset the ADC, add channels 0 through 3 using the QADCR64_QCKL_MAX value for the setting parameter, add the QADCR64_END_QUEUE channel (which serves as an end-of-queue marker), and start a conversion.

You should compile the code at this time to see if it compiles without errors, but it isn’t ready to be run yet.

Once the ADC is set up correctly, you need only to read (monitor) the stream of values; you do not need to set up the ADC for each and every value.

6. Monitoring the ADC

For this part of the lab, you should set up the ADC, as in Part 1, and monitor the raw digital values calculated by the ADC and stored in the Result Table.  After a conversion by the ADC is started, your program must wait until the conversion is complete.  Use functions defined in the QADC module to check the status of the conversion and then to read the digital values back from the ADC.

Set up a loop in your program to read the channels repeatedly, such as every 100 ms.  In other words, after the initial function calls to add the channels to the queue, simply write a loop that includes calls to the functions to start the conversion, wait for completion, and get each of the results, followed by a short delay before repeating the loop again.

Add this loop to your main program from Part 1.

Next, add C code to the loop to display the digital values from analog input channels 0-3 of the ADC to the LCD screen in the following format:

0: XXXX
1: XXXX
2: XXXX
3: XXXX

where XXXX is a digital value read in from the ADC, and each line represents an analog input channel.  The digital values may be displayed in hex or decimal format. Don’t forget to initialize the LCD.

Compile and run your program.

Analog input channels 0-3 are connected to the rotating knobs on the PowerBox, so you can adjust the values being read into the ADC. Adjust the knobs and watch the digital values change. 

Question: What are the minimum and maximum digital values from the ADC?

7. Conversion of ADC values

Your program now has digital values for the input signals. It is often useful to map the digital value (an integer) back to its real-world, continuously-varying value (a floating-point number).  In this part of the lab, you will calculate real-world values represented by the digital values.

For each of the PowerBox knobs, the physical real-world value is a voltage from 0 – 5 V. But, we are going to interpret the knobs differently (just for the fun of it).


We return to the Tractor application, and we assume that the four knobs on the PowerBox have the following interpretations:

·         Channel 0 - Contrast of the LCD display

Adjust the contrast of the LCD display in the range 0 to 100%.

·         Channel 1 - Loop timing

Adjust the timing loop (how often the LCD display is updated) in the range 100 ms to 1000 ms.

·         Channel 2 – Sprayer/nozzle output

Adjust the (hypothetical) sprayer output (range: 0 liters per acre to 200 lpa)

·         Channel 3 – Nozzle alignment with boom

Adjust the (hypothetical) angle of the nozzles (range: -90 degrees to 90 degrees)

Write code to add the YYY.Y real-world values to the output display, as follows:

0:    XXXX   YYY.Y%
1:    XXXX   YYY.Y ms
2:    XXXX   YYY.Y lpa
3:    XXXX   YYY.Y degrees

That is, an XXXX value is the digital result from the conversion on that channel; and YYY.Y is the real-world value it is being mapped to. Before outputting, you will need to calculate the YYY.Y values.  This is based on the proportionality relation.

Recall the variables and equations for ADC calculations. In this part, you are writing software to convert the digital results back to real-world values. That is, the actual real-world signals from the knobs were converted from analog to digital by the ADC hardware; now we want to calculate real-world values given the digital values. For the following variables and equations, the analog values refer to the quantities being simulated by the knobs, e.g., knob 3 is simulating the angle of the nozzle in degrees (e.g., its offset is –90).

·         Offset: minimum analog value

·         Span (or Range): difference between maximum and minimum analog values

o        Max - Min

·         n: number of bits in digital code (sometimes referred to as n-bit resolution)

·         Bit Weight: analog value corresponding to a bit in the digital number

·         Step Size (or Resolution): smallest analog change resulting from changing one bit in the digital number, or the analog difference between two consecutive digital numbers; also the bit weight of the LSb

o        Span / 2n

Let AV be Analog Value; DN be Digital Number:

AV = DN * Step Size + Offset = DN / 2n * Span + Offset

DN = (AV - Offset) / Step Size = (AV - Offset) * 2n / Span

 

Write equations in C to calculate AV for each of the knobs/channels in your code. You do not need to do anything else with the values in this part – simply display the digital and analog values on the LCD.

Use appropriate output formatting for the values in the display.

Question: Check that your calculations are correct and the real-world values follow the digital values before you move on to the next part. How did you check your calculations?

8. Application

Now you will do something with the calculated values from Part 3.  The real-world values will now be used by the tractor application. For example, in a real system, you might be moving a valve or controlling a motor based on the current value of an analog input. Add the following behaviors to your program:

·         Contrast - Adjust the contrast of the LCD display using the QTerm C library (look up how to adjust contrast)

·         Timing - Make the loop that updates the LCD display run faster or slower

·         Sprayer output - If bit 0 of DIP Switch 1 is set and the sprayer rate is greater than 70 lpa, turn on bit 7 of LED Bargraph 2. (Not very imaginative, but it does something!)

·         Nozzle alignment - If the nozzle angle exceeds 60 degrees in either direction (>60 deg, < -60 deg), turn on bit 6 of LED Bargraph 2. (Ditto.)

Compile, run, and test your program. Demonstrate the program to your lab instructor.