Data Acquisition Using Analog to Digital Conversion
This chapter describes the analog inputs available on the QCard Controller, explains how to connect the converters to external signals, and details the built-in C-language driver routines that make the analog inputs easy to use. Simple code is presented to calculate measured voltages based on A/D readings.
Data acquisition using the QCard Controller
Many instrument control and data acquisition applications require monitoring of analog signals. Analog to digital (A/D or ATD) converters can perform this function. An A/D converter samples analog signals and converts them to digital values that can be stored, processed, or displayed.
The resolution of an A/D is specified in bits. For example, an 8-bit A/D converts an analog signal into one of 256 discrete digital numbers.
Table 9-1 Analog I/O
|I/O Lines||Type||Port Address||Comments / Alternate Uses|
|8||8-bit 0-5 V analog inputs||PE 0-7||Alternately may be used as digital inputs.|
The QCard Controller Single Board Computer (SBC) hosts an analog to digital converter to address a wide variety of instrumentation and control applications. It includes an 8-channel 8-bit analog to digital (A/D) converter that is built into the 68HC11 processor chip. It converts unipolar signals with a nominal 0 to +5 volt range;
The analog inputs are brought out to the 24 pin Field Header on the QCard; the connector diagrams in Appendix B specify the pin assignments.
The 8 bit A/D converter
The 8 bit A/D converter in the processor converts unipolar signals with a nominal 0 to +5 volt range, and conversion results are returned in registers in the 68HC11. The analog inputs are connected to the PORTE pins on the processor; these can be used as digital inputs if the 8 bit A/D is disabled.
Examining the demonstration program
The code discussed in this section is located in the file named
AD8.C in the
\MOSAIC\DEMOS_AND_DRIVERS\MISC\C EXAMPLES directory. Most of the functions in this file are interactive versions of functions declared in the
ANALOG.H file in the
\MOSAIC\FABIUS\INCLUDE\MOSAIC directory; they are described in detail in the Control-C Glossary.
We recommend that you compile and download the
AD8.C program now so that you can interactively work through the exercises in this chapter. Simply use your TextPad editor to open
AD8.C, and click on the Make Tool to create the download file named
AD8.DLF. Then enter the Mosaic Terminal program, and type:
to terminate any prior multitasking program that might be running, and select the "Send File" menu item to send
AD8.DLF to the QCard Controller.
Fundamentals of analog to digital conversion
An analog to digital converter samples an analog signal and outputs a digital number that is proportional to the analog signal. The A/D converters on the QCard Controller sample input voltages and communicate the digital result to the 68HC11 processor.
The analog input signal must be within the input range of the A/D converter. On the QCard Controller, the lower bound of the range is equal to the voltage on VRL (voltage reference/low) and the upper bound of the allowable input range is equal to the voltage on VRH (voltage reference/high). By default, their values are 0 Volts (analog ground) and 5.0 Volts (analog +5V), respectively.
The converter measures the input voltage with a certain specified resolution. The resolution is the granularity with which the measurement is performed. It can be specified as a number of bits or as a voltage increment. For example, an A/D converter with only 1 bit of resolution and a 5 Volt input range would classify all voltages from 0 to just under 2.5 Volts as the digital value 0, and voltages from 2.5 Volts to 5 Volts as the digital value 1. This converter has a resolution equal to 1 bit, which corresponds to 2.5 Volts per count.
The converter measures the input voltage with a specified accuracy. The accuracy tells how close the measured value is to the actual voltage. A typical 8 bit A/D converter is accurate to within plus or minus one least significant bit.
Determining the resolution of an A/D converter
The VRL and VRH analog input reference pins define the lower and upper voltages that can be converted by the 8 bit A/D. The resolution depends on the input voltage range. The measurement resolution of a B-bit A/D, expressed in Volts per count, is
Eqn. 9-1 Resolution = (VRH - VRL) / 2B [Volts per count]
where 2B is the number of counts that can be represented by a B-bit number. From this equation we see that resolution becomes finer (better) as B grows larger or as the reference voltage range (VRH - VRL) gets smaller. For the 8 bit A/D the resolution is
Eqn. 9-2 8 bit Resolution = (VRH - VRL)/256 [Volts per count]
With the default VRL and VRH of 0 V and 5 V, respectively, the resolution of the 8 bit converter is 19.5 mV per count.
Converting an A/D count into its equivalent voltage reading
To convert the 8-bit count returned by the A/D converter into an equivalent voltage, use the formula
Eqn. 9-3 Input Voltage = VRL + ( Count * Resolution)
Combining this with Eqn. 9-1 yields
Eqn. 9-4 Input Voltage = VRL + Count * (VRH - VRL) / 2B
Figure 9-1 Conversion function for the 8-bit A/D.
Using the 8-bit A/D
Initializing the 8 bit A/D
The 8 bit A/D is inside the 68HC11 processor chip, and is accessed via
PORTE. This 8 bit port can be used as either an 8 channel A/D or as an octal digital input port. When the QCard Controller is first powered up, or after a reset or restart, the 8 bit A/D converter is disabled and
PORTE is configured as a digital input port. To turn on the A/D converter, a program must call the function:
Another function named
AD8Off() is available to turn off the 8 bit A/D so that PORTE once again acts as a digital input port.
The function named
defined in the
AD8.C file calls
AD8On(). If you interactively execute
the 8 bit A/D converter will be turned on and ready for use.
In an autostarting application you should include
InitAnalog() in your start-up code.
To sample an analog voltage, attach a voltage with a value between zero and +5 Volts to the 8 bit A/D channel 0 input named
PE0. The 8 bit A/D inputs are the PORTE signals named PE0 through PE7 available at pins 17 through 24 on the Field Header (see Appendix A). Each analog signal is converted to a number between 0 and 255 indicating its value relative to VRL (the low voltage reference) and VRH (the high voltage reference). The default values are VRL = 0 Volts (analog ground) and VRH = 5 Volts. The exact voltage difference between VRH and VRL varies slightly from board to board; it is a good idea to measure the value on your board to obtain the most exact voltage equivalents of the measured A/D results.
Interactively perform the conversion
Convert8() function is defined near the middle of the
AD8.C file; you can see from its definition that it is simply an interactively callable version of the
AD8Sample() function. Now that you have connected an input voltage to channel
PE0, you can type from your terminal:
Convert8( int 0)↓
The printed return value summary displays the conversion count; you’ll see a printout that looks something like this for a 1.5 volt signal:
Rtn: -30722 77 =0x87FE004D =fp: -3.822E-34
We know that this function returns an integer, so we identify "77" (hex 0x004D) as the return value; the other numbers in the summary are irrelevant. If the input is 1.5 volts, the result should be approximately 77 counts (256 * 1.5 / 5.0). If the input voltage equals VRL (the low reference voltage, typically at 0 Volts), the result will equal 0. If the input is within 1 bit of VRH (the high reference voltage, typically at 5.0 Volts), the result will equal decimal 255. If the input is exactly half of (VRH - VRL), the result will equal decimal 128.
Multiple 8 Bit A/D conversions with results stored in a C array
AD8Multiple() which is defined in the
ANALOG.H file expects as inputs a buffer xaddress (32 bit extended address), a sampling interval parameter, the number of samples, and a channel number. When called, it performs the specified number of conversions and saves the results as single bytes in the specified buffer. As explained in the Glossary, the sampling interval parameter specifies the timing of the samples, with 0 representing the fastest sampling, and 65,535 representing the slowest sampling. You can sample at up to 100 kHz (100,000 samples per second) using the 8-bit A/D provided the processor is devoted to this single task.
AD8ToCArray() function defined in
AD8.C uses a standard C one-dimensional array named
results_8 as the data buffer. The function accepts a channel number as input, performs conversions at the fastest sampling speed (100 kHz), and places the results in the array. The number of samples is specified by the
DEFAULT_NUMSAMPLES constant which equals 16. Because we have connected the channel 0 input (AN0), let’s perform the multiple conversion on this channel. Type at your terminal:
AD8ToCArray( int 0)↓
remembering to type at least one space after the
( character. This function does not return a value, so the printed return value summary is not relevant. To see the results of the conversion, you could write a simple C function that prints the contents of
results_8; this is left as an exercise for you. You can also use the debugging routine named
DUMP to view a hexadecimal dump of the buffer. To do this, type at your terminal:
results_8 0 16 DUMP↓
DUMP is a QED-Forth function that expects as inputs an address (in this case,
results_8 puts the address on the data stack), a page (0, representing the common page), and the number of bytes to be dumped (16). You should see a printout similar to this if you connected a 1.5 volt signal:
pg addr 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF 00 8E2D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D MMMMMMMMMMMMMMMM
This tells us that the contents of the 16 bytes starting at address 0x8E2D (which is the address of
results_8) all equal 0x4D. To the right of the hex dump the ascii equivalent of the memory contents is displayed; this is not relevant for the numerical data here. We conclude that the A/D conversion result was 0x4D, equivalent to decimal 77; this is the same value returned by the
Convert8() function above. Your results may vary slightly depending on the exact value of supply voltage on the QCard Controller, as the +5V supply is used as the default reference voltage.
For experts: how to use additional features of the 8 bit A/D
The built-in driver routines for the 8 bit A/D are easy to use and address the requirements of most applications. If you wish to gain a detailed understanding of the operation of the 8 bit A/D, or need to use one of its special modes, the information in the Analog-To-Digital Section of the MC68HC11F1 Technical Data Manual may prove useful. For example, one of the operating modes allows the 8 bit A/D converter to continuously sample four different analog inputs in rapid succession.